How to monitor the values in a Dictionary in the Excel VBA watch window? How to monitor the values in a Dictionary in the Excel VBA watch window? vba vba

How to monitor the values in a Dictionary in the Excel VBA watch window?


I usually type dict.items into the immediate window, select it and go Shift+F9 to insert it into the watch window.

Alternatively, here's a one-liner for the immediate window, to list all items:

for each i in dic.Items: debug.Print i: next


I use a recursive function which can be used to display all simple type variables and the contents of all nested dictionaries in the watch window. This produces output in the form:

Fred:rabbit; Tiddles:cat; Fluffy:cat; Food:[1:lettuce; 2:biscuits; ]; 

where keys and values are separated by ":", items are separated by "; " and nested dictionaries are shown in square brackets.

Public Function DictionaryContents(ByVal dcDictionary, Optional ByVal boolShowKeyIndex As Boolean = False)  Dim Keys  Keys = dcDictionary.Keys  Dim i As Long  Dim stIndex As String  Dim stOutput As String  stOutput = vbNullString  For i = 0 To dcDictionary.Count - 1    If boolShowKeyIndex Then      stIndex = "(" & i & ")"    End If    stOutput = stOutput & Keys(i) & stIndex & ":"    If IsObject(dcDictionary(Keys(i))) Then      stOutput = stOutput & "[" & DictionaryContents(dcDictionary(Keys(i)), boolShowKeyIndex) & "]"    Else      stOutput = stOutput & dcDictionary(Keys(i))    End If    stOutput = stOutput & "; "  Next i  DictionaryContents = stOutputEnd Function