vba 在excel vba中将集合项打印到即时窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28202869/
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-12 06:00:57  来源:igfitidea点击:

printing a collection item to immediate window in excel vba

excel-vbavbaexcel

提问by David

I was wondering how do I print an item in my collection to the immediate window in excel VBA? I want to either have a collection for each collection item or an array for each collection item, which ever is easier to pull information from. Here is some example code of what I'm talking about

我想知道如何将我收藏中的项目打印到 Excel VBA 中的即时窗口?我想为每个集合项创建一个集合,或者为每个集合项创建一个数组,哪个更容易从中提取信息。这是我正在谈论的一些示例代码

Sub test()

Dim c As Collection
Dim a As Collection

Set a = New Collection

For i = 1 To 10
    Set c = New Collection
    c.Add Array("value1", "value2", "value3","valvue4, "value5"), "key1"
    c.Add "value2", "key2"
    c.Add "value3", "key3"
    c.Add "value4, "key4"
    c.Add "value5", "key5"

    a.Add c, c.Item(1)    

'lets say I wanted to print value4 or value1 from the 1st item
Debug.Print a.Item(1(2))

Next i

End Sub

采纳答案by Frank

To add to @Gary's Student's answer, you can't use integers as keys for a collection. So you either cast them to a string using the Cstr function or you can use a dictionary instead. If you decide to use a dictionary, make sure to enable the Microsoft Scripting Runtime (under tools -> references). I've added some examples below.

要添加到@Gary 的学生答案中,您不能使用整数作为集合的键。因此,您可以使用 Cstr 函数将它们转换为字符串,也可以改用字典。如果您决定使用字典,请确保启用 Microsoft Scripting Runtime(在工具 -> 参考下)。我在下面添加了一些示例。

    Sub collExample()
        Dim i As Integer
        Dim c As Collection

        Set c = New Collection
        For i = 1 To 10
            c.Add 2 * i, CStr(i)
        Next i
        'keys cant be integers
        'see https://msdn.microsoft.com/en-us/library/vstudio/f26wd2e5(v=vs.100).aspx

        For i = 1 To 10
            c.Item (i)
        Next i
    End Sub

    Sub dictExample()
        Dim d As New Dictionary
        Dim i As Integer

        For i = 1 To 10
            d(i) = 2 * i
        Next i

        Dim k As Variant
        For Each k In d
            Debug.Print k, d(k)
        Next k

        Dim coll As New Collection
        coll.Add "value1"
        coll.Add "value2"
        coll.Add "value3"

        Set d("list") = coll

        Dim newCol As Collection
        Set newCol = d("list")

        Dim v As Variant
        For Each v In newCol
            Debug.Print v
        Next v

    End Sub

回答by Gary's Student

This seems to work:

这似乎有效:

Sub ytrewq()
    Dim c As Collection
    Set c = New Collection
    c.Add "x", CStr("x")
    c.Add "y", CStr("y")
    c.Add "z", CStr("z")
    i = 2
    MsgBox c.Item(i)
    Debug.Print c.Item(i)
End Sub