我正在使用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做了一些突破性的改变)
答案 0 :(得分:3)
今天我遇到了与Excel 2013类似的东西。来到这里,我注意到了一个没有真正解决方案的旧问题。
我发现,当选择带有过滤器的工作表中的范围时,不会出现错误。因此,以下内容对我有用:
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
答案 1 :(得分:0)
也许试试:
Workbooks("requirement_spec.xls") _
.Sheets("Requirements").Range("A4:BU279").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Range("A1:F5"), _
CopyToRange:=Range("A11"), _
Unique:=False
End Sub
所以你希望275行(约50列(A4到BU 279))将被复制到相同的工作表范围(“A11”)?