You are on page 1of 56

2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Member Area  VBA Articles Courses

Webinars About

Excel VBA Dictionary – A Complete Guide


by Paul Kelly | | Data Structures VBA, Most Popular | 228 comments

“The greatest masterpiece in literature is only a dictionary out of order.” – Jean


Cocteau

Contents [hide]

1 A Quick Guide to the VBA Dictionary


2 What is the VBA Dictionary?
3 Download the Source Code
4 Dictionary Webinar
5 A Dictionary in real world terms
6 A Simple Example of using the VBA Dictionary
7 Creating a Dictionary
7.1 Early versus Late Binding
8 Adding Items to the Dictionary
9 Assigning a Value
10 Checking if a Key Exists
11 Storing Multiple Values in One Key
12 Other useful functions
13 The Key and Case Sensitivity
We use cookies13.1 Things
to ensure towe
that Watch
give Out Forbest experience on our website. If you continue to use this site we will
you the
assume that you are happy with it.
14 Reading through the Dictionary
15 Sorting the Dictionary Ok

https://excelmacromastery.com/vba-dictionary/ 1/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

15.1 Sorting by keys


15.2 Sorting by values
16 Troubleshooting the Dictionary
16.1 Missing Reference
16.2 Exists is not Working
16.3 Object Variable Error
16.4 Useful Tips for Troubleshooting the Dictionary
17 Copying the Dictionary to an Array
18 Writing the Dictionary to the Worksheet
19 Useful Dictionary Examples
19.1 Example 1 – Summing Single Values
19.2 Example 2 – Dealing with Multiple Values
19.3 Example 3 – Summing Multiple Values
20 When To Use The Dictionary
21 What’s Next?

A Quick Guide to the VBA Dictionary


Function Example

Early binding reference “Microsoft Scripting Runtime”


(Add using Tools->References from the VB
menu)

Declare (early binding) Dim dict As Scripting.Dictionary

Create(early binding) Set dict = New Scripting.Dictionary

Declare (late binding) Dim dict As Object

Create(late binding) Set dict =


CreateObject("Scripting.Dictionary")

Add item (key must not dict.Add Key, Value


already exist) e.g. dict.Add "Apples", 50

Change value at key. dict(Key) = Value


Automatically adds if the key e.g. dict("Oranges") = 60
does not exist.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Get a value from the Value = dict(Key)
dictionary using the key OkappleCount = dict("Apples")
e.g.

https://excelmacromastery.com/vba-dictionary/ 2/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Function Example

Check if key exists dict.Exists(Key)


e.g. If dict.Exists("Apples") Then

Remove item dict.Remove Key


e.g. dict.Remove "Apples"

Remove all items dict.RemoveAll

Go through all items (for Dim key As Variant


each loop) For Each key In dict.Keys
Debug.Print key, dict(key)
Next key

Go through all items (for Dim i As Long


loop - early binding only) For i = 0 To dict.Count - 1
Debug.Print dict.Keys(i),
dict.Items(i)
Next i

Go through all items (for Dim i As Long


loop - early and late binding) For i = 0 To dict.Count - 1
Debug.Print dict.Keys()(i), dict.Items()(i)
Next i

Get the number of items dict.Count

Make key case sensitive (the dict.CompareMode = vbBinaryCompare


dictionary must be empty).

Make key non case sensitive dict.CompareMode = vbTextCompare


(the dictionary must be
empty).

What is the VBA Dictionary?


In VBA we use Arrays and Collections to store groups of values. For example, we could
use them to store a list of customer names, student marks or a list of values from a
range of cells.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
A Dictionary is similar to a Collection. Using both types, we can name an item when we
Ok

https://excelmacromastery.com/vba-dictionary/ 3/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

add it. Imagine we are storing the count of different fruit types.

We could use both a Collection and a Dictionary like this

' Add to Dictionary


dict.Add Key:="Apple", Item:=5

' Add to Collection


coll.Add Item:=5, Key:="Apple"

Example of Key, Value pairs

In both cases, we are storing the value 5 and giving it the name “Apple”. We can now get
the value of Apple from both types like this

' Get value from Dictionary


Total = dict("Apple")

' Get value from Collection


Total = coll("Apple")

So far so good. The Collection however, has two major faults

1. We cannot check if the key already exists.


2. We cannot change the value of an existing item.

We useThe first toissue


cookies is pretty
ensure that weeasy to get
give you the around: Check on
best experience Collection KeyIfexists.
our website. The second
you continue is site we will
to use this
more difficult. assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 4/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

The VBA Dictionary does not have these issues. You can check if a Key exists and you can
change the Item and the Key.

For example, we can use the following code to check if we have an item called Apple.

If dict.Exists("Apple") Then
dict("Apple") = 78

These may seem very simple differences. However, it means that the Dictionary is very
useful for certain tasks. Particularly when we need to retrieve the value of an item.

Download the Source Code

Download the Dictionary VBA


Code
Enter your email to get the Excel VBA code for this post plus
exclusive content not available on the website

Enter your email address here.

GET THE VBA CODE 

Dictionary Webinar
We useIfcookies
you aretoaensure
memberthat of
wethe
giveVBA Vault,
you the bestthen click on
experience onthe
our image
website.below
If you to accesstothe
continue use this site we will
webinar and the associated source code.
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 5/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

(Note: Website members have access to the full webinar archive.)

A Dictionary in real world terms


If you are still not clear about a Dictionary then think of it this way. A real-world
dictionary has a list of keys and items. The Keys are the words and the Items are the
definition.

When you want to find the definition of a word you go straight to that word. You don’t
read through every item in the Dictionary.

A second real world example is a phone book(remember those?). The Key in a phone
book is the name\address and the Item is the phone number. Again you use the
name\address combination to quickly find a phone number.

In Excel the VLookup function works in a similar way to a Dictionary. You look up an
item based on a unique value.

A Simple Example of using the VBA Dictionary


The code below give a simple but elegant example of using the Dictionary. It does the
following

We use1.cookies
Adds three fruitthat
to ensure types andyou
we give a value forexperience
the best each to a on
Dictionary.
our website. If you continue to use this site we will
assume that you are happy with it.
2. The user is asked to enter the name of a fruit.
Ok
3. The code checks if this fruit is in the Dictionary.
https://excelmacromastery.com/vba-dictionary/ 6/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

4. If yes then it displays the fruit name and the value.


5. If no then it informs the user the fruit does not exist.

' https://excelmacromastery.com/
Sub CheckFruit()

' Select Tools->References from the Visual Basic menu.


' Check box beside "Microsoft Scripting Runtime" in the list.
Dim dict As New Scripting.Dictionary

' Add to fruit to Dictionary


dict.Add key:="Apple", Item:=51
dict.Add key:="Peach", Item:=34
dict.Add key:="Plum", Item:=43

Dim sFruit As String


' Ask user to enter fruit
sFruit = InputBox("Please enter the name of a fruit")

If dict.Exists(sFruit) Then
MsgBox sFruit & " exists and has value " & dict(sFruit)
Else
MsgBox sFruit & " does not exist."
End If

Set dict = Nothing

End Sub

This is a simple example but it shows how useful a Dictionary is. We will see a real world
example later in the post. Let’s look at the basics of using a Dictionary.

Creating a Dictionary
To use the Dictionary you need to first add the reference.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
1. Select Tools->References from thethat
assume Visual
youBasic menu.
are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 7/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

2. Find Microsoft Scripting Runtime in the list and place a check in the box beside it.

We declare a dictionary as follows

Dim dict As New Scripting.Dictionary

or

Dim dict As Scripting.Dictionary


Set dict = New Scripting.Dictionary

Creating a Dictionary in this way is called “Early Binding”. There is also “Late Binding”.
Let’s have a look at what this means.

Early versus Late Binding


To create a Dictionary using Late binding we use the following code. We don’t need to
add a reference.

Dim dict As Object


Set dict = CreateObject("Scripting.Dictionary")

In technical terms Early binding means we decide exactly what we are using up front.
With Late binding this decision is made when the application is running. In simple terms
the difference is

1. Early binding requires a reference. Late binding doesn’t.


2. Early binding allows access to *Intellisense. Late binding doesn’t.
3. Early binding may require you to manually add the Reference to the “Microsoft
Scripting Runtime” for some users.

(*Intellisense is the feature that shows you the available procedures and properties of
an item as you are typing.)

While Microsoft recommends that you use early binding in almost all cases I would differ.
A good rule of thumb is to use early binding when developing the code so that you have
access to the Intellisense. Use late binding when distributing the code to other users to
prevent various library conflict errors occurring.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 8/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Adding Items to the Dictionary


Function Params Example

Add Key, Item dict.Add "Apples", 50

We can add items to the dictionary using the Add function. Items can also be added by
assigning a value which we will look at in the next section.

Let’s look at the Add function first. The Add function has two parameters: Key and Item.
Both must be supplied

dict.Add Key:="Orange", Item:=45


dict.Add "Apple", 66
dict.Add "12/12/2015", "John"
dict.Add 1, 45.56

In the first add example above we use the parameter names. You don’t have to do this
although it can be helpful when you are starting out.

The Key can be any data type. The Item can be any data type, an object, array, collection
or even a dictionary. So you could have a Dictionary of Dictionaries, Array and
Collections. But most of the time it will be a value(date, number or text).

If we add a Key that already exists in the Dictionary then we will get the error

The following code will give this error

dict.Add Key:="Orange", Item:=45

' This line gives an error as key exists already


We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
dict.Add Key:="Orange", Item:=75
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 9/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Assigning a Value
Operation Format Example

Assign Dictionary(Key) = Item dict("Oranges") = 60

We can change the value of a key using the following code

dict("Orange") = 75

Assigning a value to Key this way has an extra feature. If the Key does not exist it
automatically adds the Key and Item to the dictionary. This would be useful where you
had a list of sorted items and only wanted the last entry for each one.

' Adds Orange to the dictionary


dict("Orange") = 45

' Changes the value of Orange to 100


dict("Orange") = 100

Don’t forget that you can download all the VBA code used in this post from the top or
bottom of the post.

Checking if a Key Exists


Function Parameters Example

Exists Key If dict.Exists("Apples") Then

We can use the Exists function to check if a key exists in the dictionary

' Checks for the key 'Orange' in the dictionary


If dict.Exists("Orange") Then
MsgBox "The number of oranges is " & dict("Orange")
Else
MsgBox "There is no entry for Orange in the dictionary."
We use cookies
End Ifto ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 10/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Storing Multiple Values in One Key


Take a look at the sample data below. We want to store the Amount and Items for each
Customer ID.

The Dictionary only stores one value so what can we do?

We could use an array or collection as the value but this is unnecessary. The best way to
do it is to use a Class Module.

The following code shows how we can do this

' clsCustomer Class Module Code


Public CustomerID As String
Public Amount As Long
Public Items As Long

' Create a new clsCustomer object


Set oCust = New clsCustomer

' Set the values


oCust.CustomerID = rg.Cells(i, 1).Value
oCust.Amount = rg.Cells(i, 2).Value
oCust.Items = rg.Cells(i, 3).Value

' Add the new clsCustomer object to the dictionary


dict.Add oCust.CustomerID, oCust

You can see that by using the Class Module we can store as many fields as we want.
Examples 2 and 3 at the bottom of the post show how to use a class module with a
Dictionary

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will

Other useful functions


assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 11/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Function Parameters Example

Count N/A dict.Count

Remove Key dict.Remove "Apples"

RemoveAll N/A dict.RemoveAll

The three functions in the above table do the following:

1. Count – returns the number of items in the Dictionary.


2. Remove – removes a given key from the Dictionary.
3. RemoveAll – removes all items from the Dictionary

The following sub shows an example of how you would use these functions

' https://excelmacromastery.com/
Sub AddRemoveCount()

Dim dict As New Scripting.Dictionary

' Add some items


dict.Add "Orange", 55
dict.Add "Peach", 55
dict.Add "Plum", 55
Debug.Print "The number of items is " & dict.Count

' Remove one item


dict.Remove "Orange"
Debug.Print "The number of items is " & dict.Count

' Remove all items


dict.RemoveAll
Debug.Print "The number of items is " & dict.Count

End Sub

Remember that you can download all the code examples from the post. Just go to the
download section at the top.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 12/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

The Key and Case Sensitivity


Some of the string functions in VBA have a vbCompareMethod. This is used for functions
that compare strings. It is used to determine if the case of the letters matter.

© BigStockPhoto.com

The Dictionary uses a similar method. The CompareMode property of the Dictionary is
used to determine if the case of the key matters. The settings are

vbTextCompare: Upper and lower case are considered the same.

vbBinaryCompare: Upper and lower case are considered different. This is the default.

With the Dictionary we can use these settings to determine if the case of the key matters.

' https://excelmacromastery.com/
Sub CaseMatters()

Dim dict As New Scripting.Dictionary


dict.CompareMode = vbBinaryCompare
dict.Add "Orange", 1

' Prints False because it considers Orange and ORANGE different


We use cookies to ensure that we
Debug.Print give you the best experience on our website. If you continue to use this site we will
dict.Exists("ORANGE")
assume that you are happy with it.

Set dict = Nothing Ok

https://excelmacromastery.com/vba-dictionary/ 13/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

End Sub

This time we use vbTextCompare which means that the case does not matter

' https://excelmacromastery.com/
Sub CaseMattersNot()

Dim dict As New Scripting.Dictionary


dict.CompareMode = vbTextCompare
dict.Add "Orange", 1

' Prints true because it considers Orange and ORANGE the same
Debug.Print dict.Exists("ORANGE")

Set dict = Nothing

End Sub

Note: The Dictionary must be empty when you use the CompareMode property or you
will get the error: “Invalid procedure call or argument”.

Things to Watch Out For


vbBinaryCompare (the case matters) is the default and this can lead to subtle errors. For
example, imagine you have the following data in cells A1 to B2.

Orange, 5
orange, 12

The following code will create two keys – on for “Orange” and one for “orange”. This is
subtle as the only difference is the case of the first letter.

' https://excelmacromastery.com/
Sub DiffCase()

Dim dict As New Scripting.Dictionary

dict.Add
We use cookies Key:=(Range("A1")),
to ensure that Item:=Range("B1")
we give you the best experience on our website. If you continue to use this site we will
assume that youItem:=Range("B2")
dict.Add Key:=(Range("A2")), are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 14/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

End Sub

If you do use vbTextCompare for the same data you will get an error when you try to
add the second key as it considers “Orange” and “orange” the same.

' https://excelmacromastery.com/
Sub UseTextcompare()

Dim dict As New Scripting.Dictionary


dict.CompareMode = vbTextCompare

dict.Add Key:=(Range("A1")), Item:=Range("B1")


' This line will give an error as your are trying to add the same
dict.Add Key:=(Range("A2")), Item:=Range("B2")

End Sub

If you use the assign method then it does not take the CompareMode into account. So
the following code will still add two keys even though the CompareMode is set to
vbTextCompare.

' https://excelmacromastery.com/
Sub Assign()

Dim dict As New Scripting.Dictionary


dict.CompareMode = vbTextCompare

' Adds two keys


dict(Range("A1")) = Range("B1")
dict(Range("A2")) = Range("B2")

' Prints 2
Debug.Print dict.Count

End Sub

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Reading through the Dictionary
Ok

https://excelmacromastery.com/vba-dictionary/ 15/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

We can read through all the items in the Dictionary. We can go through the keys using a
For Each loop. We then use the current key to access an item.

Dim k As Variant
For Each k In dict.Keys
' Print key and value
Debug.Print k, dict(k)
Next

We can also loop through the keys although this only works with Early Binding(Update
Feb 2020: In Office 365 this now works with both versions):

Dim i As Long
For i = 0 To dict.Count - 1
Debug.Print dict.Keys(i), dict.Items(i)
Next i

This method works with both Early and Late binding:

Dim i As Long
For i = 0 To dict.Count - 1
Debug.Print dict.Keys()(i), dict.Items()(i)
Next i

Sorting the Dictionary


Sometimes you may wish to sort the Dictionary either by key or by value.

The Dictionary doesn’t have a sort function so you have to create your own. I have
written two sort functions – one for sorting by key and one for sorting by value.

Sorting by keys
To sort the dictionary by the key you can use the SortDictionaryByKey function below

' https://excelmacromastery.com/
Public Function SortDictionaryByKey(dict As Object _
We use cookies to ensure that we give you the bestsortorder
, Optional experience onAs
ourXlSortOrder
website. If you continue to use this siteAwe will
= xlAscending)
assume that you are happy with it.

Dim arrList As Object Ok

https://excelmacromastery.com/vba-dictionary/ 16/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Set arrList = CreateObject("System.Collections.ArrayList")

' Put keys in an ArrayList


Dim key As Variant, coll As New Collection
For Each key In dict
arrList.Add key
Next key

' Sort the keys


arrList.Sort

' For descending order, reverse


If sortorder = xlDescending Then
arrList.Reverse
End If

' Create new dictionary


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

' Read through the sorted keys and add to new dictionary
For Each key In arrList
dictNew.Add key, dict(key)
Next key

' Clean up
Set arrList = Nothing
Set dict = Nothing

' Return the new dictionary


Set SortDictionaryByKey = dictNew

End Function

The code below, shows you how to use SortDictionaryByKey

' https://excelmacromastery.com/
Sub TestSortByKey()
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Dim dict As Object
Ok
Set dict = CreateObject("Scripting.Dictionary")

https://excelmacromastery.com/vba-dictionary/ 17/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

dict.Add "Plum", 99
dict.Add "Apple", 987
dict.Add "Pear", 234
dict.Add "Banana", 560
dict.Add "Orange", 34

PrintDictionary "Original", dict

' Sort Ascending


Set dict = SortDictionaryByKey(dict)
PrintDictionary "Key Ascending", dict

' Sort Descending


Set dict = SortDictionaryByKey(dict, xlDescending)
PrintDictionary "Key Descending", dict

End Sub

Public Sub PrintDictionary(ByVal sText As String, dict As Object)

Debug.Print vbCrLf & sText & vbCrLf & String(Len(sText), "=")

Dim key As Variant


For Each key In dict.keys
Debug.Print key, dict(key)
Next
End Sub

Sorting by values
To sort the dictionary by the values you can use the SortDictionaryByValue function below.

' https://excelmacromastery.com/
Public Function SortDictionaryByValue(dict As Object _
, Optional sortorder As XlSortOrder = xlAscending)

We use cookies
Onto Error
ensure that
GoTowe give
eh you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok
Dim arrayList As Object

https://excelmacromastery.com/vba-dictionary/ 18/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Set arrayList = CreateObject("System.Collections.ArrayList")

Dim dictTemp As Object


Set dictTemp = CreateObject("Scripting.Dictionary")

' Put values in ArrayList and sort


' Store values in tempDict with their keys as a collection
Dim key As Variant, value As Variant, coll As Collection
For Each key In dict

value = dict(key)

' if the value doesn't exist in dict then add


If dictTemp.exists(value) = False Then
' create collection to hold keys
' - needed for duplicate values
Set coll = New Collection
dictTemp.Add value, coll

' Add the value


arrayList.Add value

End If

' Add the current key to the collection


dictTemp(value).Add key

Next key

' Sort the value


arrayList.Sort

' Reverse if descending


If sortorder = xlDescending Then
arrayList.Reverse
End If

dict.RemoveAll
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
' Read through the ArrayList and add the values and corresponding
Ok
' keys from the dictTemp

https://excelmacromastery.com/vba-dictionary/ 19/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Dim item As Variant


For Each value In arrayList
Set coll = dictTemp(value)
For Each item In coll
dict.Add item, value
Next item
Next value

Set arrayList = Nothing

' Return the new dictionary


Set SortDictionaryByValue = dict

Done:
Exit Function
eh:
If Err.Number = 450 Then
Err.Raise vbObjectError + 100, "SortDictionaryByValue" _
, "Cannot sort the dictionary if the value is an objec
End If
End Function

The code below shows you how to use SortDictionaryByValue

' https://excelmacromastery.com/
Sub TestSortByValue()

Dim dict As Object


Set dict = CreateObject("Scripting.Dictionary")

dict.Add "Plum", 99
dict.Add "Apple", 987
dict.Add "Pear", 234
dict.Add "Banana", 560
dict.Add "Orange", 34

PrintDictionary "Original", dict

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
' Sort Ascending
assume that you are happy with it.
Set dict = SortDictionaryByValue(dict)
Ok
PrintDictionary "Value Ascending", dict

https://excelmacromastery.com/vba-dictionary/ 20/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

' Sort Descending


Set dict = SortDictionaryByValue(dict, xlDescending)
PrintDictionary "Value Descending", dict

End Sub

Public Sub PrintDictionary(ByVal sText As String, dict As Object)

Debug.Print vbCrLf & sText & vbCrLf & String(Len(sText), "=")

Dim key As Variant


For Each key In dict.keys
Debug.Print key, dict(key)
Next key

End Sub

Troubleshooting the Dictionary


This section covers the common errors you may encounter using the Dictionary.

Missing Reference
Issue: You get the error message “User-defined type not defined”
This normally happens when you create the Dictionary but forget to add the reference.

Dim dict As New Scripting.Dictionary

Resolution: Select Tools->Reference from the Visual Basic menu. Place a check in the box
beside “Microsoft Scripting Runtime”.

See Section: Creating a Dictionary

Exists is not Working


Issue: You have added a key to the Dictionary but when you use the Exists function it
returns false
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
This is normally an issue with Case Sensitivity(see above).
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 21/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

The following code adds “Apple” as a key. When we check for “apple” it returns false. This
is because it takes the case of the letters into account:

dict.Add "Apple", 4

If dict.Exists("apple") Then
MsgBox "Exists"
Else
MsgBox "Does not Exist"
End If

You can set the CompareMode property to vbTextCompare and this will ignore the case:

Dim dict As New Scripting.Dictionary


dict.CompareMode = vbTextCompare

Resolution: Set the CompareMode to vbTextCompare to ignore case or ensure your data
has the correct case.

See Section: The Key and Case Sensitivity

Object Variable Error


Issue: You get the error message “Object variable or With block variable not set” when
you try to use the Dictionary.

The normally happens when you forget to use New before you use the Dictionary. For
example, the following code will cause this error

Dim dict As Scripting.Dictionary


' This line will give "Object variable..." error
dict.Add "Apple", 4

Resolution: Use the New keyword when creating the Dictionary

Dim dict As New Scripting.Dictionary

Or
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Dim dict As Scripting.Dictionary
assume that you are happy with it.
Set dict = New Scripting.Dictionary
Ok

https://excelmacromastery.com/vba-dictionary/ 22/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

See Section: Creating a Dictionary

Useful Tips for Troubleshooting the Dictionary


If you are investigating an issue with the Dictionary it can be useful to see the contents.

Use the following sub to Print each Key and Item to the Immediate Window(Ctrl + G).

' https://excelmacromastery.com/
Sub PrintContents(dict As Scripting.Dictionary)

Dim k As Variant
For Each k In dict.Keys
' Print key and value
Debug.Print k, dict(k)
Next

End Sub

You can use it like this

Dim dict As Scripting.Dictionary


Set dict = New Scripting.Dictionary

' Add items to Dictionary here

' Print the contents of the Dictionary to the Immediate Window


PrintContents dict

If you are stepping through the code you can also add dict.Count to the Watch Window
to see how many items are currently in the Dictionary. Right-click anywhere in the code
window and select Add Watch. Type dict.Count into the text box and click Ok.

You can also use the Dictionary itself as a Watch. Add Dict to the Watch window. If you
click on the plus sign you will see the contents of the Dictionary. This can be useful but it
only shows the key and not the item.

Note: You can only view Watches when the code is running.

We useRemember that you


cookies to ensure that can download
we give all the
you the best code examples
experience from the
on our website. post.
If you Just go
continue to the
to use this site we will
download section at the top. assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 23/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Copying the Dictionary to an Array


As we know the dictionary is made up of Key and Value pairs. The dictionary has a Keys
property which is an array of all the keys and an Items property which is an array of all
the items(i.e. values).

As both of these properties are arrays, we can write them directly to a worksheet as we
will see in the next section.

If we want to copy either the Keys or Items array to a new array then we can do it very
easily like this:

Dim arr As Variant


arr = dict.Keys

The following example copies the Keys and Items arrays to new arrays. Then the contents
of the new arrays are printed to the Immediate Window:

Sub DictionaryToArray()

' Create dictionary and add entries


Dim dict As New Dictionary
dict.Add "France", 56
dict.Add "USA", 23
dict.Add "Australia", 34

' Declare variant to use as array


Dim arr As Variant
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
' Copy keys to array
Ok
arr = dict.Keys

https://excelmacromastery.com/vba-dictionary/ 24/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

' Print array to Immediate Window(Ctrl + G to View)


Call PrintArrayToImmediate(arr, "Keys:")

' Copy items to array


arr = dict.Items
' Print array to Immediate Window(Ctrl + G to View)
Call PrintArrayToImmediate(arr, "Items:")

End Sub

' Prints an array to the Immediate Window(Ctrl + G to View)


Sub PrintArrayToImmediate(arr As Variant, headerText As String)

Debug.Print vbNewLine & headerText


Dim entry As Variant
For Each entry In arr
Debug.Print entry
Next

End Sub

When you run the code you wil get the following output:

We useNote thatto you


cookies canthat
ensure only
wecopy thethe
give you bestarray
Items when on
experience it contains basic
our website. data
If you typestolike
continue usestring,
this site we will
assume
long, date, double etc. If the items arethat you are
objects happy
then youwith
canit.not copy them to an array.
You’ll need to read through the dictionary using
Ok a loop instead.

https://excelmacromastery.com/vba-dictionary/ 25/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Writing the Dictionary to the Worksheet


We can write the Dictionary keys or items to the worksheet in one line of code.

When you write out the keys or items they will be written to a row. If you want to write
them to a column you can use the WorksheetFunction.Transpose function.

The code below shows examples of how to write the Dictionary to a worksheet:

Sub DictionaryToWorksheet()

Dim dict As New Dictionary

dict.Add "France", 56
dict.Add "USA", 23
dict.Add "Australia", 34

Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Sheet1")

' Write keys to range A1:C1


sh.Range("A1:C1").Value = dict.Keys

' Write items to range A2:C2


sh.Range("A2:C2").Value = dict.Items

' Write keys to range E1:E3


sh.Range("E1:E3").Value = WorksheetFunction.Transpose(dict.Keys)

' Write items to range F1:F3


sh.Range("F1:F3").Value = WorksheetFunction.Transpose(dict.Items)

End Sub

Useful Dictionary Examples


We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
The easiest way to see the benefits of the Dictionary is to see some real-world examples
assume that you are happy with it.
of it’s use. So in this section we are going to look at some examples. You can get
Ok

https://excelmacromastery.com/vba-dictionary/ 26/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

workbooks and code for these examples by entering your email below:

Download the Dictionary VBA


Code
Enter your email to get the Excel VBA code for this post plus
exclusive content not available on the website

Enter your email address here.

GET THE VBA CODE 

Example 1 – Summing Single Values


Let’s have a look at a real-world example of using a dictionary. Our data for this example
is the World Cup Final matches from 2014.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Our task here is to get the number of goals scored by each team.
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 27/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

The first thing we need to do is to read all the data. The following code reads through all
the matches and prints the names of the two teams involved.

' https://excelmacromastery.com/vba-dictionary
' Reads the World Cup data from the 2014 Worksheet
' View the results in the Immediate Window(Ctrl + G)
Sub GetTotals()

' Get worksheet


Dim wk As Worksheet
Set wk = ThisWorkbook.Worksheets("2014")

' Get range for all the matches


Dim rg As Range
Set rg = wk.Range("A1").CurrentRegion

Dim Team1 As String, Team2 As String


Dim Goals1 As Long, Goals2 As Long

Dim i As Long
For i = 2 To rg.Rows.Count
' read the data from each match
Team1 = rg.Cells(i, 5).Value
Team2 = rg.Cells(i, 9).Value
Goals1 = rg.Cells(i, 6).Value
Goals2 = rg.Cells(i, 7).Value
' Print each teams/goals to Immediate Window(Ctrl + G)
Debug.Print Team1, Team2, Goals1, Goals2
Next i

End Sub

What we want to do now is to store each team and the goals they scored. When we meet
a team for the first time we add the name as a Key and the number of goals as the Item.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 28/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Celebrating a Goal | © BigStockPhoto.com

If the team has already been added then we add the goals they scored in the current
match to their total.

We can use the following line to add goals to the current team:

dict(Team1) = dict(Team1) + Goals1

This line is very powerful.

If the teams exists in the Dictionary, the current goals are added to the current total for
that team.

If the team does not exist in the Dictionary then it will automatically add the team to the
Dictionary and set the value to the goals.

For example, imagine the Dictionary has one entry

Key, Value
Brazil, 5

Then

dict("Brazil") = dict("Brazil") + 3

We usewill update
cookies the dictionary
to ensure soyou
that we give it now looks
the best like this on our website. If you continue to use this site we will
experience
assume that you are happy with it.
Key, Value
Ok

https://excelmacromastery.com/vba-dictionary/ 29/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Brazil, 8

The line

dict("France") = dict("France") + 3

will update the dictionary so it now looks like this

Key, Value
Brazil, 8
France, 3

This saves us having to write code like this:

If dict.Exists(Team1) Then
' If exists add to total
dict(Team) = dict(Team) + Goals1
Else
' if doesn't exist then add
dict(Team) = Goals1
End If

We write out the values from the Dictionary to the worksheet as follows:

' Write the data from the dictionary to the worksheet


' https://excelmacromastery.com/vba-dictionary
Private Sub WriteDictionary(dict As Scripting.Dictionary _
, shReport As Worksheet)

' Write the keys


shReport.Range("A1").Resize(dict.Count, 1).Value = WorksheetFuncti

' Write the items


shReport.Range("B1").Resize(dict.Count, 1).Value = WorksheetFuncti

End Sub

We useWe obviously
cookies want
to ensure the
that wescores tothe
give you bebest
sorted. It is much
experience easier
on our to read
website. If youthis way.toThere
continue is no
use this site we will

easy way to sort a Dictionary. assume


The waythat
to you
do itare
is happy with it.
to copy all the items to an array. Sort the
Ok
array and copy the items back to a Dictionary.

https://excelmacromastery.com/vba-dictionary/ 30/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

What we can do is sort the data once it has been written to the worksheet. We can use
the following code to do this:

' Sort the data on the worksheet


' https://excelmacromastery.com/vba-dictionary
Public Sub SortByScore(shReport As Worksheet _
, Optional sortOrder As XlSortOrder = xlDescending)

Dim rg As Range
Set rg = shReport.Range("A1").CurrentRegion
rg.Sort rg.Columns("B"), sortOrder

End Sub

Our final GetTotals sub looks like this:

' https://excelmacromastery.com/vba-dictionary
Sub GetTotalsFinal()

' Create dictionary


Dim dict As New Scripting.Dictionary

' Get worksheet


Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("2014")

' Get range


Dim rgMatches As Range
Set rgMatches = sh.Range("A1").CurrentRegion

Dim team1 As String, team2 As String


Dim goals1 As Long, goals2 As Long
Dim i As Long

' Read through the range of data


For i = 2 To rgMatches.Rows.Count

' read the data to variables


We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
team1 = rgMatches.Cells(i, 5).Value
assume that you are happy with it.
team2 = rgMatches.Cells(i, 9).Value
Ok
goals1 = rgMatches.Cells(i, 6).Value

https://excelmacromastery.com/vba-dictionary/ 31/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

goals2 = rgMatches.Cells(i, 7).Value

' Add the totals for each team to the dictionary.


' If the team doesn't exist it will be automatically added
dict(team1) = dict(team1) + goals1
dict(team2) = dict(team2) + goals2

Next i

' Get the report worksheet


Dim shReport As Worksheet
Set shReport = ThisWorkbook.Worksheets("2014 Report")

' Write the teams and scores to the worksheet


WriteDictionary dict, shReport

' Sort the range


' Change to xlAscending to reverse the order
SortByScore shReport, xlDescending

' Clean up
Set dict = Nothing

shReport.Activate

End Sub

When you run this code you will get the following results

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Teams ordered by number of goals scored
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 32/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Example 2 – Dealing with Multiple Values


We are going to use the data from the Multiple Values section above

Imagine this data starts at cell A1. Then we can use the code below to read to the
dictionary.

The code includes two subs for displaying the data:

1. WriteToImmediate prints the contents of the dictionary to the Immediate Window.


2. WriteToWorksheet writes the contents of the dictionary to the worksheet called Output.

To run this example:

1. Create a worksheet called Customers.


2. Add the above data to the worksheet starting at cell A1.
3. Create a worksheet called Output and leave it blank.
4. Go to the Visual Basic Editor(Alt + F11).
5. Select Tools->Reference and then check “Microsoft Scripting Runtime” from the list.
6. Create a new class module and add the first piece of code from below.
7. Create a new standard module and add the second piece of code from below.
8. Press F5 to run and select Main from the menu.
9. Check the ImmediateWindow(Ctrl + G) and the Output worksheet to see the results.

' clsCustomer Class Module Code


Public CustomerID As String
Public Amount As Long
Public Items As Long

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
' Standard module Code
assume that you are happy with it.
' https://excelmacromastery.com/
Ok
Sub Main()

https://excelmacromastery.com/vba-dictionary/ 33/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Dim dict As Dictionary

' Read the data to the dictionary


Set dict = ReadMultiItems

' Write the Dictionary contents to the Immediate Window(Ctrl + G)


WriteToImmediate dict

' Write the Dictionary contents to a worksheet


WriteToWorksheet dict, ThisWorkbook.Worksheets("Output")

End Sub

Private Function ReadMultiItems() As Dictionary

' Declare and create the Dictionary


Dim dict As New Dictionary

' Get the worksheet


Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Customers")

' Get the range of all the adjacent data using CurrentRegion
Dim rg As Range
Set rg = sh.Range("A1").CurrentRegion

Dim oCust As clsCustomer, i As Long


' read through the data
For i = 2 To rg.Rows.Count

' Create a new clsCustomer object


Set oCust = New clsCustomer

' Set the values


oCust.CustomerID = rg.Cells(i, 1).Value
oCust.Amount = rg.Cells(i, 2).Value
oCust.Items = rg.Cells(i, 3).Value
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
' Add the new clsCustomer object to the dictionary
Ok
dict.Add oCust.CustomerID, oCust

https://excelmacromastery.com/vba-dictionary/ 34/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Next i

' Return the dictionary to the Main sub


Set ReadMultiItems = dict

End Function

' Write the Dictionary contents to the Immediate Window(Ctrl + G)


' https://excelmacromastery.com/
Private Sub WriteToImmediate(dict As Dictionary)

Dim key As Variant, oCust As clsCustomer


' Read through the dictionary
For Each key In dict.Keys
Set oCust = dict(key)
With oCust
' Write to the Immediate Window (Ctrl + G)
Debug.Print .CustomerID, .Amount, .Items
End With

Next key

End Sub

' Write the Dictionary contents to a worksheet


' https://excelmacromastery.com/
Private Sub WriteToWorksheet(dict As Dictionary, sh As Worksheet)

' Delete all existing data from the worksheet


sh.Cells.ClearContents

Dim row As Long


row = 1

Dim key As Variant, oCust As clsCustomer


' Read through the dictionary
For Each key In dict.Keys
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Set oCust = dict(key)
assume that you are happy with it.
With oCust
Ok
' Write out the values

https://excelmacromastery.com/vba-dictionary/ 35/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

sh.Cells(row, 1).Value = .CustomerID


sh.Cells(row, 2).Value = .Amount
sh.Cells(row, 3).Value = .Items
row = row + 1
End With

Next key

End Sub

Example 3 – Summing Multiple Values


In this example were are going to make a small update to Example 2. In that example
there was only one entry per customer in the data.

This time there will be multiple entries for some customers and we want to sum the total
Amount and total Items for each customer.

See the updated dataset below:

Note: If you run the “Example 2” code on data with multiple copies of the CustomerID, it
will give the “Key already exists error”.

' clsCustomer Class Module Code


Public CustomerID As String
Public Amount As Long
Public Items As Long

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
' Read from worksheet: CustomerSum
assume that you are happy with it.
' Write to worksheet: CustomerRepSum
Ok
' https://excelmacromastery.com/vba-dictionary

https://excelmacromastery.com/vba-dictionary/ 36/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Sub MainSum()

Dim dict As Dictionary

' Read the data to the dictionary


Set dict = ReadMultiItemsSum

' Write the Dictionary contents to the Immediate Window(Ctrl + G)


WriteToImmediate dict

' Write the Dictionary contents to a worksheet


WriteToWorksheet dict, ThisWorkbook.Worksheets("CustomerRepSum")

End Sub

' Read multiple items but this time sums the items
' https://excelmacromastery.com/
Private Function ReadMultiItemsSum() As Dictionary

' Declare and Create the Dictionary


Dim dict As New Dictionary

' Get the worksheet


Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("CustomerSum")

' Get the range of all the adjacent data using CurrentRegion
Dim rg As Range
Set rg = sh.Range("A1").CurrentRegion

Dim oCust As clsCustomer, i As Long, customerID As String


' read through the data
For i = 2 To rg.Rows.Count

customerID = rg.Cells(i, 1).Value

' check if the customerID has been added already


If dict.Exists(customerID) = True Then
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
' Get the existing customer object
assume that you are happy with it.
Set oCust = dict(customerID)
Ok
Else

https://excelmacromastery.com/vba-dictionary/ 37/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

' Create a new clsCustomer object


Set oCust = New clsCustomer

' Add the new clsCustomer object to the dictionary


dict.Add customerID, oCust
End If

' Set the values


oCust.Amount = oCust.Amount + rg.Cells(i, 2).Value
oCust.Items = oCust.Items + rg.Cells(i, 3).Value

Next i

' Return the dictionary to the Main sub


Set ReadMultiItemsSum = dict

End Function

' Write the Dictionary contents to the Immediate Window(Ctrl + G)


' https://excelmacromastery.com/vba-dictionary
Private Sub WriteToImmediate(dict As Dictionary)

Dim key As Variant, oCust As clsCustomer


' Read through the dictionary
For Each key In dict.Keys
Set oCust = dict(key)
With oCust
' Write to the Immediate Window (Ctrl + G)
Debug.Print key, .Amount, .Items
End With

Next key

End Sub

' Write the Dictionary contents to a worksheet


' https://excelmacromastery.com/
Private Sub WriteToWorksheet(dict As Dictionary, sh As Worksheet)
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
' Delete all existing data from the worksheet
Ok
sh.Cells.ClearContents

https://excelmacromastery.com/vba-dictionary/ 38/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Dim row As Long


row = 1

Dim key As Variant, oCust As clsCustomer


' Read through the dictionary
For Each key In dict.Keys
Set oCust = dict(key)
With oCust
' Write out the values
sh.Cells(row, 1).Value = key
sh.Cells(row, 2).Value = .Amount
sh.Cells(row, 3).Value = .Items
row = row + 1
End With

Next key

End Sub

When To Use The Dictionary


So when should you use the VBA Dictionary? When you have a task where:

1. You have a list of unique items e.g. countries, invoice numbers, customer name and
addresses, project ids, product names etc.
2. You need to retrieve the value of a unique item.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

2
https://excelmacromastery.com/vba-dictionary/ 39/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery
2
Key/Values of Countries and Land area in Km

Download the Dictionary VBA


Code
Enter your email to get the workbook and Excel VBA code for
this post plus exclusive content not available on the website

Enter your email address here.

GET THE VBA CODE 

What’s Next?
Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA skills
then why not try out the The Ultimate VBA Tutorial.

Related Training: Get full access to the Excel VBA training webinars and all the tutorials.

(NOTE: Planning to build or manage a VBA Application? Learn how to build 10 Excel VBA
applications from scratch.)

228 Comments
← Older Comments
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
V BALACHANDRAN on you
assume that Juneare
11,happy
2020with
at 4:17
it. am
Ok

https://excelmacromastery.com/vba-dictionary/ 40/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Excellent. Very well covered


Can you pl help with a general programme taking number of keys as
variable and dynamically taking the entire table and then place results in
the work sheet by totalling for respective keys.

Reply
David on June 19, 2020 at 3:11 pm
Hi,
I successfully managed to read data from a closed workbook & storing the
excel data sheet (it’s rows/columns values) into an array() variant. All
works smoothly with the exception that some rows contain more than
255 characters. I’m not getting any error message but the text is simply
cut up and until it reaches the 255 character. Is there a way I could
increase that maximum limit by using one of the above possibilities such
as Dictionaries, Arraylist or Collection? Thanks in advance for your prompt
response.

Reply

Quinn Williams on June 28, 2020 at 4:17 am


No luck with this method

Dim Key As Variant


For Each Key In dictReadProfile.Keys
Debug.Print Key, dictReadProfile(Key)
Next Key

Using GitHub vba Dictionary which is supposed to exactly emulate the MS


version ( I want this App to work on macs as well, obviously)

Reply

Al1 on September 3, 2020 at 3:17 pm


Great One,
I was just wondering if clsCustomer could contain a Collection ? for
exemple
We use cookies to ensure item1
that we give youitem2 … item
the best n
experience on our website. If you continue to use this site we will
And how you would assume that you
update it.are
Forhappy with it.
exemple when it contains item1 item2
==> updating it to item1 item2 item 3
Ok

https://excelmacromastery.com/vba-dictionary/ 41/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Something like MyColl = dict(key).TheColl MyColl.Add item3 and


dict(key).TheColl = MyColl would do the job ?

Reply

Paul Kelly on September 7, 2020 at 9:22 am


Yes it can contain a collection. How you update it really depends
on what you are trying to do.

Reply
Sam on October 13, 2020 at 1:42 pm
Very nice reference. Is the only way to do a multidimensional dictionary
(i.e., a first and a second key) to store a collection or a dictionary inside
another dictionary? I’ve always found that method a bit clunky.

Reply

Paul Kelly on October 23, 2020 at 2:06 am


Glad you like it. Yes – that is the only way.

Reply

Pablo Garcia on November 10, 2020 at 2:46 am


Hello Paul. I really enjoy your content, I think it’s the best VBA learning
resource out there.
I have a short question: I’ve seen in this very post, that most times you
declare the Dictionary: DictName as Scripting.Dictionary vs in Example 2 it
just says DictName as Dictionary (without “Scripting.”). And I’ve also seen
that difference elsewhere. Is it the same or does it have any difference?

Reply

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Paul Kelly on November 11, 2020 at 10:12 am
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 42/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

It doesn’t make much difference. Using Scripting is a way of


clarifying that the dictionary is from the scripting library.

Reply
Philip Petev on February 13, 2021 at 6:07 pm
I thinks there’s an unused variable in the SortDictionaryByKey functions

coll As New Collection

It seems coll is declared, but never used, not in this function anyway…

Reply

Paul Kelly on February 15, 2021 at 3:11 am


Thanks Philip. I have updated the code.

Reply

Oliver on March 15, 2021 at 3:59 pm


Hi Paul,
the function “SortDictionaryByValue” works fine but it sorts only text. If
the values are numbers you get a somewhat unexpected result, I think. I
put a loop before filling the ArrayList like this.

Dim mKeys As Variant, mItems As Variant


Dim isNumber As Boolean, i As Integer
isNumber = False

mItems = dict.Items
mKeys = dict.Keys
For i = 0 To dict.Count – 1
If IsNumeric(mItems(i)) = True Then
isNumber = True
Else
isNumber = False
End If
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Next
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 43/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

and convert the values if they are all numeric in the section where the
ArrayList is generated like this

If isNumber = True Then


value = CDbl(dict(key)) ‘Convert to Double and sort as number
Else
value = dict(key) ‘Sort as text
End If

This way the result of the function sorts the dictionary correctly if there
are only numeric values.
I think that this way the function works more like excel would sort in a
table.

You have a great blog that illustrates so much for everybody that gets into
VBA. Thank you for sharing
your knowledge.

Cheers, Oliver

Reply
Monica on May 5, 2021 at 5:25 pm
Thank you for a GREAT resource!
Could you help me understand if it’s possible to use multiple key fields as
a UID with the dictionary? I’m trying to compare two sheets; the same
data feed but at different points in time. Each row is unique base on the
combination of the PO document number, line item number and a
delivery date. I’d like to identify when all changes from between the two
points in time but not sure how to match the data. Any recommendations
on a technique that could work?

Reply

Paul Kelly on May 6, 2021 at 1:47 am


You can combine the fields to provide one key as long as it will be
unique.

Reply
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 44/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

plsadd on May 23, 2021 at 11:13 am


Its not complete when it doesnt have the .item property included..
.item makes using the .Exist function obsolete

Reply

Paul Kelly on May 24, 2021 at 9:17 am


dictionary(key) is a shortcut for dictionary.Item(key)

Exist is still required if that value is an object.

Reply

MARK E. YOUNG on July 23, 2021 at 4:48 pm


Paul,

Thank you for creating and publishing your website & YouTube Channel,
they are a great help as I learn VBA! I am developing an Excel Application
and look forward to working through The Excel VBA Handbook in the very
near future!

I am working my way through your Article on Dictionaries. I have built a


dictionary, using a Class Module as you demonstrate in Sect 19.2 Example
2 – Dealing with Multiple Values in this Article.

I have a table of Materials on a worksheet. Each Material Item has a


Unique ID in Field 1, and has a total of nine (8) more columns of
information. The Sub ran well, however; the dict.count property shows a
count of 3,134 items, while the Locals Window shows a count of only 256
Items in the Dictionary.

Is there a limit on the number of Key/Items that can be stored in a


Dictionary or is it that the Locals Window will only show a set number of
Items.

Thanks again!

Reply
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 45/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Paul Kelly on July 24, 2021 at 3:04 am


Hi Mark, That limit is the Locals Window.

You can use a for loop with Debug.Print to the print the contents
to the Immediate Window.

-Paul

Reply
Steve Wombat on August 3, 2021 at 5:42 am
Comprehensive and extrememly helpful, thanks for this, amazing work.

Reply

Paul Kelly on August 5, 2021 at 1:41 am


Thanks Steve.

Reply

Jason McCoy on August 4, 2021 at 2:46 pm


I am going through the “How to Design and Code an Excel VBA Application
Like a Pro” on the Excel Macro Mastery YouTube channel (the video was
Premiered Mar 11, 2020). I have gotten to about the 24:28 mark where I
have run into a problem I can’t fix. I am working on a MacBook Air (Big
Sur). I have installed the Dictionary class module from github and it seems
to be working just fine (when I Debugged the application, I could see my
Dictionary listed in the Watch Window). The line that is giving me the issue
is the line that says

For Each key In Dict


Debug.Print key, Dict(key)
Next key

When I click on the “Create Report” button to run the macro, I get a run-
time error at the codeline I just pasted above:
Run-time Error ‘438’:
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
Object doesn’t assume
supportthat
thisyou
property or method
are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 46/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

So there is something wrong with the syntax for this line “For Each key In
Dict”
Do I need to use something other than a For Each loop or does the key
need any qualifiers added to it? I really don’t know this is the first time I
have ever worked with a Dictionary in Excel. Any and all help would be
most appreicated. I have went through a lot of your videos and they are
all awesome. I have learned a lot!!

Jason

Reply

Paul Kelly on August 5, 2021 at 1:40 am


Hi Jason,

Try this:
For Each Key in Dict.Keys

Paul

Reply

Exy Ayala on May 6, 2022 at 10:09 pm


Paul Kelly is the Goat!! That means double important!!

Reply
David on August 22, 2021 at 6:14 pm
Hello Paul,
First of all, thank you very much for all of your guides, they are extremely
clear and helpful.
Using your dictionary guide I have written a code to read through a
filtered column range.
As per your suggestion I wrote it first by using Early Binding, and it
worked.
Afterwards I wrote a new code, copying the one from before but changing
it to Late Binding.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
I can read the filtered
assume column andhappy
that you are i get with
a dictionary
it. where the keys are
the row number and the items Ok
are the value on each row.
But when I tried to debug print the keys and items I got the error:
https://excelmacromastery.com/vba-dictionary/ 47/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Property let procedure not defined and property get procedure did not
return an object (Error 451)
Could you clarify to me why this error pops up?
Thank you very much in advance.

Here is the code:

Public Sub testsub()

Dim rng1 As Range


Dim count As Long
Dim RColDict As Object
Set RColDict = CreateObject(“Scripting.Dictionary”)

Set rng1 = Range(“A1:A100”).SpecialCells(xlCellTypeVisible).Cells


Set RColDict = ReadColData(rng1)

For count = 0 To RColDict.count – 1


Debug.Print RColDict.Keys(count), RColDict.Items(count)
Next count

End Sub

Public Function ReadColData(ColRng As Range) As Object


Dim RColDict As Object
Set RColDict = CreateObject(“Scripting.Dictionary”)
Dim RngAddressArray() As String
Dim NumberOfAreas As Long
Dim RngAddress As Variant
Set ReadColData = CreateObject(“Scripting.Dictionary”)

NumberOfAreas = ColRng.Areas.count
ReDim RngAddressArray(0 To NumberOfAreas – 1)
RngAddressArray = Split(ColRng.Address, “,”)

For Each RngAddress In RngAddressArray


Set RColDict = ReadColRangeAddress(RColDict, RngAddress)
Next RngAddress

Set ReadColData = RColDict


Set RColDict = Nothing

End
We use cookies to ensure thatFunction
we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Private Function ReadColRangeAddress(RColDict,
Ok
RngAddress As Variant)

https://excelmacromastery.com/vba-dictionary/ 48/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

As Object

Dim count As Long


Dim StartRow As String, FinalRow As String, ColIndex As String
Dim CellValue As Variant
Set ReadColRangeAddress = CreateObject(“Scripting.Dictionary”)

If UBound(Split(RngAddress, “:”)) > 0 Then


StartRow = Split(RngAddress, “:”)(0)
ColIndex = Split(StartRow, “$”)(1)
StartRow = Split(StartRow, “$”)(2)
FinalRow = Split(RngAddress, “:”)(1)
FinalRow = Split(FinalRow, “$”)(2)
Else
StartRow = Split(RngAddress, “:”)(0)
ColIndex = Split(StartRow, “$”)(1)
StartRow = Split(StartRow, “$”)(2)
FinalRow = StartRow
End If

For count = StartRow To FinalRow


CellValue = Cells(count, ColIndex) ‘Cells accepts letters as column indexes
RColDict.Add count, CellValue
Next count

Set ReadColRangeAddress = RColDict

End Function

Reply
Vic Asp on October 22, 2021 at 7:30 pm
Paul,

Thank you so much for the great VBA articles, I have learned so much. I
am having one problem though I can’t get passed.

sub Main()
Dim dict As New Dictionary
‘fill dictionary
Call Routine(dict)
end
We use cookies to ensure thatsub
we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
sub Routine(dict As Dictionary)
Ok
x = dict.count <—— Compile error: Constant expression required
https://excelmacromastery.com/vba-dictionary/ 49/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

end sub

If I code around this problem I can use dict.keys later with no problems.
Any help would be greatly appreciated.
Thanks,
Vic

Reply

Paul Kelly on October 27, 2021 at 10:47 am


Hi Vic,

It depends on what the variable x is defined as. VBA seems to


think that it is a constant. It should be declared using Dim to be a
long variable.

-Paul

Reply
sandeep on November 4, 2021 at 3:29 am
Great article & great webinar.

Reply

sandeep on November 4, 2021 at 3:33 am


Great article & a great webinar.

Reply

Raj on November 7, 2021 at 12:45 pm


Hi, I have a scripting.dictionary in Excel vba and would like to pass this
dictionary to a C++ DLL. Would you know what the data type in C++ would
be please? In C++ the equivalent to a dictionary is called map. I am able to
pass double quite easily but with this more complex data type I am
unsure
We use cookies to ensure that wewhat the the
give you C++best
conversion
experiencewould
on ourbe here. IfMany
website. thanks to use this site we will
you continue
assume that you are happy with it.
Ok Reply
https://excelmacromastery.com/vba-dictionary/ 50/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Göran Borgolte on November 26, 2021 at 2:21 pm


Hello Mr. Kelly,

this is my first online post regarding VBA. Your guide was superb and I
was able to improve a lot of my tools with dictionaries.

Maybe you can help me with my question:

Property Get Value(strTimestamp As String) As Double

Value = MydictValues.Item(strTimestamp)

End Property

Whenever my code ran this line, it added the string as a new key to the
dictionary if it wasn’t already a key. This became a huge issue. I thought
this line just gets the value of the key and if the key does not exist it
returns null.

I changed my code now to this:

Property Get Value(strTimestamp As String) As Double

If MydictValues.Exists(strTimestamp) Then
Value = MydictValues.Item(strTimestamp)
Else
Err.Raise Number:=1, Description:=”Es wurde für Zeitstempel ein Wert
abgefragt, welcher nicht existiert.”
End If

End Property

The error description is german, because I am a native german person.


Sorry for my bad english.

Reply

Stefano Gatto on February 12, 2022 at 11:15 am


Thank you for this very instructive article. This is really useful, I must say.
How important is it to include the Clean Up section of your code (Set dict =
Nothing).
We use cookies to ensure Is this
that we give you capital? I have inherited
the best experience some code
on our website. which
If you seems
continue tothis
to use endsite we will
up being short assume
in resources and
that you areI happy
wouldwith
likeit.to know whether it’s worth
Ok

https://excelmacromastery.com/vba-dictionary/ 51/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

investing time to check out that those clean-up sections are well
implemented everywhere in the code.

Reply
Joseph on March 28, 2022 at 2:20 pm
Paul – all of your stuff is so great. Thank you.

Question – can one use a user-defined data type variable instead of a


class module in order to store multiple values? Class modules are
certainly more versatile, but a little bit more horsepower than what I need
the dictionary for.

Reply

KWEKU ABAKA QUAGRAINE on July 19, 2022 at 10:27 pm


I recently discovered this site and find it a very useful resource for
building up my knowledge and skills. Thanks for your excellent deliveries.

Reply

Paul Kelly on July 25, 2022 at 11:14 am


you’re welcome Kweku

Reply

Sanjay on August 14, 2022 at 5:56 pm


Great article.

I have a set of customer data of 15 different fields and numerous (1000’s)


rows each with a unique Cust ID from Book1 which needs to be copied
into Book2 but has the fields in a different order as it will be getting data
from other sources. So the column order may not be continuous.

Would it be best to create 15 dictionaries one for each field and have the
key
We use cookies to ensure forweeach
that give dictionary
you the bestlinked to the
experience oncustomer IDIfor
our website. yourow number
continue from
to use this site we will

Book1 or wouldassume that youaare


you suggest happy with
different it.
method?
Ok

https://excelmacromastery.com/vba-dictionary/ 52/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Reply
joe on October 31, 2022 at 4:19 pm
Hi Paul, This is my first time posting and I wanted to thank you for the
excellent articles. Can you help me understand what is ClearData function
that’s used in the below WriteDictionary Sub? I don’t see it defined
anywhere in this article. Thank you.

Private Sub WriteDictionary(dict As Scripting.Dictionary _


, shReport As Worksheet)

ClearData shReport

‘ Write the keys


shReport.Range(“A1”).Resize(dict.Count, 1).Value =
WorksheetFunction.Transpose(dict.Keys)

‘ Write the items


shReport.Range(“B1”).Resize(dict.Count, 1).Value =
WorksheetFunction.Transpose(dict.Items)

End Sub

Reply

Paul Kelly on November 8, 2022 at 12:37 pm


I removed this now. Typically I use a ClearData function to remove
existing data before I write new data. It ensures that you don’t mix
new with old.
It looks something like this:
shReport.Range(“A1”).CurrentRegion.Offset(1).ClearContents

Reply

Joe on November 8, 2022 at 10:11 pm


Perfect, thanks very much for taking the time to clarify and
respond.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok Reply
https://excelmacromastery.com/vba-dictionary/ 53/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Brandon on December 19, 2022 at 6:18 pm


Hi Paul, great site! I have a question about dictionaries when pulling from
an API. I am using the VBA-JSON converter to get data about different
cryptocurrencies, but occasionally a field will be missing from one of them
and I would like to know how to catch that with an If statement.

Example of JSON (abbreviated):


{
“_embedded”: {
“records”: [
{
“asset”: “yXLM-
GARDNV3Q7YGT4AKSDF25LT32YSCCW4EV22Y2TV3I2PU2MMXJTEDL5T55-
1”,
“volume7d”: 10379068759729,
“tomlInfo”: {
“orgName”: “Ultra Stellar LLC dba Ultra Stellar”,
},
},
}

but some of them don’t have a “tomlInfo” key, so it’s giving me a Type
Mismatch error when I try accessing Rec(“tomlInfo”)(“orgName”).

My VBA (snippet):
Dim Recs As Collection
Set Recs = AResp(“_embedded”)(“records”) ‘ Response parsed with
JSONconverter

Dim Rec As Dictionary

For Each Rec In Recs


‘ Assume findVar.Row and ONc below are valid values
.Cells(findVar.Row, ONc).Value = Rec(“tomlInfo”)(“orgName”)
Next Rec

Is there a good way to skip this part if that key doesn’t exist? I tried using
dict.exists, but I don’t know how to use that with nested dictionaries like
these…

Thanks
We use cookies to ensure that wefor your
give you time
the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok Reply
https://excelmacromastery.com/vba-dictionary/ 54/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

Edgar on March 30, 2023 at 2:46 pm


For the sorting, the line “Dim key As Variant, coll As New Collection” – coll
is never used in the function. Is this a leftover from an older version?

Reply

DimZa on July 7, 2023 at 4:23 am


Hi Paul. It is my first post here and therefore I would like to thank you for
amazing resourse for learning vba. You are really great teacher.
I am having a mistake when I try to reproduce the code of example 1 in
this row:

Set shReport = ThisWorkbook.Worksheets(“2014 Report”)

Could you give me any syggestions to fix it?

Reply

Paul Kelly on July 10, 2023 at 8:52 am


You need a worksheet called 2014 Report in the current
workbook.

Reply

← Older Comments

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 55/56
2/29/24, 10:35 PM Excel VBA Dictionary - A Complete Guide - Excel Macro Mastery

You are not logged in


You are not currently logged in.
Username or Email Address:

Password:

Remember Me
Login

» Register
» Lost your Password?

5 Things I wish I knew When I started using Excel VBA

   
Designed by Elegant Themes | Powered by WordPress

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will
assume that you are happy with it.
Ok

https://excelmacromastery.com/vba-dictionary/ 56/56

You might also like