Loop filter multiple criteria different columns

amitcohen

Board Regular
Joined
Jan 14, 2010
Messages
118
Hello Team

I'm struggling with this one..

Step 1) Filter MAIN Category (On field 20)
Step 2) Then Filter SUB Category (On field 4)
Step 3) Do the magic
Step 4) Goto the next MAIN Category
Step 5) Filter SUB Category
Step 6) Do the magic again

Code:
Sub Filter_On_2_Columns_BB()
'This Macro Filter MAIN Category, Creat SUB Category list on AA Column, Then filter by the SUB Category
'On Error Resume Next
Range("X1").Value = "=COUNTA(V:V)+1"
Range("Y1").Value = "=COUNTA(C:C)"
Range("Z1").Value = "=COUNTA(AA:AA)"
Range("AB1").Value = "=SUBTOTAL(3,C:C)"


'Creat MAIN Category Column on V:V
    Columns("T:T").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Range("V1").Select
    ActiveSheet.Paste
'Remove Duplicated MAIN Categories From T:T onto 'V:V Column'
    Range(ActiveCell, ActiveCell.End(xlDown)).Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
    Range("V1").Select
    Selection.ClearContents
    

    Range("A1:T1").Select
    Selection.AutoFilter

'Filter MAIN Category
For iMainCategory = 2 To Range("X1").Value
ActiveSheet.Range("A1:T" & Range("Y1").Value).AutoFilter Field:=20, Criteria1:=Range("V" & iMainCategory).Value
'Creat SUB Category on Column AA:AA
    Columns("D:D").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Range("AA1").Select
    ActiveSheet.Paste
'Remove Duplicated SUB Categories From T:T onto 'V:V Column'
    Range(ActiveCell, ActiveCell.End(xlDown)).Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlNo
    Range("AA1").Select
    Selection.ClearContents
    

'Filter SUB Category
For iSubCategory = 2 To (Range("Z1").Value + 1)
ActiveSheet.Range("A1:T" & Range("Y1").Value).AutoFilter Field:=4, Criteria1:=Range("AA" & iSubCategory).Value

'Do the Magic
'Based on the filter, now select the 1st VALUE in 'Column C' and copy it
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 3).Select
Selection.Copy
'After copy the 1st value on Column C, Now Goto the 'Parent' Column (A:A) and select the range available in the filter and paste the value to all cells in the range.
    Range("A2:A" & Range("Y1").Value).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
'Paste Values
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
'Remove the value of the 1st product on Column A
ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
Selection.ClearContents


Next iSubCategory
Next iMainCategory


End Sub


While it's running Ok on the first MAIN Category,
It fails to continue to the next one.


Can you help me spot the problem?

Many thanks,
Amit.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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