
That would not work, but you might create an array:
Dim v(1 To 2) As String
Dim i As Long
v(1) = "Test"
v(2) = "Hello"
i = 2
Debug.Print v(i)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Howdy. I'm pretty sure the answer is no, but I thought it would be worth asking just in case I'm not searching the right way.
Can you use a variable value in a variable name? Like...
v1 = "Test"
v2 = "Hello"
i = 2
Debug.Print v & i outputs "Hello"
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
That would not work, but you might create an array:
Dim v(1 To 2) As String
Dim i As Long
v(1) = "Test"
v(2) = "Hello"
i = 2
Debug.Print v(i)
I tried several iterations of [removed_js] and wasn't able to find a solution.
As the others have said, you'd need to use an array or dictionary to achieve this
Dim v(1 To 2) As String
Dim i As Integer
v(1) = "Test"
v(2) = "Hello"
i = 2
Debug.Print v(i) ' => Hello
OR
Dim dict As Object
Dim i As Integer
Set dict = CreateObject("Scripting.Dictionary")
dict("v1") = "Test"
dict("v2") = "Hello"
i = 2
Debug.Print dict("v" & i) ' => Hello
Set dict = Nothing