Dictionary object - Storing Multiple Values in One Key

Mike756

New Member
Joined
Apr 10, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi!

I use the code below to store ID's and the corresponding email addresses. Now I need to add a name too and I realized that the Dictionary object can only have one value. I tried adapting my code using a Class Module, but my VBA knowledge is weak. Could someone please help me out a little?

Account #EmailName
123​
foo@bar.coFoo Bar

VBA Code:
  Dim wb As Workbook, ws As Worksheet
    Dim iLastRow As Long, i As Long
    Dim dictID As Object, ID, addr As String

    Set dictID = CreateObject("Scripting.Dictionary")
 
    ' get list of IDS
    Set wb = ThisWorkbook
    Set ws = wb.Sheets(WS_ID)
    iLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To iLastRow
        ID = Trim(ws.Cells(i, "A"))
        addr = Trim(ws.Cells(i, "B"))
        If dictID.exists(ID) Then
            MsgBox ID & " is duplicated", vbCritical, "Duplicate ID"
            Exit Sub
        ElseIf InStr(1, addr, "@") > 0 Then
            dictID.Add ID, addr
        End If
    Next

Thank you very much!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi @Mike756. Thanks for posting on MrExcel board.

You can add several concatenated items to the same dictionary key.
In the following code I show you how to add the email and the name concatenated and separated by "|".
In the next part of the code I show you how to read the data from the dictionary.

VBA Code:
Sub id_email()
  Dim wb As Workbook, ws As Worksheet
  Dim iLastRow As Long, i As Long
  Dim dictID As Object
  Dim sID As String, sData As String, sEmail As String, sName As String
  Dim ky As Variant
  
  Set dictID = CreateObject("Scripting.Dictionary")
  
  ' get list of IDS
  Set wb = ThisWorkbook
  Set ws = wb.Sheets("WS_ID")
  iLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  For i = 2 To iLastRow
    sID = Trim(ws.Cells(i, "A"))
    sData = Trim(ws.Cells(i, "B").Value) & "|" & Trim(ws.Cells(i, "C").Value)
    If dictID.exists(sID) Then
      MsgBox sID & " is duplicated", vbCritical, "Duplicate ID"
      Exit Sub
    ElseIf InStr(1, sData, "@") > 0 Then
      dictID.Add sID, sData
    End If
  Next
  '
  '...
  'To read the data from the dictionary:
  For Each ky In dictID.keys
    sID = ky
    sEmail = Split(dictID(ky), "|")(0)
    sName = Split(dictID(ky), "|")(1)
    MsgBox "Id: " & sID & vbCr & _
           "Email: " & sEmail & vbCr & _
           "Name: " & sName
  Next
End Sub

If you want to use the class check the following example is very similar:



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Upvote 0
Solution
Although the dictionary key can contain only one item, the item can also be an array, list or even another dictionary.
 
Upvote 0
Yes like

dictID.Add sID, array("email","name")
 
Upvote 0
Hi @Mike756. Thanks for posting on MrExcel board.

You can add several concatenated items to the same dictionary key.
In the following code I show you how to add the email and the name concatenated and separated by "|".
In the next part of the code I show you how to read the data from the dictionary.

VBA Code:
Sub id_email()
  Dim wb As Workbook, ws As Worksheet
  Dim iLastRow As Long, i As Long
  Dim dictID As Object
  Dim sID As String, sData As String, sEmail As String, sName As String
  Dim ky As Variant
 
  Set dictID = CreateObject("Scripting.Dictionary")
 
  ' get list of IDS
  Set wb = ThisWorkbook
  Set ws = wb.Sheets("WS_ID")
  iLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  For i = 2 To iLastRow
    sID = Trim(ws.Cells(i, "A"))
    sData = Trim(ws.Cells(i, "B").Value) & "|" & Trim(ws.Cells(i, "C").Value)
    If dictID.exists(sID) Then
      MsgBox sID & " is duplicated", vbCritical, "Duplicate ID"
      Exit Sub
    ElseIf InStr(1, sData, "@") > 0 Then
      dictID.Add sID, sData
    End If
  Next
  '
  '...
  'To read the data from the dictionary:
  For Each ky In dictID.keys
    sID = ky
    sEmail = Split(dictID(ky), "|")(0)
    sName = Split(dictID(ky), "|")(1)
    MsgBox "Id: " & sID & vbCr & _
           "Email: " & sEmail & vbCr & _
           "Name: " & sName
  Next
End Sub

If you want to use the class check the following example is very similar:



--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
Thank you Sir!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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