VBA Dictionary Unique Items

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,168
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good morning, I need some guidance on how to make a dictionary produce unique Items based on a key. Please see below:

NameNumberNameNumber
AL1AL1, 1, 7
AL1Barry5
AL7Bill1, 1, 6, 6
Barry5Ken3
Bill1Paul4
Bill1Sal6, 12, 13
Bill6Sam3, 3, 4
Bill6
Ken3
Paul4
Sal6
Sal12
Sal13
Sam3
Sam3
Sam4


This is what I am looking for:

NameNumberNameNumber
AL1AL1, 7
AL1Barry5
AL7Bill1, 6
Barry5Ken3
Bill1Paul4
Bill1Sal6, 12, 13
Bill6Sam3, 4
Bill6
Ken3
Paul4
Sal6
Sal12
Sal13
Sam3
Sam3
Sam4


This is the Code that I am using:

VBA Code:
Sub DicUniqueItem()
Dim dic As New Dictionary
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each xcell In Range("A1:A" & lastrow)
        If Not dic.Exists(xcell.Value) Then
            dic.Add xcell.Value, xcell.Offset(0, 1).Value
        Else
            dic.Item(xcell.Value) = dic.Item(xcell.Value) & ", " & xcell.Offset(0, 1).Value
        End If
    Next
    Dim k
    Dim i As Long
    i = 0
    For Each k In dic.Keys
        i = i + 1
        Cells(i, 7).Value = k
        Cells(i, 8).Value = dic(k)
    Next
End Sub

Thanks in advance
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
VBA Code:
Sub DicUniqueItem()
Dim dic As New Dictionary
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    For Each xCell In Range("A1:A" & lastrow)
        If Not dic.Exists(xCell.Value) Then
            dic.Add xCell.Value, xCell.Offset(0, 1).Value
        ElseIf InStr(1, " " & dic(xCell.Value) & ",", " " & xCell.Offset(, 1).Value & ",") = 0 Then
            dic.Item(xCell.Value) = dic.Item(xCell.Value) & ", " & xCell.Offset(0, 1).Value
        End If
    Next
   Cells(1, 7).Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.Keys, dic.Items))
End Sub
 
Upvote 0
Solution
Sub DicUniqueItem() Dim dic As New Dictionary Dim lastrow As Long lastrow = Cells(Rows.Count, "A").End(xlUp).Row For Each xCell In Range("A1:A" & lastrow) If Not dic.Exists(xCell.Value) Then dic.Add xCell.Value, xCell.Offset(0, 1).Value ElseIf InStr(1, " " & dic(xCell.Value) & ",", " " & xCell.Offset(, 1).Value & ",") = 0 Then dic.Item(xCell.Value) = dic.Item(xCell.Value) & ", " & xCell.Offset(0, 1).Value End If Next Cells(1, 7).Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.Keys, dic.Items)) End Sub
Thanks Fluff! I really appreciate your help and guidance. That worked perfectly!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Hi, I need help on a macro I'm doing.

I need it to open several .csv files in thisworkbook.path.

A moment ago it was working and now I'm getting an error.

Can you please help me?

Thanks in advance!

1622897176457.png
 

Attachments

  • 1622897096387.png
    1622897096387.png
    6.7 KB · Views: 2
Upvote 0
As this is a totally different question, you need to start a thread of your own. Thanks
 
Upvote 0
@Stephen_IV
A couple of comments/suggestions.

Perhaps there is a particular need for vba but since you have Excel 365 it can easily be done by formula.

Stephen_IV.xlsm
ABCDEFGH
1NameNumberNameNumber
2AL1AL1, 7
3AL1Barry5
4AL7Bill1, 6
5Barry5Ken3
6Bill1Paul4
7Bill1Sal6, 12, 13
8Bill6Sam3, 4
9Bill6 
10Ken3 
11Paul4 
12Sal6
13Sal12
14Sal13
15Sam3
16Sam3
17Sam4
Sheet1
Cell Formulas
RangeFormula
G2:G8G2=UNIQUE(A2:A17)
H2:H11H2=TEXTJOIN(", ",1,UNIQUE(FILTER(B$2:B$17,A$2:A$17=G2,"")))
Dynamic array formulas.



If vba is required then since you are using a dictionary to get the unique names, you can also use the dictionary idea to get unique numbers.
It may not be an issue with your data but this can also be a speed issue as continually joining strings in the dictionary items can get sluggish if the strings get at all long.
In any case, if you are interested, here is a method using a 'dictionary of dictionaries' that I believe does the same job.

VBA Code:
Sub DicUniqueItem_v2()
  Dim dic As New Dictionary
  Dim i As Long
  Dim a As Variant, ky As Variant

  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value2
  For i = 1 To UBound(a)
    If Not dic.Exists(a(i, 1)) Then dic.Add (a(i, 1)), CreateObject("Scripting.Dictionary")
    dic(a(i, 1))(a(i, 2)) = Empty
  Next i
  For Each ky In dic.Keys
    dic.Item(ky) = Join(dic.Item(ky).Keys, ", ")
  Next ky
  Cells(1, 7).Resize(dic.Count, 2).Value = Application.Transpose(Array(dic.Keys, dic.Items))
End Sub
 
Upvote 0
Thanks Peter_SSs I am always interested in learning. I originally thought of a double dictionary but learned something new from Fluff. I appreciate your help and assistance!!!
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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