使用用户输入键填充数组

时间:2017-06-16 06:57:12

标签: vba

我需要编写一个数组,该数组可以存储来自使用键和另一个变量作为值的输入值

所以我想要实现的一个真正简单的例子是:

Sub addValuesToArray()

 Dim aRandomVar as String
 aRandomVar = "test"

 Dim myArray() as String

 userInput = inputBox("How do you want to call this variable")
 myArray(userInput) = aRandomVariable

End sub

然而,运行它会给我一个9型错误。关于我应该改进什么的想法?

1 个答案:

答案 0 :(得分:1)

我这样使用dictionary

Sub addValuesToArray()
    Dim aRandomVar As String, dic As Object

    Set dic = CreateObject("Scripting.Dictionary")
    aRandomVar = "test"

    userinput = InputBox("How do you want to call this variable")

    dic.Add userinput, aRandomVar

    For Each Key In dic.Keys
        Debug.Print "Key: " & Key & " Value: " & dic(Key)
    Next
End Sub