3

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 supposed to be applied to is not always in the same column, i.e, worksheet one the header in question is in column H, in worksheet 2 it's in column A, in worksheet 3 it's in column L and so on...

Also, some of the headers will not be an exact match for the criteria - i.e, some will have "STATUS" as the header, some will be "prefix_Status", others "CurrentStatus" and so on.. So I need to use the Instr funciton (Unless there's some better option) but I cannot seem to figure out where or how to use it..

Here is the code I have so far:

Sub WorksheetLoop()

         Dim WS_Count As Integer
         Dim I As Integer

         ' Set WS_Count equal to the number of worksheets in the active
         ' workbook.
         WS_Count = ActiveWorkbook.Worksheets.count

         ' Begin the loop.
         For I = 1 To WS_Count

            Dim count As Integer, rngData As Range
            Set rngData = Range("A1").CurrentRegion

            count = Application.WorksheetFunction.Match("STATUS", Range("A1:AZ1"), 0)

            rngData.autofilter Field:=count, Criteria1:="INACTIVE"

         Next I

End Sub

This code applies the filter to only ONE sheet.

Eitel Dagnin
  • 959
  • 4
  • 24
  • 61

2 Answers2

4

You are referring always to the ActiveSheet, whenever you do not specify the worksheet explicitly in your code. Thus, in the Range() you have to refer to the worksheet like this:

From:

Set rngData = Range("A1").CurrentRegion
count = Application.WorksheetFunction.Match("STATUS", Range("A1:AZ1"), 0)

Change to:

With Worksheets(I)
    Set rngData = .Range("A1").CurrentRegion
    count = Application.WorksheetFunction.Match("STATUS", .Range("A1:AZ1"), 0)
End With

The dots in the code between With Worksheets(I) - End With are what makes the difference:

enter image description here


Concerning the Application.WorksheetFunction.Match, it only matches cells which contain exactly the word "STATUS". If there is something else like a space before or a sign after, then something like this is a good idea:

count = Application.Match("*STATUS*", Worksheets(1).Range("A1:AZ1"), 0)

Then a check is still needed. Like this:

If Not IsError(count) Then
    rngData.autofilter Field:=count, Criteria1:="INACTIVE"
End If

Concerning the second part of the question, use * around the value in the Match function:

Public Sub TestMe()    
    Range("E1") = "5teSt34"
    Debug.Print Application.WorksheetFunction.Match("*Test*", Range("A1:H1"), 0)    
End Sub

Will always return 5.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Hi Vitata, thank you for the reply.. I included the "with" statement but it's still only applying to the sheet that I execute the macro on.. – Eitel Dagnin Mar 28 '18 at 10:41
  • @EitelDagnin - are you sure? Can you save the excel file, exit, open it and try again? Have you see the `.` before the `Range` in the code? There are 2 of them and they are quite important. – Vityata Mar 28 '18 at 10:43
  • Sorry I deleted my last comment because I didn't see you updated the post... I do apologize, I didn't notice the dots in the code above, I simply added the "with" statement. I put the dots there and it works perfectly but when it gets to the 4th sheet, I get the Error Message: "Unable to get the Match property of the WoeksheetFunction class". In my last comment below to SJR, this is the SAME sheet that SJR's code seems to "skip". Any suggestions? – Eitel Dagnin Mar 28 '18 at 10:54
  • 1
    @EitelDagnin - it skips the worksheet, because the `count` returns an error, as far as the "STATUS" is not found in it. Probably it is written `"STATUS "` with a space after or before or anything. – Vityata Mar 28 '18 at 10:58
  • I checked what possible spaces in the word and there were extra spaces.. I removed the spaces and it still didn't apply the filter correctly. I then searched for all blank rows and removed them and then it worked 100%. – Eitel Dagnin Mar 28 '18 at 12:16
  • @EitelDagnin - congrats! Now you have a working code :) – Vityata Mar 28 '18 at 12:17
  • 1
    Thank you for the help Vityata :) – Eitel Dagnin Mar 28 '18 at 12:21
  • Vityata, can you perhaps assist with the second part of the question regarding the Instr? – Eitel Dagnin Mar 28 '18 at 12:44
  • I made the changes and I am getting an error in the Match line - Type Mismatch – Eitel Dagnin Mar 28 '18 at 13:46
  • @EitelDagnin - it is probably something small, but it would be probably a better idea to ask a new question, so more people can take a look at it. Make sure to include input, expected output and exact error. – Vityata Mar 28 '18 at 13:51
  • I will do so - Thank you Vityata for your help :) – Eitel Dagnin Mar 28 '18 at 13:53
2

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
SJR
  • 22,986
  • 6
  • 18
  • 26
  • Hi SJR, thank you it seems to be half working... The first 3 worksheets the filter is applied and then I get the Error Message: "Unable to get the Match property of the WoeksheetFunction class". Currently there are 10 worksheets, but sometimes there might be less and sometimes there can be more.. – Eitel Dagnin Mar 28 '18 at 10:39
  • I've made a change above, can you try again please. – SJR Mar 28 '18 at 10:40
  • It is working fantastically! :) There's just ONE worksheet that it's not applying to. I have triple checked and the header is spelled correctly and the column does contact the correct search criteria. Other than that, the solution works great! Any suggestions for the one sheet? – Eitel Dagnin Mar 28 '18 at 10:48
  • Do you mean it doesn't apply the filter? Step through the code and check the value of `count`. – SJR Mar 28 '18 at 10:53
  • By the way did you add the wildcards into your match line? `"*STATUS*"`. – SJR Mar 28 '18 at 11:10
  • Sorry, no it didn't apply the filter and no I did not add the wildcards in the match line.. I did add them and it applied the filter but it didn't apply it correctly - i.e, it still showed other results. I checked what Vityata mentioned above about possible spaces in the word and there were extra spaces.. I removed the spaces and it still didn't apply the filter correctly. I then searched for all blank rows and removed them and then it worked 100%. I don't know why, but only this one sheet is giving issues. Will need to account for these issues I suppose. – Eitel Dagnin Mar 28 '18 at 12:15
  • Thank you for the help SJR :) – Eitel Dagnin Mar 28 '18 at 12:21
  • My pleasure. Perhaps you have some hidden characters. If you have spaces in the "INACTIVE" also you need to use wildcards for the filter as well as the match. – SJR Mar 28 '18 at 12:24
  • So if I understand correctly, wildcards account for variations of the text, as in lower case, upper case, more characters or less characters etc? – Eitel Dagnin Mar 28 '18 at 12:27
  • SJR, can you perhaps assist with the second part of the question regarding the Instr? – Eitel Dagnin Mar 28 '18 at 12:44