【发布时间】: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 中解决这个问题的方法,但最好是两者都有?
谢谢
【问题讨论】: