VBA Dictionary greater than 1 pull items

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,176
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to learn dictionary. Somone gave me the code below. What I am trying to do is almost the same thing but instead of counting the item I would like to pull the items. Please see below. Thanks in advance!

IDLast NameFirst Name
1111aaaaaa11112
1111bbbbdd55552
2222cccccc
4444dddddd
5555eeeeee
5555ffffff
8888gggggg
9999hhhhhh

VBA Code:
Sub CountGreaterThenTwo()
Dim xcell As Range
Dim i As Long
Dim r As Range
Dim dic As Object
Set r = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
   For Each xcell In r
        If xcell.Value <> "" Then
            If Not dic.Exists(xcell.Value) Then
                Set dic(xcell.Value) = CreateObject("Scripting.Dictionary")
            End If
                dic(xcell.Value)(xcell.Offset(0, 1).Value & "|" & xcell.Offset(0, 2).Value) = Empty
            End If
    Next xcell
i = 1
    For Each k In dic.Keys
        If dic(k).Count > 1 Then
            i = i + 1
            Cells(i, 10).Value = k
            Cells(i, 11).Value = dic(k).Count
        End If
    Next k
End Sub

to this:

IDLast NameFirst Name
1111aaaaaa1111aaaa|aabbbb|bb
1111bbbbdd5555eeee|eeffff|ff
2222cccccc
4444dddddd
5555eeeeee
5555ffffff
8888gggggg
9999hhhhhh
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Check if the following approach helps you.

VBA Code:
Sub greater_than_1_pull_items()
  Dim a As Variant, b As Variant, c As Variant
  Dim dic As Object, ky As Variant
  Dim i As Long, j As Long, n As Long, y As Long, m As Long
 
  Set dic = CreateObject("Scripting.Dictionary")
  dic.CompareMode = 1
  a = Range("A2", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 1) + 1)
 
  'store in array b
  For i = 1 To UBound(a, 1)
    If Not dic.Exists(a(i, 1)) Then
      y = y + 1
      j = 2
      n = 1
      b(y, 1) = a(i, 1)
    Else
      y = Split(dic(a(i, 1)), "|")(0)   'counter row in output array
      j = Split(dic(a(i, 1)), "|")(1)   'counter column in output array
      n = Split(dic(a(i, 1)), "|")(2)   'counter items
      j = j + 1
      n = n + 1
    End If
    b(y, j) = a(i, 2) & "|" & a(i, 3)
    dic(a(i, 1)) = y & "|" & j & "|" & n
  Next
 
  'store in array c
  ReDim c(1 To y, 1 To UBound(b, 2))
  m = 0
  For Each ky In dic.keys
    If Split(dic(ky), "|")(2) > 1 Then
      m = m + 1
      For j = 1 To Split(dic(ky), "|")(1)
        c(m, j) = b(Split(dic(ky), "|")(0), j)
      Next
    End If
  Next
 
  'output array c
  Range("J2").Resize(y, UBound(b, 2)).Value = c
End Sub
 
Upvote 0
Yes that will do it!! Thank you DanteAmor I appreciate your help and guidance on this!
 
Upvote 0
If you are interested in a different (shorter) approach you could also try this.

VBA Code:
Sub greater_than_1_pull_items_v2()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("C" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
    d(a(i, 1)) = d(a(i, 1)) & ";" & a(i, 2) & "|" & a(i, 3)
  Next i
  With Range("J2:K2").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    .Columns(2).TextToColumns DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 9), Array(1, 1))
    .Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True, Space:=False, Other:=False
    On Error Resume Next
    Intersect(.Columns(3).SpecialCells(xlBlanks).EntireRow, .CurrentRegion).Delete Shift:=xlUp
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
Peter_SSs love it!! Thank you so much for your solution! I greatly appreciate it!
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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