VBA Advanced filtering and applying some actions on filtered data

ineedmesome

New Member
Joined
Dec 28, 2017
Messages
5
Hi guys,
Here's the type of data i'm talking about :
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Client[/TD]
[TD]Invoice[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]F17XXX[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]F18XXX[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]F18XXX[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]F17XXX[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]F18XXX[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD]F18XXX[/TD]
[/TR]
[TR]
[TD]E[/TD]
[TD]F17XXX[/TD]
[/TR]
</tbody>[/TABLE]

So i know how to use advanced filtering to have an output range that looks like this :
[TABLE="width: 500"]
<tbody>[TR]
[TD]A[/TD]
[/TR]
[TR]
[TD]B[/TD]
[/TR]
[TR]
[TD]C[/TD]
[/TR]
[TR]
[TD]D[/TD]
[/TR]
</tbody>[/TABLE]

But what i actually nead, is having an 'internal' list in my code. Something like that : mylist = "A,B,C,D". The idea is that i wanna perform a filter on my data based on this list.
Like this
Code:
.Range("A1","B8").AutoFilter field:=7, Criteria1:="[COLOR=#ff0000]Element of my list[/COLOR]"

So the data will become :
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Client[/TD]
[TD]Invoice[/TD]
[/TR]
[TR]
[TD]Element of my list[/TD]
[TD]F17XXX[/TD]
[/TR]
[TR]
[TD]Element of my list[/TD]
[TD]F17XXX[/TD]
[/TR]
</tbody>[/TABLE]

And then i can perform some data processing with conditions about the Invoice... But first i just wanna perform this first step.
Maybe it's easier to just perform an advanced filter with VBA and actually outputting the filter in a range and then looping through this range and delete it at the end ? Because i don't want any change on the actual sheet.

Thanks guys in advance for your answers!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can use the Dictionary object to create a unique list of clients. Then you can loop through each element/key of the dictionary like this...

Code:
Sub test()

    Dim dicClients As Object
    Dim vClients As Variant
    Dim sClient As String
    Dim i As Long
    
    Set dicClients = CreateObject("Scripting.Dictionary")
    dicClients.CompareMode = 1 'vbTextCompare
    
    vClients = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    For i = LBound(vClients) To UBound(vClients)
        sClient = vClients(i, 1)
        If Not dicClients.Exists(sClient) Then
            dicClients(sClient) = ""
        End If
    Next i
    
    For i = 0 To dicClients.Count - 1
        Debug.Print dicClients.Keys()(i)
    Next i
    
    Set dicClients = Nothing
    
End Sub

Hope this helps!
 
Upvote 0
You can use the Dictionary object to create a unique list of clients. Then you can loop through each element/key of the dictionary like this...

Code:
Sub test()

    Dim dicClients As Object
    Dim vClients As Variant
    Dim sClient As String
    Dim i As Long
    
    Set dicClients = CreateObject("Scripting.Dictionary")
    dicClients.CompareMode = 1 'vbTextCompare
    
    vClients = Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
    
    For i = LBound(vClients) To UBound(vClients)
        sClient = vClients(i, 1)
        If Not dicClients.Exists(sClient) Then
            dicClients(sClient) = ""
        End If
    Next i
    
    For i = 0 To dicClients.Count - 1
        Debug.Print dicClients.Keys()(i)
    Next i
    
    Set dicClients = Nothing
    
End Sub

Hope this helps!

Thanks sir! Yeah this definitely helped!
 
Upvote 0

Forum statistics

Threads
1,225,473
Messages
6,185,189
Members
453,281
Latest member
shantor

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