Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using dictionaries in Excel VBA via dict As New Dictionary (and adding a reference to the scripting runtime). When I try to monitor those during debugging, I can only see the keys which lie in the dictionary, but not the respective value of each key.

Is there any way to see the value as well? It would make debugging much more easy for me.

EDIT: Based on your answers, there is no easy solution, but I can do the following.

Use a global variable Dim d_obj As Object and monitor it constantly and whenever I need to look up a value of a dictionary, I type into the immediate window Set d_obj(key) = ... and I will be able to see the value in the monitor-window.

What I may do in addition is write a function which takes in a dictionary and returns the values as a list and use this function similarly at the direct window. Thx to all!

like image 885
tyrex Avatar asked Mar 21 '12 11:03

tyrex


People also ask

How do I use the watch window in Excel VBA?

To add a Watch expression, select Add Watch under the Debug menu. When the Add Watch window appears, enter the expression to watch and click the OK button when you are done. Next, we've selected AlphaNumeric as the Procedure and Module1 as the Module when setting up the Context for the watched expression.

How do I view values in VBA?

On the Debug menu, choose Add Watch. The Add Watch dialog box is displayed. If an expression is already selected in the Code window, it is automatically displayed in the Expression box. If no expression is displayed, enter the expression you want to evaluate.

Which window of VBA debugging displays the value of all constants and variables?

The Watch window shows the current values of selected variables or expressions and the current property settings for selected objects. You can use the Watch window to monitor the status of variables and objects as you step through a procedure.


2 Answers

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
like image 184
Nick Avatar answered Sep 25 '22 13:09

Nick


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 = stOutput

End Function
like image 44
neilt17 Avatar answered Sep 23 '22 13:09

neilt17