+ Reply to Thread
Results 1 to 6 of 6

Call a constant/variable by creating another variable

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    2

    Call a constant/variable by creating another variable

    Hi. I am trying to run through a series of constants based on filed names.
    For example, I have x number of Constant field names named Field1, Field2, Field3, .......,Fieldx.
    I would like to do something with the value of those field names without having to state the actual field name, but rather cycling through the fields

    For Example:

    Dim Record as integer

    For Record=1 to x' which is the number of fields

    OtherVariable=Field & Record 'I would like to use this to call the value of the constants

    Next Record
    '----------------------------------------------

    This does not work. I am sure I need to do something to tell vba that "Field & Record" is actually a constant/variable and on text/.string.

    Can anyone help?

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Call a constant/variable by creating another variable

    It sounds like you need an array variable which stores multiple variables and loop through them easily.

    Please Login or Register  to view this content.
    Elegant Simplicity............. Not Always

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Where are these fields located?
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    01-22-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Call a constant/variable by creating another variable

    The fields are constants before the sub routines.
    I am not sure I explained this well before. I will give another example:
    Say I define a constant:
    Public Constant Field1=2

    What I now want to do is call this constant value in a sub routine, but I do not want to explicity call field1.
    I would like to call this constant value by calling "Field" & "1".
    I imagine there is a function that will convert this tect value of "Field1" to recognise that this is the constant being referred to.

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,821

    Re: Call a constant/variable by creating another variable

    I'm going to agree with AndyLitch and suggest that what you want is an array. I don't know all the nuances of dealing with array constants defined outside of procedures, so there may be other ways to look at this. A structure like this might work
    Please Login or Register  to view this content.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Call a constant/variable by creating another variable

    What to do these constants refer to?

    Is it columns?

    You can't refer to variables/constants by concatentating to get there name so using an array as Andy suggested might be the best idea.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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