Autofilter in Visual Basic

swilkes

New Member
Joined
Apr 3, 2008
Messages
5
Using Excel 2007 and a novice VBA user.

I am trying to write some code that will access the Autofilter list. However, I would like the autofilter list to be arbitary so I will not really know what the criteria will be inside the quotation marks of the code below. Is there a simple solution in VBA for such a problem:

The current code lines may be something of the form:

Selection.AutoFilter
ActiveSheet.Range("$A$1:$E$21").AutoFilter Field:=2, Criteria1:= _
"Unknown Entry 1"
ActiveSheet.Range("$A$1:$E$21").AutoFilter Field:=2, Criteria1:= _
"Unknown Entry 2"
......[an so on....]

and where the Criteria text is different in each spreadsheet I would like to run the code on.

Any solutions or thoughts would be welcome.

Cheers
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Criteria will come form the same columns in each spreadsheet, but the criteria will change form spreadsheet to spreadsheet thats the difficulty I am having at the moment). I suspose I may need to define an array or something like that.

Cheers
 
Upvote 0
Untested. Try this:

Code:
Sub test()
Dim TheArray

'Create array
With ActiveSheet.Range("$A$1:$E$21")
    ReDim TheArray(1 To .Rows.Count, 1 To 1)
    TheArray = .Value2
End With

For i = LBound(TheArray, 1) To UBound(TheArray, 1)
    ActiveSheet.Range("A1").AutoFilter Field:=2, Criteria1:=TheArray(i, 1)
    
    'Do what you want here...Like paste data somewhere after autofilter
    
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,203,681
Messages
6,056,712
Members
444,886
Latest member
peishin98

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top