Wauvel

Excel like a finance pro.

← The library

VBA · Variables

Collection and Dictionary

Containers for lists and key-value pairs; Dictionary (Scripting.Dictionary) gives fast lookups by key.

OccasionalDifficulty 1450 · Advanced
Practice · 2 questions →

When to use it

A Collection is an ordered list of items; a Dictionary (from the Scripting runtime) is a key-value store with fast Exists checks. Dictionaries replace slow lookups and duplicate checks in loops.

The code

Code
Set d = CreateObject("Scripting.Dictionary")
d("West") = 120
If d.Exists("West") Then ...

Worked examples

  • Dictionary basics

    Set d = CreateObject("Scripting.Dictionary") d("West") = 120 If d.Exists("West") Then MsgBox d("West") A keyed store with an existence test

    Late binding needs no reference.

  • Group by key

    For i = 2 To lastRow k = Cells(i, 1).Value d(k) = d(k) + Cells(i, 2).Value Next Sums by key in one pass, a SUMIF in memory

    Group totals.

  • Collection

    Dim c As New Collection c.Add "a": c.Add "b" MsgBox c.Count A simple list

    Collections are 1-based.

Worth knowing

  • Set a reference to Microsoft Scripting Runtime for early binding and autocomplete.
  • d.Keys and d.Items return arrays for writing to a sheet.
  • CompareMode = vbTextCompare makes keys case-insensitive; set it before adding.

Where it goes wrong

  • Reading d(k) for a missing key silently creates it.
  • Collections cannot test for a key without an error handler.

Related

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.