填充数组与用户输入键

问题描述:

我需要编写能够从使用存储的输入值的关键,另一个变量的值数组填充数组与用户输入键

所以我想什么是真正简单的例子来实现是:

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错误。对我应该改进什么有什么想法?

+0

首先'aRandomVariable'是从未宣布过!其次,你不能为键/值对使用数组。看看'收藏'... –

我会使用一个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 
+0

感谢您的快速回复! –