Loop through autofilter

henrik2h

Board Regular
Joined
Aug 25, 2008
Messages
139
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?
 

Some videos you may like

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Worf

Well-known Member
Joined
Oct 30, 2011
Messages
4,096
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
139
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,333
Messages
5,601,008
Members
414,421
Latest member
tonybear1994

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