Can you use a variable value in a different variable's name?

Anonymous
2025-05-19T14:29:07+00:00

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"

Microsoft 365 and Office | Access | Other | Other

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.

0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. HansV 460.6K Reputation points MVP Volunteer Moderator
    2025-05-19T15:13:12+00:00

    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)
    
    0 comments No comments
  2. Duane Hookom 26,280 Reputation points Volunteer Moderator
    2025-05-19T15:17:37+00:00

    I tried several iterations of [removed_js] and wasn't able to find a solution.

    0 comments No comments
  3. Anonymous
    2025-05-19T19:39:03+00:00

    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

    1 person found this answer helpful.
    0 comments No comments