2017-06-16 113 views
0

我需要編寫能夠從使用存儲的輸入值的關鍵,另一個變量的值數組填充數組與用戶輸入鍵

所以我想什麼是真正簡單的例子來實現是:

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'是從未宣佈過!其次,你不能爲鍵/值對使用數組。看看'收藏'... –

回答

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 
+0

感謝您的快速回復! –