VBA:字典项到字符串数组?

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

VBA: Dictionary items to string array?

arraysvbadictionarytype-conversion

提问by Lee White

What I am trying to do is fairly straightforward. I want to get a list of all items (values) in a Dictionary, and save them in an array of strings.

我正在尝试做的事情相当简单。我想获取 a 中所有项目(值)的列表Dictionary,并将它们保存在一个字符串数组中。

I'd guess this code would work:

我猜这段代码会起作用:

Sub PrintFilters(ByVal crit As Dictionary)
    Dim i() As String
    i = crit.Items()
    ' Do stuff with i
End Sub

However, I am getting a type mismatch on the third line. I am guessing that crit.Items()'s return value is some kind of list, an not an array. The MSDN pages do not mention what this method's return value's type is, though.

但是,我在第三行发现类型不匹配。我猜它crit.Items()的返回值是某种列表,而不是数组。不过,MSDN 页面没有提到此方法的返回值的类型是什么。

Is there a proper way to do this?

有没有合适的方法来做到这一点?

回答by Mailkov

I think is Variant type so try this:

我认为是 Variant 类型所以试试这个:

Sub PrintFilters(ByVal crit As Dictionary)
    Dim i As Variant
    i = crit.Items()
    ' Do stuff with i
End Sub

回答by Alex K.

If you need a string array you need to build one manually as .Itemsis a variant()

如果你需要一个字符串数组,你需要建立一个手动的.Itemsvariant()

Sub PrintFilters(crit As Dictionary)
    Dim key As Variant, i As Long

    ReDim items(crit.Count - 1) As String

    For Each key In crit.Keys()
        items(i) = crit(key)
        i = i + 1
    Next

    Debug.Print Join(items, ", ")
End Sub

回答by Guest1234-5678-9101112

Please find below 2 options achieving the same result either by handling the conversion directly as part of your main routine or by passing the dictionary to a function returning a String Array; a check in the VBA Locals Window indicates that arrStringis a variable of type "String(0 to 2)". Note that the array can be built with both the dictionary .Keysor .Items.

请在以下 2 个选项中找到实现相同结果的选项,方法是将转换直接作为主程序的一部分进行处理,或者将字典传递给返回字符串数组的函数;VBA Locals 窗口中的检查表明这arrString是一个“字符串(0 到 2)”类型的变量。请注意,可以使用字典.Keys.Items.

In the first example, the Joinfunction creates a string including all aDict.Itemsseparated by the "|"character. Then the Splitfunction breaks down that string into an array of strings where "|"acts as the delimiter used to create each array element.

在第一个示例中,该Join函数创建一个字符串,其中包含所有aDict.Items"|"字符分隔的字符串。然后该Split函数将该字符串分解为一个字符串数组,其中"|"充当用于创建每个数组元素的分隔符。

In both examples the actual conversion from Dictionary to String Array is achieved with 1 line of code.

在这两个示例中,从 Dictionary 到 String Array 的实际转换是通过 1 行代码实现的。

OPTION #1 - As part of the main routine

选项 #1 - 作为主程序的一部分

Sub Dictionary_to_StringArray()

   Dim aDict As Scripting.Dictionary
   Dim arrString() As String

   Set aDict = New Scripting.Dictionary
   aDict.Add "United Kingdom", "London"
   aDict.Add "France", "Paris"
   aDict.Add "United States of America", "Washington, D.C."

   arrString = Split(Join(aDict.Items, "|"), "|") 'this works equally with .Items & .Keys

   Set aDict = Nothing
   Erase arrString

End Sub

OPTION #2 - Conversion handled by dedicated Function

选项 #2 - 由专用函数处理的转换

Sub CallingProc()

   Dim aDict As Scripting.Dictionary
   Dim arrString() As String

   Set aDict = New Scripting.Dictionary
   aDict.Add "United Kingdom", "London"
   aDict.Add "France", "Paris"
   aDict.Add "United States of America", "Washington, D.C."

   arrString = Make_StringArray_From_Dictionary(aDict)

   Set aDict = Nothing
   Erase arrString

End Sub

Function Make_StringArray_From_Dictionary(ByVal SubmitDict As Scripting.Dictionary) As String()

   Make_StringArray_From_Dictionary = Split(Join(SubmitDict.Items, "|"), "|")

End Function