Return to VBA Code Examples

VBA Dictionary Objects

Using a VBA Dictionary

A VBA dictionary operates in a similar way to a collection object, but it has more properties and methods, and offers more flexibility

The dictionary stores the data in memory and can be easily manipulated. There is no auto calculation, background backup, and screen refreshing required, so your code will run considerably faster.

The dictionary object works in a similar way to a normal dictionary that you would use if you want to find out the meaning of a word. Each entry in the dictionary object has a ‘key’ value and an ‘item’ value.  You use the ‘key’ the key value to lookup the item value in the dictionary object, in a similar way that you would use a conventional dictionary.

Because of the way the dictionary object works, the key values must all be unique, in the same way as in a conventional dictionary.  Imagine if you opened your conventional dictionary to look up the meaning of a word, and found the word listed more than once with two entirely different definitions. You would be very confused!

Key values are usually text or numbers, or both.  Users often find it easier to remember names of keys as text rather than just numbers.

Comparing to a collection object, the collection object is read only.  It only has two methods (Add and Remove) and two properties (Count and Item). Once an item has been added to a collection object, it can only be removed, but not edited, which is a cumbersome procedure if the value of an item needs to be changed.

A dictionary object will change in size automatically to suit the number of items within it.  It does not need to be defined in size, like a conventional array

The dictionary object is one-dimensional and the data type is ‘Variant’, so any data type can be entered in to it e.g. numeric, text, date

The VBA dictionary is not native to Excel and needs to be accessed by either early or late binding when defining the dictionary object

If you use the early binding, you must add a reference to the ‘Microsoft Scripting Runtime’ library

You do this by selecting ‘Tools | References’ on the menu bar of the Visual Basic Editor (VBE) window and a pop-up window will appear with a list of available libraries.

PIC 01

Scroll down to ‘Microsoft Scripting Runtime’ and tick the box next to it.  Click OK and this library is now part of your VBA project and can be referenced using early binding.  All examples of code in this article will use early binding.

Your code will run considerably faster with early binding, because it is all compiled up front. With late binding, the object has to be compiled as the code runs

The Scripting Runtime library has ‘Intellisense’. As you write your code, you will see lists of available methods and properties appearing, which helps to prevent mistakes being made in spelling, which will cause bugs in your program

Also, if you press F2 within the VBE, and select the ‘Scripting’ library, you will see all the methods and properties available, and the parameters required for each


Distributing your Excel Application Containing a Dictionary

As already pointed out, the Scripting Runtime library is not part of Excel VBA so if you distribute your application to other users, they must have access to the Scripting Runtime library on their computer.  If they have not, then an error will occur.

It is a good idea to include some VBA code to check that this library is present when your Excel application is loaded.  You can use the ‘Dir’ command to do this on the ‘Workbook Open’ event

The location of the file is C:\Windows\SysWOW64\scrrun.dll


Scope of a Dictionary Object

The Dictionary object is only available while the Excel workbook is open. It does not get saved when the workbook is saved.

If your dictionary is to be available to all the routines within your module, you need to declare it (Dim) in the Declare section at the very top of the module

You define it as a global object if you want your dictionary to be used throughout your code.

Populating and Reading from Your Dictionary

To start with, you need to create a dictionary, populate it with some data, and then iterate through it to prove that the data exists

This code creates a new dictionary object called ‘MyDictionary’ and then populates it with three items.  The Add method has two parameters – Key and Item, and they are both required

The data types for Key and Item are both variant so they will accept any type of data – numeric, text, date, etc

The first item in the dictionary could be added as:

The values have been reversed between Key and Item, but this would still work, although the search key would now become 10.

However, it is important to understand that the key value is the lookup value in the dictionary. It works in a very similar way to the VLOOKUP function in Excel.  Because all the keys have to have unique values, you can specify a key value and instantly return the item value for that key.

Note that the dictionary index starts at 0 so you need to subtract 1 from the dictionary count used in the For…Next loop

You can also use a For…Each loop to read the values in the dictionary:

This code will iterate through each item and display the item key and the item value


Using the Item Index Number

You can use the index number of a key or item to read the value

This code will return the key ‘item3’ as the index starts at 0, and the item value 20

You can refer to individual key or item values within the Keys or Items collections by using the index numbers.

Filtering the Dictionary

There is not a direct method to do this, but it is quite simple to write code to do it:

The filter value only works from the beginning of the key value.  You cannot use wildcards in the filter. This code will return the two item values with key names beginning with ‘BB’

This will give you a subset of the dictionary based on your filter value, which you could then transfer to another dictionary or a worksheet. With careful planning on key names, making sure that there is a meaningful prefix to each, you would easily be able to split the dictionary into various component parts.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! automacro

Learn More!!

Changing an Item Value of a Key

The dictionary object has a big advantage over a collection in that the item value can be changed e.g.

In the collection, you would need to delete that entry and then re-create it.

Here is a code example:

The above code sets up three items within the dictionary, and then changes the value of ‘MyItem2’ from 20 to 25.

It also changes the value of ‘MyItem4’ to 40.  Note that in the add statements of the code, no ‘MyItem4’ was added.  When you change the value of a key that does not exist, it is automatically created. This is extremely convenient, as no error is triggered, but it does mean that you need to be careful with your key names. An inadvertent spelling error in the key name would mean that a new key is created, and the original key name would still have the old value.

This could easily lead to integrity problems in the dictionary object.


Test if a Key Exists

You can check if a key value exists within the dictionary

The code adds three items to a new dictionary object, and then tests for a key (‘MyItem8’) which is not in the dictionary.  This returns False, but had one of the existing keys been used, it would return True

Wildcards are not accepted. Search text is also case sensitive by default, but this can be changed (see later in article)


Using Multiple Values in a Dictionary

Unlike an array, the dictionary object is one dimensional only.  This can lead to problems if you have several values that you want to put against a key.

One way round this is to concatenate each item value using a delimiter character in between each value e.g. ‘|’


Another way around this problem is to design your own sub-scripting system for key names.  There is no reason why you should not use brackets and numbers in key names

This code adds three keys to the dictionary, but each key name contains a sub script number in brackets. You can then refer to key name, but using the sub script number concatenated in.  This is very similar to using an array object

VBA Programming | Code Generator does work for you!

Deleting Items

You can remove individual items by reference to the key value

Note that because key names are unique, this only removes that one particular key and item value

You can also clear the dictionary completely

Here is an example of using ‘Remove’ in VBA:


The code adds three items to the dictionary, and then removes ‘Item2’. It then iterates through the dictionary to prove that ‘Item2’ no longer exists

Finally, the code removes all items in the dictionary and displays the dictionary count, which is now zero.


Changing Case Sensitivity for Searches

If you do a search for a key, it is case sensitive by default.  However, you can use the ‘CompareMode’ property to change this.

Note that this must be done immediately in the code after you create the dictionary object, but before you add any data into the dictionary.  Once the compare mode has been set, it cannot be changed within that dictionary.

In this example, the compare mode is set to ‘TextCompare’ which means that it is not case sensitive.  The ‘Exists’ statement at the end of the example will return True, in spite of the fact that the search text is all in lower case.

In Excel there are only two values that can be used for compare mode.  Binary Compare is case sensitive, and Text Compare is non case sensitive

If you have compare mode set to Binary Compare, you need to be careful in naming your keys. If you set a name to have an upper-case letter as the first character, then when you are changing the value, you must make sure that you still make the first character upper-case.  If you start with a lower-case character, this will be interpreted as a new key, and could easily lead to confusion and errors in your dictionary

Remember that if you change a value for a key and the key name does not exist due to a Binary Compare being used, a new key and value will be added to the dictionary.

If you use Text Compare instead, then any value changes will go to the key regardless of case. If you try to add the same item but spelt with a different case character, you will get an error because it already exists.

Sorting the Dictionary

As with the collection object, there is no method provided of being able to sort the dictionary, either using keys or item values.

However, since the VBA code is sitting in an Excel workbook, the dictionary data can be transferred into Excel in tabular form, and then the Excel sort facility can be applied to it. The dictionary can then be cleared using ‘RemoveAll’ and the sorted values added from the worksheet.

This code will sort both the keys and the items values

This code creates a dictionary with five random order values added.  It captures the number of items into a variable, and then iterates through the dictionary, transferring the key and item values into separate columns on a worksheet.

It then sorts the downloaded range, using column A as the sort field. The dictionary is cleared completely using the ‘RemoveAll’ method, and the code then iterates through the cell values in the worksheet adding them back to the dictionary.

Finally, the code iterates through the dictionary, displaying the key and item values concatenated to prove that the sort has worked.

By changing the parameters in the sort code, the data could be sorted by item values.


Copying a List of Keys to a Worksheet

You can copy a list of all key values into a worksheet using the following code:

This will produce the result in your worksheet:

PIC 02

You can copy an entire dictionary into a worksheet using this code:

Your worksheet will look like this:

PIC 03

Comparing a Dictionary with a Collection

The Dictionary is faster than a collection.

A Collection is already within VBA. A Dictionary needs a reference to the Microsoft Scripting Dictionary to be added or an object created using late binding

A Collection item can only be written once and read many times. In a Dictionary, the item value can be changed. With a Collection, the item has to be removed and then the changed item added back.

The Collection works on index values, which can be difficult to work out which index value belongs where.  The Dictionary works on unique key values which are used to locate an item

Retrieving a single item is slower in a large Collection than in a Dictionary

In a Collection, the keys are only used to lookup data and are not retrievable.  In a dictionary, keys can be tested for existence, and can be used to find a particular item.

Collections are case-sensitive and this cannot be changed. In a Dictionary, the compare mode can be set to give case-sensitivity or non-case-sensitivity

In a Collection, the key values must be strings.  In a Dictionary they can be any data type e.g. numeric, date, etc

Removing all items in a Collection involves re-defining the Collection object.  The Dictionary has the ‘RemoveAll’ method for this.