自动过滤从访问中打开受保护的Excel工作表

时间:2014-09-03 15:06:19

标签: excel vba excel-vba ms-access access-vba

我目前正在将一些代码从excel移动到访问。在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字段上收到错误:= 3,Critera1:= 110,运算符:= 7 我不能选择一个自动过滤的范围,因为工作表受到保护,我没有写访问权限。工作表上已经有自动过滤器,我只需要设置一个值。

是否有人在访问或excel中都知道这个解决方案,但最好是两者?

由于

1 个答案:

答案 0 :(得分:0)

我认为需要将AutoFilter应用于Range。你的"用"语句解析为工作表,而不是范围。

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

我希望这会有所帮助。

相关问题