1

I have the below VBA, which loops through all the named tabs in the array() formula. I want that to be a dynamic range that, instead of having the values typed into the array(), it references a range in a worksheet that has those values, and it dynamic when I add more

Sub Filter_To_Send()

Dim WshtNames As Variant
Dim WshtNameCrnt As Variant

WshtNames = Array("RASC", "RSUV", "SBKP", "RGRN", "SBTK", "RWAS", "RABT", "RHAY", "RLEX", "RARM", "RBCK", "RBER", "RCCP", "RDAL", "RHEB", "RSST", "RMST", "RMIA", "REHV", "RNBY", "RNOF", "RUTC", "RSCS", "RSJO", "RSCQ", "RSHA", "RAWP", "SART", "SASK", "SALC", "SNHQ", "SOEX", "SPHL", "SSHP", "SMRN")

For Each WshtNameCrnt In WshtNames
With Worksheets(WshtNameCrnt)
    .Range("A5").AutoFilter field:=16, Criteria1:="<>0"
End With
Next WshtNameCrnt

End Sub

Thanks!

1 Answer 1

1

If the list of sheet names is stored on the worksheet "Config" beginning at A1:

Sub Filter_To_Send()

    Dim c As Variant
    Dim rngSheets As Range

    With Sheets("Config")
        Set rngSheets = .Range(.Range("A1"), .Cells(.Rows.Count, 1).End(xlUp))
    End with


    For Each c In rngSheets.Cells
        With Worksheets(c.Value)
            .Range("A5").AutoFilter field:=16, Criteria1:="<>0"
        End With
    Next c

End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

@tim.williams -- thanks so much. I get run time error '9' - subscript out of range, when it gets to the "With Worksheets(c.Value)" line.
That would indicate there's no worksheet with that name in the active workbook. Does your list of worksheets have a header? If Yes then change that .Range("A1") to .Range("A2")
Got it. I tried to add the list of tabs to an existing tab and move it from A1 to where the list started. But, sounds like it's easier to have its own dedicated tab. Thanks so much!!
It can be anywhere - as long as it's in its own column with nothing below it, so the code can find the last populated value using End(xlUp)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.