PDA

View Full Version : Solved: VBA: Assign Filter criteria using loops



chungtinhlak
12-19-2012, 02:10 PM
I use the code below to set criteria for my auto filter, this example on have 1 criteria on each field/column. If i have a lot of criteria for each column, can someone help me/show me how i can assign criteria with a for loop?

thanks
Sub test()
With Sheets("Sheet1")
.AutoFilterMode = False

With .Range("A1:B1")

.AutoFilter

.AutoFilter Field:=1, Criteria1:="Other"

.AutoFilter Field:=2, Criteria1:="John"
End With
End With
End Sub

werafa
12-19-2012, 04:02 PM
how about


for x = 1 to n
myCriteria = myarray(x)
.autofilter field:=x, criteria1:= mycriteria
next x

(This is a guess, but the logic works with filters in a pivot table)

chungtinhlak
12-20-2012, 10:52 AM
thanks, worked!