+ Reply to Thread
Results 1 to 5 of 5

Thread: Call variable from String

  1. #1
    Registered User
    Join Date
    06-03-2010
    Location
    Bogotá, Colombia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Call variable from String

    Hi,

    I'm trying to do something like the indirect function on the sheets but on VBA. I have a variable that it's value is a String and it's the name of another variable and I'm trying to call the variable.

    Ex.

    Sub a()

    Dim SM
    Dim name

    SM = 1

    name = SM

    msgbox("name")

    End Sub

    I'm expecting a message box with the value of the variable SM but I don't know how I can do this or if it is even possible.

    Thanks ,

    juancamilo87

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,175

    Re: Is it posible to call variable from String

    Sub a()
    Dim SM
    Dim name
    SM = 1
    name = SM
    MsgBox name
    End Sub

    This code will display a messagebox with the prompt = 1

    Regards

  3. #3
    Registered User
    Join Date
    06-03-2010
    Location
    Bogotá, Colombia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Is it posible to call variable from String

    I didn't know that work.

    Actually what I'm needing is this.

    Sub d()

    dim name23
    dim sm

    name23 = 2
    sm = "e23"


    msgbox "nam"&"e23"


    End sub

    And I'm looking for the result 2.

    Thanks

  4. #4
    Registered User
    Join Date
    01-19-2011
    Location
    Houston, TX
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Call variable from String

    maybe if you described the problem you are trying to solve, a solution would present itself.
    The combining strings as variables names just doesn't sound like the real problem.

  5. #5
    Forum Guru mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,984

    Re: Call variable from String

    When using variables do not enclose them within "
    msgbox name23 & sm
    or
    msgbox name23 + sm
    or to put a space between the variables
    msgbox name23 & " " & sm
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0