循环代码失败

时间:2014-03-17 23:39:17

标签: excel vba loops worksheet

我想在我的所有工作表中运行以下代码。我有大约20张。我错过了什么?

Sub Filter1()
    Dim wSheet As Worksheet
    Dim i As Long

    For i = 0 To ActiveWorkbook.Worksheets.Count        
       ActiveSheet.Range("$Q$1:$Q$90").AutoFilter Field:=1, Criteria1:="<>"
       ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
       ActiveSheet.Select        
    Next i

End Sub

1 个答案:

答案 0 :(得分:2)

试试这个:

Sub Filter1()
    Dim i As Integer
    For i = 1 To ThisWorkbook.Worksheets.Count
       ThisWorkbook.Worksheets(i).Range("$Q$1:$Q$90").AutoFilter Field:=1, Criteria1:="<>"
       ThisWorkbook.Worksheets(i).Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
    Next i
End Sub