A Scripting Dictionary in Excel VBA is a fast, flexible data structure that stores key–value pairs, letting you look up, update, and remove items by a unique key instead of scanning a range. To use one, add a reference to the Microsoft Scripting Runtime library (or create it late-bound with CreateObject("Scripting.Dictionary")), declare a variable as Scripting.Dictionary, then use Add, Item, Exists, and Remove to manage entries. This guide walks through the exact steps to set it up, populate it, and use it in real workbook tasks, plus the mistakes that trip people up.

What You'll Need

  • Microsoft Excel with the VBA editor available (press Alt+F11 on Windows; on Mac use the Developer tab or Tools > Macro > Visual Basic Editor).
  • A macro-enabled workbook (.xlsm) if you want to keep your code.
  • Basic familiarity with declaring variables and writing a Sub in a standard module.
  • No add-ins are required; the Scripting Runtime is part of Windows and is available on Mac Excel through late binding.

Note: menu labels and dialog wording can differ slightly by Excel version, platform, and region.

Step-by-Step

  1. Open the VBA editor and insert a module. In Excel, press Alt+F11 (Windows) or open the Developer tab and choose Visual Basic. In the editor, choose Insert > Module so your code lives in a standard module, not a sheet module.
  2. Add a reference (early binding) or skip it (late binding). For early binding, in the VBA editor choose Tools > References, tick Microsoft Scripting Runtime, and click OK. Early binding gives you IntelliSense and named constants. If you cannot find the reference or need the file to work on Mac, skip this step and use late binding instead.
  3. Declare the dictionary variable. Early binding: Dim dict As Scripting.Dictionary then Set dict = New Scripting.Dictionary. Late binding: Dim dict As Object then Set dict = CreateObject("Scripting.Dictionary"). Always use Set; a plain = assignment will fail.
  4. Add key–value pairs. Use dict.Add "Apple", 1.25. The key must be unique; adding the same key twice raises a runtime error. If you want to overwrite instead, use dict("Apple") = 1.25, which adds the key if missing or updates it if present.
  5. Check whether a key exists before reading it. Use If dict.Exists("Apple") Then before accessing dict("Apple"). Reading a missing key with the default setting silently adds it with an empty value, which can corrupt your data if you are not careful.
  6. Read and count items. dict.Count returns the number of entries. dict.Keys returns a zero-based array of keys, and dict.Items returns a zero-based array of values. Loop with For i = 0 To dict.Count - 1 and read dict.Keys()(i).
  7. Remove entries and clear the dictionary. dict.Remove "Apple" deletes one key; removing a key that does not exist raises an error, so guard with Exists. dict.RemoveAll empties the dictionary but keeps the object usable.
  8. Use it in a real task. A common pattern is deduplicating a column: loop through the range, and for each cell value use If Not dict.Exists(cell.Value) Then dict.Add cell.Value, cell.Row. The dictionary now holds each unique value once, and you can write dict.Keys to a new sheet or use the stored row to flag duplicates.
  9. Change the comparison mode if needed. By default keys are case-sensitive. Set dict.CompareMode = TextCompare (or 1 for late binding) before adding any keys to make lookups case-insensitive. Changing CompareMode after items exist raises an error.
  10. Release the object. At the end of the procedure, Set dict = Nothing frees the reference. This is good hygiene, especially in long-running macros or add-ins.

Common Mistakes

  • Using Dim dict As New Scripting.Dictionary. The auto-instancing form hides bugs and can create a fresh dictionary when you least expect it. Declare and Set explicitly.
  • Forgetting the reference with early binding. If you declare As Scripting.Dictionary without ticking the reference, the code will not compile. Either add the reference or switch to As Object.
  • Reading a missing key. dict("Missing") on a default dictionary adds the key with an empty value. Always check Exists first when the key may not be present.
  • Assuming keys are case-insensitive. They are not by default. Set CompareMode before populating if you need case-insensitive matching.
  • Using Remove without checking. Removing a non-existent key raises run-time error 32811. Guard with Exists.
  • Treating Keys and Items as one-based. Both arrays start at index 0, unlike many VBA collections.

Tips

  • Use a dictionary as a fast lookup table instead of Application.WorksheetFunction.VLookup inside a loop; it is typically much faster on large data.
  • Store row numbers or object references as values so you can jump back to the source after deduplicating.
  • Use dict.CompareMode = BinaryCompare (0) when case matters, such as product codes.
  • For portability, prefer late binding if the workbook will be opened on Mac or shared widely; you lose IntelliSense but avoid reference errors.
  • Wrap the population loop in Application.ScreenUpdating = False and restore it afterwards for large ranges.

Troubleshooting

  • Compile error: User-defined type not defined. The Scripting Runtime reference is missing. Add it via Tools > References, or change the declaration to As Object and use CreateObject.
  • Run-time error 457: This key is already associated with an element of this collection. You used Add with a duplicate key. Check with Exists first, or use the assignment form dict(key) = value.
  • Run-time error 32811: Element not found. You tried to Remove a key that is not present. Guard with Exists.
  • Run-time error 424: Object required. You declared the variable but did not Set it before use.
  • Code works on Windows but fails on Mac. Early binding may not resolve. Switch to late binding with CreateObject("Scripting.Dictionary").
  • Unexpected empty keys appearing. You read a missing key with dict(key). Add Exists checks or set the dictionary's default to not add on read by checking first.

Frequently Asked Questions

What is a Scripting Dictionary used for in Excel VBA?

It stores unique keys mapped to values, making it ideal for deduplicating lists, building fast lookup tables, counting occurrences, and grouping data without repeated range scans.

Do I need to add a reference to use it?

Only for early binding. Late binding with CreateObject("Scripting.Dictionary") works without any reference and is more portable, including on Mac Excel.

Are dictionary keys case-sensitive?

Yes by default. Set CompareMode to TextCompare before adding keys to make lookups case-insensitive.

How do I loop through all keys and values?

Use For i = 0 To dict.Count - 1 and read dict.Keys()(i) and dict.Items()(i). Both arrays are zero-based.

Can I store objects in a dictionary?

Yes. Values can be any variant, including ranges, arrays, or class instances. Use Set dict(key) = object when storing objects.

Why does reading a missing key not raise an error?

By default the dictionary adds the key with an empty value on read. Check Exists first to avoid accidental additions.