Loop through autofilter

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
145
Office Version
  1. 2019
Platform
  1. Windows
Hi
I have a table with autofilters. For column A I would like, in VBA, to choose each item one by one until all have been looped through. Is it possible? i.e. choose number one item in the autofilter-list "colA" (without knowing the name of the item), I then do some copy paste of those filtered values to another sheet, next number two on the autofilter-list is choosen and the corresponding filtered values are copied to another place and so on.

Any ideas?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,183
Hi

See if this helps:

Code:
Sub Sweden()
Dim r%, sh As Worksheet, lastrow%, lc%, i%
Set sh = Sheets("Sheet3")                                           ' source sheet with table at a1:d18
sh.Range("h1") = sh.Range("a1"): sh.Range("h2") = ""                ' the table need headers for advanced filtering
sh.Range("a1:a18").AdvancedFilter xlFilterCopy, sh.Range("h1:h2"), sh.Range("i1"), True ' list items at column I
sh.Activate
lastrow = Evaluate("=match(lookup(2,1/(i:i<>""""),i:i),i:i)")       ' end of column A items, at column I
Sheets("Sheet2").Activate                                           ' destination sheet
lc = Cells(1, Columns.Count).End(xlToLeft).Column
For r = 2 To lastrow                                                ' copy to destination
    sh.Range("h2") = sh.Range("i" & r)                              ' criterion
    sh.Range("a1:d18").AdvancedFilter 2, Range("Sheet3!Criteria"), Cells(1, lc + 5 * r), False
Next
End Sub
 

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
145
Office Version
  1. 2019
Platform
  1. Windows
Thank you for your efforts. I had to solve it another way but I will try your solution whenever I get the chance.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,377
Messages
5,836,914
Members
430,462
Latest member
djkiwi1

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
Top