自动过滤从访问中打开的受保护的 Excel 工作表
Posted
技术标签:
【中文标题】自动过滤从访问中打开的受保护的 Excel 工作表【英文标题】:Autofilter a protected excel sheet opened from access 【发布时间】:2014-09-03 15:06:18 【问题描述】:我目前正在将一些代码从 excel 移动到 access。在 excel 中有一个按钮可以打开另一个 excel 文档并应用自动过滤器。
Dim cell As Long
cell = Sheet2.Cells(9, "i").Value
Workbooks.Open Filename:= _
"C:/worksheet1.xls"
Selection.AutoFilter Field:=3, Criteria1:=cell
这是来自 excel 的代码,它曾经可以正常工作,但现在也抛出错误,因为工作表受到保护。
使用我从这个线程Autofilter Excel with VBA得到的一些代码
我想出了应该在访问中工作但不能工作的代码
到目前为止我所拥有的是
Dim oApp As Object
Dim wb As Object
Dim ws As Object
Set oApp = CreateObject("Excel.Application")
oApp.Visible = True
'tries to open workbook
On Error Resume Next
'change file path to the correct one
Set wb = oApp.Workbooks.Open(FileName:="C:/worksheet1.xls")
On Error GoTo 0
'if workbook succesfully opened, continue code
If Not wb Is Nothing Then
'specify worksheet name
Set ws = wb.Worksheets("BOM")
With ws
'apply new filter
.Cells(3, 3).Select
.AutoFilter Field:=3, Criteria1:=110, Operator:=7
End With
End If
Set wb = Nothing
Set oApp = Nothing
我在 .AutoFilter Field:=3, Critera1:=110, Operator:=7 上遇到错误 我不能只选择一个范围进行自动筛选,因为工作表受到保护并且我没有写访问权限。工作表上已经有自动过滤器,我只需要设置一个的值。
有没有人知道在 access 或 excel 中解决这个问题的方法,但最好是两者都有?
谢谢
【问题讨论】:
【参考方案1】:我认为需要将自动过滤器应用于范围。您的“With”语句解析为工作表,而不是范围。
ws.Range(xxxxxx).Autofilter Field:=3, Criteria1:=110, Operator:=7
除非有权访问工作表的人使用以下链接中的信息更改保护,否则这是无法做到的。
http://office.microsoft.com/en-us/excel-help/enable-autofilter-functionality-for-a-protected-worksheet-HA001098270.aspx
我希望这会有所帮助。
【讨论】:
您是正确的,但不幸的是,这不能应用于受保护的工作表。由于我没有写权限,我无法将其更改为不受保护。 我添加了一个 Microsoft 链接,该链接准确地解释了您所面临的问题。请重新访问我修改后的答案。这不是你想听到的,但它是一个答案。 感谢您的帮助!至少我现在知道了以上是关于自动过滤从访问中打开的受保护的 Excel 工作表的主要内容,如果未能解决你的问题,请参考以下文章