VBA - Loop Through Multiple Worksheets and Apply Filter

前端 未结 2 861
慢半拍i
慢半拍i 2020-12-21 07:30

I need to apply the same filter to all the worksheets in the same workbook.

All the sheets have headers in the first row, but the header name that the filter is supp

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-21 08:16

    Basically what you need to do is reference the sheet in your code as you loop through, which you are not doing - you are only referring to the active sheet by not including any references.

    With Match you can use wildcards.

    Sub WorksheetLoop()
    
    Dim WS_Count As Long
    Dim I As Long
    
    WS_Count = ActiveWorkbook.Worksheets.count
    
    Dim count As Variant, rngData As Range
    For I = 1 To WS_Count
       Set rngData = Worksheets(I).Range("A1").CurrentRegion
       count = Application.Match("STATUS", Worksheets(I).Range("A1:AZ1"), 0)
       If IsNumeric(count) Then rngData.AutoFilter Field:=count, Criteria1:="INACTIVE"
    Next I
    
    End Sub
    

提交回复
热议问题