0

我正在使用 Microsoft Office 2010。

我想创建一个在单击时刷新“AdvancedFilter”的按钮。
我所做的是录制一个宏以重播我的操作。
但是宏在第一次执行时就已经失败了。

Sub Test()
'
' Test Macro
'

Workbooks("requirement_spec.xls") _
    .Sheets("Requirements").Range("A4:BU279").AdvancedFilter _
        Action:=xlFilterCopy, _
        CriteriaRange:=Range("A1:F5"), _
        CopyToRange:=Range("A11:BU11"), _
        Unique:=False
End Sub

通过 Excel 的宏菜单或按钮启动宏会导致此错误:

运行时错误“1004”:
Range 类的 AdvancedFilter 方法失败

生成错误消息时,我没有更改自动录制的宏的字符。
(我刚刚为 SO 做了一些突破性的改变)

4

2 回答 2

3

我今天在 Excel 2013 中遇到了类似的问题。来到这里,我注意到没有真正解决方案的老问题。

What I found out, is that the error does not appear when a range in the sheet with the filter is being selected. 因此,以下内容对我有用:

Sub Test()
'
' Test Macro
'
Dim wb As Workbook, wbSave As Workbook, _
    ws As Worksheet, wsSave As Worksheet, rSel As Range
'don't annoy users with changing the selection
Application.ScreenUpdating = False
'save old selection values to be sure
Set wbSave = ActiveWorkbook
Set wsSave = ActiveSheet
Set rSel = Selection
'activate target workbook + select something
'in my case it's a sheet in the same test workbook
Set wb = Workbooks("requirement_spec.xls")
wb.Activate
Sheets("Filter").Select
Range("A11").Select
'apply filter now    
Sheets("Requirements").Range("A4:BU279").AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=Range("A1:F5"), CopyToRange:=Range("A11"), Unique:=False
'restore old selection to be sure
wbSave.Activate
wsSave.Activate
rSel.Select
'allow screen updating again
Application.ScreenUpdating = True
End Sub
于 2014-07-16T21:15:31.760 回答
0

也许尝试:

Workbooks("requirement_spec.xls") _
    .Sheets("Requirements").Range("A4:BU279").AdvancedFilter _
        Action:=xlFilterCopy, _
        CriteriaRange:=Range("A1:F5"), _
        CopyToRange:=Range("A11"), _
        Unique:=False
End Sub

所以你希望从大约 50 列(A4 到 BU 279)的 275 行中复制到同一个工作表 Range("A11") ?

于 2013-05-14T09:31:59.987 回答