Cat On A Spreadsheet
In the previous post, we explored how VBA dictionaries can simplify lookups, eliminate nested loops, and boost performance. But what if you want to group data across multiple dimensions? For example:
Region → Product Category → Sales Total
Department → Employee → Hours Worked
Year → Month → Sales Rep → Revenue
This is where multi-level (nested) dictionaries come in.
In this post, you’ll learn:
How to build and navigate multi-level dictionaries
Real-world use cases for grouping and aggregating
How to safely manage missing keys and create structure dynamically
Multi-level dictionaries let you store and access hierarchical data without relying on extra arrays or database queries. Instead of flattening your data, you can build a tree-like structure:
This is similar to how JSON or Python dictionaries work—and it's possible in VBA too.
Let’s say you have data in this format:
Region Product Sales
North A 100
North B 150
South A 200
We want to group by Region → Product → Total Sales.
Step 2: Loop through your data
Now your structure looks like this:
To read from this structure:
This time, we’ll group by two levels of time:
To make your code cleaner, abstract the creation of nested dictionaries into a helper function:
Usage:
Let’s say you want to print the values to the worksheet:
You can keep nesting dictionaries inside dictionaries for even deeper hierarchies:
✅ Best for:
Grouping and aggregating large datasets
Creating multi-dimensional reports
Replacing nested loops and fragile array logic
❌ Not ideal for:
Sorting (dictionaries are unordered)
Writing back to Excel in a pre-defined structure without flattening
Cat On A Spreadsheet