我的宏出现越界错误

时间:2019-06-05 19:41:57

标签: excel vba

我正在制作一个VBA脚本,该脚本从页面中提取一个excel表格,将其复制到当前页面中,然后应用一个记录的宏来更改表格过滤器。问题出现在过滤器中,每隔运行一次脚本就会出现一个超出范围的错误。

我的猜测是,由于该项目已经设置在某个过滤器下,因此发生这种情况,如果我重新运行该宏,它将不知道如何处理这种情况。

我很好奇是否有人可以找到解决方案。

Sub FilterCES()

ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=27, Operator:= _
    xlFilterValues, Criteria2:=Array(0, "6/3/2019")

ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=10, Criteria1 _
    :="Released to Customers"
'Filer by Type:
ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=13, Criteria1 _
    :=Array("full_trial", "renewal", "sale", "trial_to_sale"), Operator:= _
    xlFilterValues

ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=15, Criteria1 _
    :=Array("academic", "public_library", "schools"), 
    Operator:=xlFilterValues

ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=16, Criteria1 _
    :=Array("Australia", "Canada", "Finland", "Germany", "Italy", 
"Netherlands", _
    "New Zealand", "United Kingdom (GB)", "United States", "Nordics", 
"Norway", "Sweden", "Finland", "Denmark"), Operator:= _
    xlFilterValues
End Sub

我希望宏可以将设置更改为我选择的任何设置,因为它使用了自动筛选功能,但事实并非如此,并且出现了超出范围的错误。

1 个答案:

答案 0 :(得分:1)

您可以尝试在每次过滤之间插入此行吗?

Activesheet.AutoFilterMode = False

就是这样:

Activesheet.AutoFilterMode = False
ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=27, Operator:= _
    xlFilterValues, Criteria2:=Array(0, "6/3/2019")


Acivesheet.AutoFilterMode = False
ActiveSheet.ListObjects("Stories").Range.AutoFilter Field:=10, Criteria1 _
    :="Released to Customers"
.......