Iteration in Autofilter using VBA

Ron_N

New Member
Joined
Aug 25, 2011
Messages
30
Hi,

Is there a way to iterate through the different selections of an autofilter via VBA? Is there any kind of code that would help me select each item in the autofilter one by one, so that i could apply some formulae to it.

Now i have the following code:

Range("A1").Select
Selection.autofilter
ActiveSheet.Range("$A$1:$W$12").autofilter Field:=4, Criteria1:= _
"Inv Copies"

a = ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).Count

b = a * 0.25
c = b + 1

Rows(Irow + c).Resize(ActiveSheet.Range("a1", ActiveSheet.Range("a1"). _
End(xlDown)).Count - c).Delete

ActiveSheet.Range("$A$1:$W$12").autofilter Field:=4, Criteria1:= _
"Closing Future"

a = ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).Count

b = a * 0.25
c = b + 1

Rows(Irow + c).Resize(ActiveSheet.Range("a1", ActiveSheet.Range("a1"). _
End(xlDown)).Count - c).Delete

---------------------------------------------------------
but i donot want to mention the criteria again and again. Moreover i may have a new list tomorrow where i may add/delete certain names. So i want a dynamic code which will select each item in the autofilter one by one and apply the formula.
i am sorry for my ignorance but i am very new to VBA.
Can anybody help me out. It is urgent!

thank you,
Ron_N
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
To make sure i understand:
you want it to go through all the dropdown options of field 4 in your autofiler and do this
Code:
b = a * 0.25
        c = b + 1
        
        Rows(Irow + c).Resize(ActiveSheet.Range("a1", ActiveSheet.Range("a1"). _
        End(xlDown)).Count - c).Delete
right?
 
Upvote 0
Hello..

Yes, you have got it right. I will be very glad if you can help me out with this.

Thank you
 
Upvote 0
Hi Ron

So you need a list (or array) of the distinct values in your D column? You can use the Dictionary object to generate this (among other ways) eg:

Code:
Dim dic As Object
Dim rngAllValues As Range, cell As Range
Dim arrUniqueValues As Variant
Dim i As Long

Set dic = CreateObject("Scripting.Dictionary")
Set rngAllValues = Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)  'this is your entire list of values in Column D
 
'iterate thru column D values, extracting unique entries:
For Each cell In rngAllValues
    If Not dic.exists(cell.Value) Then _
        dic.Add cell.Value, cell.Value
Next cell
'dic now holds all unique entries - get them into your array via:
arrUniqueValues = dic.Keys
'use the unique values:
For i = LBound(arrUniqueValues) To UBound(arrUniqueValues)
    'MsgBox arrUniqueValues(i)
    range(A1:G1).autofilter field:=4, criteria1:=arrUniqueValues(i)
Next i
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,738
Members
452,940
Latest member
Lawrenceiow

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