Excel VBA: Loop through autofilter problem

alantse2010

New Member
Joined
Jun 9, 2018
Messages
34
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
Hi all,
I want to loop through autofilter criteria in column G( below photo) and do something, such as copy or sum.
1686497713457.png

How can i loop each choice in VBA?

Thank you very much for your help.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I note you have 365 indicated on your account profile, so how about something like this?

VBA Code:
Option Explicit
Sub Test()
    Dim rng As Range, a, i As Long
    Set rng = Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
    
    a = Application.Sort(WorksheetFunction.Unique(rng))
    For i = LBound(a) To UBound(a)
    
        'Do something with each value here...
    
    Next i

End Sub
 
Upvote 1
Solution
I note you have 365 indicated on your account profile, so how about something like this?

VBA Code:
Option Explicit
Sub Test()
    Dim rng As Range, a, i As Long
    Set rng = Range("G2:G" & Cells(Rows.Count, "G").End(xlUp).Row)
   
    a = Application.Sort(WorksheetFunction.Unique(rng))
    For i = LBound(a) To UBound(a)
   
        'Do something with each value here...
   
    Next i

End Sub
Thank you very much for your reply, that i want something like this but it cause the error
"must be array"

how to fix it?
 
Upvote 0
It works for me, but I don't know how you're trying to use it. Could you post the full code you're attempting please?
 
Upvote 0
It works for me, but I don't know how you're trying to use it. Could you post the full code you're attempting please?
I try your code again, it works now.
I don't know why the previous will cause an error.
Anyway, thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,223,919
Messages
6,175,368
Members
452,638
Latest member
Oluwabukunmi

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