+ Reply to Thread
Results 1 to 14 of 14

Dynamic variable name not working

  1. #1
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Dynamic variable name not working

    Hi

    I know there's several other threads about this, however I can't seem to find a solution on what i'm doing wrong.

    Please help me find out what is wrong with below code:

    Please Login or Register  to view this content.
    I get the error message "The subcript is out of range"

    The Whole code is written in a userform, and is executed when a commandbutton is clicked.

    Thanks for helping out

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Dynamic variable name not working

    Please Login or Register  to view this content.
    You declare varLookup with an upper bound of 5, then try to refer to element number 11 ...

  3. #3
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Re: Dynamic variable name not working

    Then this should work? But it doesn't :-/

    Please Login or Register  to view this content.

  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Dynamic variable name not working

    Error occurs on which line of code?


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  5. #5
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Re: Dynamic variable name not working

    In this one:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Dynamic variable name not working

    No, it won't work. Your array has 5 elements (Regardless of the index number of the lowest) - you've just moved the problem from 1 side of the = sign to the other as now index number 1 does not exist.

    I have no idea what the code is supposed to do so can't offer any suggestions.
    Last edited by cytop; 05-11-2016 at 04:10 AM.

  7. #7
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Dynamic variable name not working

    Quote Originally Posted by Desauv86 View Post
    Then this should work? But it doesn't :-/

    Please Login or Register  to view this content.
    How you can expect it to work?

    varLookup(i) is depends on For i = 1 To 5 (i.e.) 1 to 5 but your declaration is Dim varLookup(11 To 15) As Integer. So there is no 1 to 5 declaration in varLookup.

    This part varLookup(i + 10) will work since it has been covered by this declaration Dim varLookup(11 To 15) As Integer.

    So your above quoted code contains a contradictory logic

  8. #8
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,019

    Re: Dynamic variable name not working

    Quote Originally Posted by Desauv86 View Post
    Then this should work? But it doesn't :-/

    Please Login or Register  to view this content.
    That won't work too, because when i=1 there is no varLookup(1).
    Maybe this is what you want?
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Re: Dynamic variable name not working

    I have 5 public variables named varLookup11, 12, 13, 14, 15...

    I want them to be included in the first code I posted, just like e.g the "Me.Controls("txtPris" & i)

    Please Login or Register  to view this content.
    So basically i need the variable to be dynamic just like the "Controls".

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Dynamic variable name not working

    While you can refer to a control by 'assembling' it's name from some variables or constants, you cannot refer to a variable like that in VBA.

    If you really did declare varLookup as an array, you can refer to that using a counter
    Please Login or Register  to view this content.
    But, only having a small snippet of code out of context, it is difficult to suggest a solution especially considering the names assigned to your existing variables - why varLookup11, 12, 13 ...?
    Last edited by cytop; 05-11-2016 at 04:34 AM.

  11. #11
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Re: Dynamic variable name not working

    Below is the entire code for that Sub.


    Please Login or Register  to view this content.
    I could do below for every variable varLoolup11, varLoolup12, varLoolup13 etc..., I just wanted to make the code more simple. Because I might be adding more variable

    Please Login or Register  to view this content.
    I don't know howelse to describe the problem.

    But thanks for trying to help

  12. #12
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,019

    Re: Dynamic variable name not working

    If I understand you correctly:
    What you need from the public variables are the values in it not the variable itself. So why not use an array to store that values and then use that array in the loop.
    Something like this:
    Please Login or Register  to view this content.
    Notes: because arr1 index begin with 0 (the default) so it is 'arr1(i - 1)'

  13. #13
    Registered User
    Join Date
    11-25-2015
    Location
    Copenhagen
    MS-Off Ver
    2010
    Posts
    26

    Re: Dynamic variable name not working

    That's Works really nice

    Thanks so much for your reply.

  14. #14
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,019

    Re: Dynamic variable name not working

    Quote Originally Posted by Desauv86 View Post
    That's Works really nice

    Thanks so much for your reply.
    You're welcome & thanks for the feedback

+ 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. Dynamic Variable Names
    By Summerlover in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-19-2015, 01:00 PM
  2. Dynamic Variable Names
    By Summerlover in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2015, 09:27 AM
  3. Need help to get the dynamic Variable
    By harpyaph in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-28-2014, 04:28 AM
  4. [SOLVED] Dynamic Variable Value -- HELP!
    By Thomo88 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2014, 07:15 AM
  5. dynamic vlookup using variable
    By gammaman in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-23-2013, 10:21 AM
  6. Dynamic Variable identification
    By Armado159 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2012, 04:27 PM
  7. help with making a dynamic variable
    By squeekee35 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2011, 11:01 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