Filter by NEXT unique data value VBA

McMasher

Board Regular
Joined
Mar 8, 2013
Messages
54
I have a table with three columns: Style, Color, Size. What I want to be able to do is hit a "Next" button, and it will change the filter on the table to whatever the next unique Style is within this table.

My Example:

Full table:
StyleColorSize
ArdvarkianRedSmall
ArdvarkianRedMedium
BouncyBlueMedium
BouncyBlueLarge
BubblyPinkSmall
BubblyPinkMedium
ZymphBlack/WhiteLarge
ZymphBlack/WhiteX_Large

<tbody>
</tbody>

First Filter:
ArdvarkianRedSmall
ArdvarkianRedMedium

<tbody>
</tbody>

Then I would hit NEXT and would see this:
BouncyBlueMedium
BouncyBlueLarge

<tbody>
</tbody>

Then NEXT again:
BubblyPinkSmall
BubblyPinkMedium

<tbody>
</tbody>




Etc. etc.


Thanks in advance for all your help!!!

~McMasher
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
your table begins in A1?

try:

Code:
Private aUniques As Variant, Cntr As Long


Sub filter_NEXT()
If (Not ActiveSheet.AutoFilterMode) Or (IsEmpty(aUniques)) Then
    Cntr = 0
    With Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        aUniques = Filter(Evaluate(Replace("=TRANSPOSE(IF(MATCH(@,@,0)=ROW(@)-ROW(A2)+1,@,""###""))", "@", .Address)), "###", 0)
    End With
    Range("A1").CurrentRegion.AutoFilter 1, aUniques(0)
Else
    Cntr = 1 + Cntr
    Range("A1").CurrentRegion.AutoFilter 1, aUniques((Cntr Mod (1 + UBound(aUniques))))
End If
End Sub
 
Upvote 0
VBA Geek,

Thank you for your impressive code...It didn't actually work for me, HOWEVER, your formula with the loop counter
Code:
Cntr = 1 + Cntr
Made me think of a formula I could use in an added column that would give me a numerical sequence to my style changes.

Here is the formula I am using:

Code:
=IF($AG23="Unique Value Count", 1,IF($B24<>$B23,AG23+1,$AG23))

Where Unique Value count is the column header. Basically I knew my Styles were sorted alphabetically so I don't need to worry that a style will show up in row 25 and then again in line 500 for example.

I'm going to use this column along with the following code:

Code:
Sub NextItem()

Range("B5").Value = Range("B5").Value + 1
ActiveWorkbook.Worksheets("Item Qty by Whs DB").ListObjects("tblItemQty").Range.AutoFilter Field:=33, Criteria1:="=" & Range("B5")

End Sub

Thanks again!!!

~McMasher
 
Upvote 0
Hello, thanks for feedback.

by looking at your formulas, I am guessing my solution did not work as your table mustn't be located in columns A:C, starting in A1

if you let me know where does your table begin I can update the code



VBA Geek,

Thank you for your impressive code...It didn't actually work for me, HOWEVER, your formula with the loop counter
Code:
Cntr = 1 + Cntr
Made me think of a formula I could use in an added column that would give me a numerical sequence to my style changes.

Here is the formula I am using:

Code:
=IF($AG23="Unique Value Count", 1,IF($B24<>$B23,AG23+1,$AG23))

Where Unique Value count is the column header. Basically I knew my Styles were sorted alphabetically so I don't need to worry that a style will show up in row 25 and then again in line 500 for example.

I'm going to use this column along with the following code:

Code:
Sub NextItem()

Range("B5").Value = Range("B5").Value + 1
ActiveWorkbook.Worksheets("Item Qty by Whs DB").ListObjects("tblItemQty").Range.AutoFilter Field:=33, Criteria1:="=" & Range("B5")

End Sub

Thanks again!!!

~McMasher
 
Upvote 0

Forum statistics

Threads
1,216,070
Messages
6,128,615
Members
449,460
Latest member
jgharbawi

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