VBA "With" Statement

Viewed 51

I have multiple variables that I want to use in one with "with" statement instead of several. Is there a way to do this? I have the snippet below, and would like to use variables a,b,c,d,e, and f

       With a
            .AutoFilter 1, "0"
            With a
                .clearContents
                .ClearComments
            End With
       End With
1 Answers

Instead of a, b, c, d, e and f define an array and loop through

Dim Vars(5) As Variant 'choose your type instead of Variant

'fill your variables with content
Vars(0) = a
Vars(1) = b
' …

Dim Var As Variant 'keep this Variant
For Each Var in Vars
    With Var 
        .AutoFilter 1, "0"
        With Var 
            .clearContents
            .ClearComments
        End With
    End With
Next Var
Related