Filtering data using a dictionary

Demirion

Board Regular
Joined
Sep 21, 2022
Messages
66
Platform
  1. Windows
Hi, I would like to create a macro that will filter the data, but the problem is, I don't know exactly what the data will be in the file. For this purpose, I used a dictionary that goes through a given column and saves the keys that I want to filter later. Unfortunately, I don't know how to get data from this dictionary. Below, the loop that goes through the given column. I added Debug.Print to check if the dictionary is working properly.
VBA Code:
For Each key In dictCity.Keys
Set oCity = dictCity(key)
With oCity
Debug.Print key
ws1.Range("A1").Autofilter Field:=1, Criteria1:=key, Operator:=xlFilterValues
End With
Next key
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I'm not sure how you get your dictionary keys because you've only shown a snippet of your code, however, this is how I would normally do it (using the name you've used for your dictionary).

VBA Code:
With ws1.Cells(1, 1).CurrentRegion
    .AutoFilter 1, Array(dictCity.keys), 7
End With
 
Upvote 0
Yes this is a code snippet. The dictionary works fine because single keys are displayed. It partially works because it goes over all the keys, but I still don't know how to copy the scope for one key.
 
Upvote 0
Suppose I have three key cities in my dictionary: London, Paris, Madrid. I want to copy a data range that will filter only for London, then only for Paris and finally for Madrid. I want to paste the copied data into my e-mail. I have to do it through the dictionary because I don't know what cities will be in the file.
 
Upvote 0
Something along these lines (you'll have to adapt it to suit your actual data - and insert your copy-to location)

VBA Code:
Option Explicit
Sub Loop_Through_Keys()
    Dim dictCity As Object, key As Variant, x, i As Long, ws As Worksheet
    Set ws = Sheet1
    Set dictCity = CreateObject("scripting.dictionary")
    x = Application.Transpose(Range("A2", Cells(Rows.Count, "A").End(xlUp)))
    
    For i = 1 To UBound(x, 1)   '<< fill the dictionary with unique values
        dictCity(x(i)) = 1
    Next

    For Each key In dictCity.keys   '<< begin looping through the keys
        With ws.Cells(1, 1).CurrentRegion
            .AutoFilter 1, key, 7
            .Offset(1).Copy         '<< at this point put your copy-to location
        End With
    Next key
    
End Sub
 
Upvote 0
It works the same way as my first code.
VBA Code:
For Each key In dictCity.Keys
Set oCity = dictCity(key)
With oCity
Debug.Print key
ws1.Range("A1").Autofilter Field:=1, Criteria1:=key, Operator:=xlFilterValues
End With
Next key
It deletes some cities when copying, and I don't know what it depends on.

The line of code you entered works in a different way, but I don't know if it's correct.
VBA Code:
        With ws1.Cells(1, 1).CurrentRegion
.AutoFilter 1, Array(dictCity.keys), 7
End With
It copies all the cities, but one by one as they are on the sheet. I don't think that's what I mean. Now I am testing pasting on the sheet, but ultimately I want to copy these individual ranges to the content of individual e-mails. It is even possible?
 
Upvote 0
I'm just testing pasting data on a blank sheet and it looks like this. The keys are not ordered in any way, it looks like I just copied all the data and pasted it into a blank sheet. I don't know if this will work for what I want to do. These were just tests. Ultimately, I want to paste the filtered data into Outlook for each key separately.

It gets complicated. That's why I'm leaving the dictionary for now, I'll come back to it later. For now, I will filter this data as standard. The next step is that I want to paste this filtered data into Outlook as a table. Could you give me some tips on how to do this?
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,658
Latest member
GStorm

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