Creating a dictionary of all the values from a column

sinz0000

New Member
Joined
Jul 6, 2018
Messages
12
there is going to be a data in A column of current sheet containing unique values. I want to add all these already existing unique values to a dictionary and use each of the unique value as a criteria for filtering in next sheet.

How to add all the values of A column to the dictionary? and how to refer to each of the value in it?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the MrExcel board!

See if this heads you in the right direction. Post back with more details if you need further help.

Rich (BB code):
Sub Test()
  Dim Dict As Object
  Dim Data As Variant, Ky As Variant, Itm As Variant
  
  Set Dict = CreateObject("Scripting.Dictionary")
  Data = Range("A2", Range("A" & Rows.Count).End(xlUp)).Value
  For Each Itm In Data
    Dict(Itm) = Empty
  Next Itm
  For Each Ky In Dict.Keys()
    'Do your filtering of each value here
  Next Ky
End Sub
 
Upvote 0
thanks for the reply.

Could you please tell what this code is doing?
Rich (BB code):
Dict(Itm) = Empty

Also i will share my full scenario now.

I have one sheet named "criteria", one temporary sheet will be added named as "temporary". This temporary sheet is to take out uniques from Criteria!A:A. Later from these two sheets processing will be done and third sheet will be added.
I want to apply filter in Criteria sheet where the criteria will be taken from the unique cells of "temporary" sheet.

My Original data in Criteria Sheet
<code>Module Process
A P1
A P2
A P3
B P5
B P7
C P4
C P6
C P8

What i want from vba into third new sheet

</code><code><code>Module Process
A P1 | P2 | P3
B P5 | P7 |
C P4 | P6 | P8</code>


For this So far what i have done is
Code:
Sub FilterTest1()


ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "temporary"

ActiveSheet.[A:A].Value = Worksheets("Criteria").[A:A].Value

Cells.RemoveDuplicates Columns:=Array(1)

<code>'for heading deletion</code>
Range("A1").EntireRow.Delete 

Dim B As Integer
B = Cells.CurrentRegion.Rows.Count

With Sheets("Temporary")
Selection.End(xlDown).Select
End With


Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

[COLOR=#006400]'need help from here[/COLOR]</code><code>[COLOR=#006400][/COLOR]  

End Sub

I have learned this much so far but i am still really new. So it would be really helpful if you could explain what a particular syntax in your additions is doing.

thanks a lot.

</code>
 
Upvote 0
Could you please tell what this code is doing?
Code:
Dict(Itm) = Empty
When you put something in a dictionary you must add a Key and an associated Item. That line puts every entry in the column as a Key and 'Empty' (nothing really) as the Item. The Item could just as easily have been something like 1 (ie Dict(Itm) = 1) since the way you want to use the dictionary only involves the Keys, not the Items.


What i want from vba into third new sheet

Module Process
A P1 | P2 | P3
B P5 | P7 |
C P4 | P6 | P8
It is not clear quite what that means.
- Is "P1 | P2 | P3" all in a single cell, or
- Does that mean P1 in one cell, P2 in the next cell to the right, P3 in the next cell to the right?

I doubt you will need to use RemoveDuplicates (as the dictionary will only allow each entry to occur once anyway), or a temporary worksheet. I will suggest a code when the above question is clarified.
 
Upvote 0
Oh so remove duplicates is already done while making the dictionary. great.

I want to have P1, P2, P3 in different cells sequentially one after another like you said in the second line of ur question.
 
Upvote 0
I want to have P1, P2, P3 in different cells sequentially one after another like you said in the second line of ur question.
Try this in a copy of your workbook.
Rich (BB code):
Sub CollateData()
  Dim Dict As Object
  Dim Data As Variant
  Dim i As Long
  
  'Set up the dictionary
  Set Dict = CreateObject("Scripting.Dictionary")
  
  'Read the data from cols A:B of sheet 'Criteria" into an array in memory
  'This gives faster processing than going back to the worksheet to read each cell
  With Sheets("Criteria")
    Data = .Range("A1", .Range("B" & .Rows.Count).End(xlUp)).Value
  End With
  
  'Put all the column A values into the dictionary.
  'They will only be added once and just append all the column B values with a separator
  For i = 1 To UBound(Data)
    Dict(Data(i, 1)) = Dict(Data(i, 1)) & Data(i, 2) & ";"
  Next i
  
  'Add the new sheet
  Sheets.Add After:=Sheets(Sheets.Count)
  
  With Sheets(Sheets.Count).Range("A1")
    'Put all the dictionary Keys (col A values) and items (appended col B values) into this sheet
    .Resize(Dict.Count, 2).Value = Application.Transpose(Array(Dict.Keys, Dict.Items))
    'Use Text to Columns to split the appended col B values into separate columns
    .CurrentRegion.Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True
  End With
End Sub
 
Last edited:
Upvote 0
Hi there is more processing that has come up on this.

Suppose the current data is like this:
ModuleProcess
AP1P2P3P3P4P5P6P7P8P9
BP10P11P12P13P14P15P16P17
CP20P21P22P23P24P25P26P27P28
and so on
I need to split 'Process values' in the 4 columns
ModuleProcess
AP1P2
P3P3
P4P5P6P7
P8P9
BP10P11P12P13
P14P15P16P17
CP20P21P22P23
P24P25P26P27
P28

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

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