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

前端 未结 2 1852
鱼传尺愫
鱼传尺愫 2020-12-29 05:58

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,

相关标签:
2条回答
  • 2020-12-29 06:36

    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
    
    0 讨论(0)
  • 2020-12-29 06:37

    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
    
    0 讨论(0)
提交回复
热议问题