How to filter on 1 unique value at a time in VBA

Bassie

Board Regular
Joined
Jan 13, 2022
Messages
66
Office Version
  1. 2019
Platform
  1. Windows
Hey,

I have around 7-10 unique values in column A and I want to filter by every single one of them and do some copy pasting etc. I just cant seem to figure out how to filter on the first unique value, do some stuff, then unfilter and filter on the next unique value.

Any help would be greatly appriciated.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Bassie,

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim i As Long
    Dim rngCell As Range
    Dim cln As New Collection
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet with data. Change to suit if necessary.
    
    'Create an unique collection of entries in Col. A (starting at Row 2). Change to suit if necessary.
    For Each rngCell In wsSrc.Range("A2", wsSrc.Range("A" & Rows.Count).End(xlUp))
        If Len(rngCell) > 0 Then
            On Error Resume Next
                cln.Add CStr(rngCell), rngCell
            On Error GoTo 0
        End If
    Next rngCell
    
    'Loop through the unique collection filtering on each item
    For i = 1 To cln.Count
        wsSrc.Range("A1", wsSrc.Range("A" & Rows.Count).End(xlUp)).AutoFilter Field:=1, Criteria1:=cln(i), Operator:=xlFilterValues
        '//Do stuff with the filtered data here//
    Next i

    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Would something like this suffice?

VBA Code:
Sub ProcessUniqueValues()
  Dim a As Variant
  Dim i As Long
  
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    a = Evaluate("unique(" & .Address & ")")
    For i = 2 To UBound(a)
      .AutoFilter Field:=1, Criteria1:=a(i, 1)
      'Do your stuff here
    Next i
    .AutoFilter
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,745
Members
449,116
Latest member
alexlomt

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