运行时错误“ 91”

时间:2018-07-18 21:02:02

标签: excel vba

我记录了一个带有小计的宏,当我运行它时,这就是我得到的错误。错误与第一行相关

ActiveWorkbook.Worksheets("Celine HB").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Celine HB").AutoFilter.Sort.SortFields.Add Key:= _
        Range("E1:E301"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    With ActiveWorkbook.Worksheets("Celine HB").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

1 个答案:

答案 0 :(得分:1)

当工作表上没有设置AutoFilter时,您将得到该错误。如果要测试过滤器是否存在,要处理该包装代码,此

If ActiveWorkbook.Worksheets("Celine HB").AutoFilterMode Then
    ' your code
Else
    'No Filter set, what now?
End if