+ Reply to Thread
Results 1 to 16 of 16

Get variable value by passing a variable name

  1. #1
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    452

    Get variable value by passing a variable name

    I know that subject won't make much sense but I couldn't word it any better :-(

    I have several different variables
    PopulationLondon
    PopulationNewYork
    PopulationTokyo

    Depending on the current city that my code is working with, I would like to get the variable value. I have tried

    Please Login or Register  to view this content.
    But it doesn't give me the value of PopulationLondon. Could anyone help with that please?
    Last edited by johncassell; 07-22-2020 at 12:20 PM. Reason: tags

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 2016
    Posts
    4,907

    Re: Get variable value by passing a variable name

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Also, the code in more context would be very helpful.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    You must be doing something wrong .... it should work.

    Example:

    Please Login or Register  to view this content.
    Probably, the rest of your original code is causing the problem.

  4. #4
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    452

    Re: Get variable value by passing a variable name

    Thanks, sorry but my code above is *exactly* like my code is at the minute. All it does is pop up with the word 'Population' and not 100000 as I would like

  5. #5
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    You have changed the code in your first message.

    Now, change to this:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 2016
    Posts
    4,907

    Re: Get variable value by passing a variable name

    And currentcity is just blank in your original post:

    Please Login or Register  to view this content.
    London doesn't have quotes around it, so it is being treated as a variable (with no value assigned)

    CurrentCity = "London"

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 2016
    Posts
    4,907

    Re: Get variable value by passing a variable name

    Reading deeper, I see what you are trying to do, but it can't be done that way. Maybe this approach will do what you want:

    Please Login or Register  to view this content.
    p.s. thanks for adding code tags to your post!

  8. #8
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    As an alternative to Arkadi's solution, using "Type Structure" :

    Please Login or Register  to view this content.

  9. #9
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 2016
    Posts
    4,907

    Re: Get variable value by passing a variable name

    @Haluk, another nice option

  10. #10
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    Thanks Arkadi ...

  11. #11
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    452

    Re: Get variable value by passing a variable name

    Thanks again for the replies. haluk, I tried your code but was getting errors (such as User-defined type not defined on xData(1 to 3))

    Arkadi, thanks for your code but that doesn't take into consideration the fact that sometimes I don't want the 'population' value from the City (and sorry I now realise I should have included that info). Sometimes I want the a different value (such as the size of the city). Hopefully this code explains better.

    Please Login or Register  to view this content.
    Last edited by johncassell; 07-23-2020 at 04:09 AM.

  12. #12
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    @johncassell ;

    Seems, you have not placed the following at the top of the module:

    Please Login or Register  to view this content.
    Try the attached sample workbook...
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    452

    Re: Get variable value by passing a variable name

    Ahh, I think I had that half way down my code (under my sub). My bad. Your code does work but as I explained there are times when I don't just need the population of the city, I might need the temperature of the city. I've attached my file to test, it doesn't give me the answer of the variable, it just gives me the name of the variable.
    Attached Files Attached Files

  14. #14
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    @johncassell ;

    My code needs to be revised according to your new needs as:

    Please Login or Register  to view this content.
    Sample workbook is also attached...
    Attached Files Attached Files

  15. #15
    Forum Contributor
    Join Date
    07-07-2005
    Location
    England
    MS-Off Ver
    2003 Pro
    Posts
    452

    Re: Get variable value by passing a variable name

    Perfect! Thank you so much that works!!! Really appreciate your help

  16. #16
    Valued Forum Contributor Haluk's Avatar
    Join Date
    02-14-2019
    Location
    Türkiye
    MS-Off Ver
    2010 - 64 Bit
    Posts
    706

    Re: Get variable value by passing a variable name

    You're welcome...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Passing a Formula and Variable AND evaluating the Formula with the Variable
    By lovecolorado in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-24-2020, 04:09 PM
  2. Passing a variable through a variable?
    By domgilberto in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2016, 08:51 AM
  3. Passing Variable from one sub to another
    By bbmak in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-27-2014, 12:39 PM
  4. passing variable name...
    By zara_toustra in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-18-2013, 01:15 PM
  5. Passing a variable from one sub to another sub
    By NRMDU in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2013, 04:45 PM
  6. passing variable?
    By leroiv in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-06-2008, 10:52 AM
  7. [SOLVED] Passing variable to VarType of other Variable
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2005, 10:06 AM

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.6.0 RC 1