+ Reply to Thread
Results 1 to 27 of 27

Build named variable dynamically

  1. #1
    Forum Contributor
    Join Date
    07-16-2007
    MS-Off Ver
    Excel 2003 and Excel 2016
    Posts
    178

    Build named variable dynamically

    Hello. Not sure if this is possible but I'll give it a try. Let's say I have boolean variables named tab1, tab2, tab3, ... tabn. I know the code below won't work but is it possible to check the status of these boolean variables with a loop such as:
    Please Login or Register  to view this content.
    Thanks.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Build named variable dynamically

    Use an array:

    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    07-16-2007
    MS-Off Ver
    Excel 2003 and Excel 2016
    Posts
    178

    Re: Build named variable dynamically

    Thanks for your help. I should have been more specific. My defined boolean variables are actually something like sn_fs where 'sn' is the name of a sheet. So there is an existing boolean variable for each sheet, and that variable is set somewhere in the code. So now I need to construct the boolean variable by combining the sheet name and the literal "_fs", and then check that variable for true/false.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Build named variable dynamically

    AFAIAA, you cannot do what you want to do in VBA. Perceived wisdom suggest using an array or a Collection.

    As a workaround, you could have two arrays. The first would contain a list of sheet names. You could build a text string from your sheet name and match it to the first array. Then, you could update/check the status in the second array.

  5. #5
    Forum Contributor
    Join Date
    07-16-2007
    MS-Off Ver
    Excel 2003 and Excel 2016
    Posts
    178

    Re: Build named variable dynamically

    That's what I thought. Thanks.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Hi,

    If you were to declare your variables within a class, you might use CallByName to access the variables by name.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Build named variable dynamically

    @xlnitwit: can you provide a worked example that meets the OP's stated requirement to build a variable name so that he can loop through them and test the values for True or False?

  8. #8
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Certainly. Using ThisWorkbook to host the code as an example class

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Thanks Mr. xlnitwit
    I tried to test the code but I got an error at this line
    Please Login or Register  to view this content.
    Run time error 438
    Object doesn't support this property or method
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Hi,

    The code must be in the ThisWorkbook module. Is that where you put it?

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,364

    Re: Build named variable dynamically

    The code has to be in the ThisWorkbook Class Module, not a Standard/Normal module.

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Thanks a lot for help
    Can I put the code in standard module ..
    I tried the following but got the same error
    Please Login or Register  to view this content.

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Quote Originally Posted by YasserKhalil View Post
    Can I put the code in standard module ..
    The variables must be declared in a class module. They cannot be in a standard module.

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Is this because of using CallByName ?!!
    I need to know more about this function (CallByName)

  15. #15
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Yes it is. CallByName allows one, as the name implies, to call a member, property or method of an Object using the name.

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Thanks for reply and for great information
    Why the code in post #12 failed ?! I refer to ThisWorkbook Object in CallByName Function ..??

  17. #17
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Because the variables were not in the ThisWorkbook module and thus were not members of the object you were using.

  18. #18
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Did you mean these variables?
    Please Login or Register  to view this content.
    They are declared as public so it is supposed that it can be used at any module

  19. #19
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    They can be used in any module, but they are not members of the ThisWorkbook object unless they are in the ThisWorkbook module. If they are not members of the object, they cannot be accessed using the CallByName function.

  20. #20
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Thank you very much for your patience and for great illustration
    Regards

  21. #21
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    You're most welcome.

    A simple illustration that may also assist is to use Intellisense. If you were to type ThisWorkbook followed by a full stop, you would see a list of all the things you can access for that object when using CallByName.

  22. #22
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Last point .. I have do some search to learn more about CallByName
    I have this code
    Please Login or Register  to view this content.
    I got error at this line
    Please Login or Register  to view this content.
    How can I get the item using CallByName?

    I tried various typos for 1 .. "1" .. (1) >> but all failed

  23. #23
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Item is a method of a Collection rather than a property, therefore you must use VbMethod
    Please Login or Register  to view this content.

  24. #24
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    Really awesome
    I was confused because of the comment line which referred to "Item" as property
    Thanks a lot for great help
    Best Regards

  25. #25
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    Did you not write the comments?

  26. #26
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Build named variable dynamically

    No I got the code from this link (VB code)
    https://msdn.microsoft.com/en-us/lib...=vs.90%29.aspx

    This is the original code
    Please Login or Register  to view this content.
    I tweaked it a little to suit VBA

  27. #27
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Build named variable dynamically

    A Collection in VB is implemented slightly differently to VBA, it seems.

+ 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. Need to build a Macro that can dynamically add rows to a table based on criteria
    By vbanoobz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-05-2014, 06:39 PM
  2. Build a Dynamic Named Range from criteria.
    By sinspawn56 in forum Excel General
    Replies: 8
    Last Post: 01-15-2013, 04:40 AM
  3. build graph from named ranges
    By Chris79 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2012, 10:36 AM
  4. Transferring a dynamically named string variable to another string variable.
    By Concorde Rules in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-09-2012, 04:08 AM
  5. build If statement condition dynamically
    By newbie_vba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-13-2008, 02:05 PM
  6. Dynamically build a link
    By Hall in forum Excel General
    Replies: 1
    Last Post: 03-15-2006, 04:55 PM
  7. [SOLVED] Build a Named Range
    By AMDRIT in forum Excel General
    Replies: 2
    Last Post: 03-03-2006, 12:55 PM

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