如何在 Excel VBA 监视窗口中监视字典中的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9803708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:35:45  来源:igfitidea点击:

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

excelvbaexcel-vbams-officeaccess-vba

提问by tyrex

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.

我通过dict As New Dictionary(并添加对脚本运行时的引用)在 Excel VBA 中使用字典。当我在调试期间尝试监视这些时,我只能看到字典中的键,而看不到每个键的相应值。

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 Objectand 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.

使用全局变量Dim d_obj As Object并不断监视它,每当我需要查找字典的值时Set d_obj(key) = ...,我都会在即时窗口中键入,然后我将能够在监视窗口中看到该值。

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!

另外我可以做的是编写一个函数,该函数接受字典并将值作为列表返回,并在直接窗口中类似地使用此函数。谢谢大家!

回答by Nick

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

我通常dict.items在即时窗口中键入,选择它并使用 Shift+F9 将其插入到监视窗口中。

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

或者,这是直接窗口的单行代码,用于列出所有项目:

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

回答by neilt17

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