创建一个字符串数组,其中索引"value"是字符串



已经在论坛中查找,但找不到类似的东西。

我想创建一个字符串数组,其中索引"值"是一个字符串。

例如:

MyArray(“abc”)=1
MyArray(“def”)=2
MyArray(“ghi”)=3

有没有办法在 VBA 中做到这一点,或者我可以用不同的方式实现相同的目标吗?

字典介绍

一个简单的例子

Sub DictIntroduction()
Dim InitText As Variant, InitNumbers As Variant
InitText = Array("abc", "def", "ghi")
InitNumbers = Array(1, 2, 3)
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim i As Long
For i = 0 To UBound(InitText)
dict.Add InitText(i), InitNumbers(i)
Next
Dim Key As Variant
For Each Key In dict.Keys
Debug.Print Key, dict(Key)
Next
Debug.Print dict("abc")
Debug.Print dict("def")
Debug.Print dict("ghi")
End Sub

要了解有关该词典的更多信息,请访问以下链接:

Excel VBA 词典 - 完整指南 (文章(

Excel VBA Dictionary (YouTube Playlist(

Sub main()
Set myArray = CreateObject("Scripting.Dictionary")
myArray("abc") = 1
myArray("def") = 2
myArray("ghi") = 3

Debug.Print myArray("abc")

For Each tempKey In myArray
Debug.Print tempKey, myArray(tempKey)
Next
End Sub

最新更新