+ Reply to Thread
Results 1 to 9 of 9

Append Number to Variable Name?

  1. #1
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    Append Number to Variable Name?

    Hello,
    My question is about appending a number to a variable name.

    I know you can append both a number and variable's value to a string. I do this in the message box in my example below.

    However, is there any way to append a number to a variable name?

    My code for doing so in the following example does not work. But I was wondering if there was any possible way. The following example demonstrates a scenario in which it would be helpful.

    Please Login or Register  to view this content.
    Last edited by CrazyFileMaker; 05-14-2010 at 12:48 AM.

  2. #2
    Forum Contributor PingPing's Avatar
    Join Date
    02-19-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    158

    Re: Append Number to Variable Name?

    I could be wrong, but I don't think you can change the name of a memory location's pointer in VBA.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Append Number to Variable Name?

    You can't do that. What you would do is use an array in the first place.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Append Number to Variable Name?

    Hi,

    It's not clear, at least to me, what result you expect to see in the message box.

    There seem to be one or two problems at first glance but it's difficult to comment without knowing exactly what you want.

    For instance your initial variable declaration only declares sName3 as a string, the other two are variants - which may not matter in this case but can give unexpected results. You can declare several variables on the same line with one Dim statement, but each one should be explicitly defined. e.g

    Dim st1 As String, st2 As String, iNo as Integer ....etc

    Then your line ReDim Preserve sNameArray(i) As String seems to be trying to change the (numeric) dimensions but as a string. Then in the next line sName hasn't been defined, neither for that matter has i.

    Tell us the result you expect and no doubt we can help. If this is a single example and there are other variables in the real world enhance your example to cover at least two or three situations.

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile Re: Append Number to Variable Name?

    Hi CrazyFileMaker

    Just as a point of interest, your line :
    Please Login or Register  to view this content.
    would only type varaible sName3 as string. You must type each one individually, thus :
    Please Login or Register  to view this content.
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  6. #6
    Registered User
    Join Date
    10-28-2008
    Location
    Guildford, UK
    Posts
    5

    Re: Append Number to Variable Name?

    Had the same problem a while ago - turns out the solution is pretty trivial once you find out about the Evaluate() function

    Please Login or Register  to view this content.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Append Number to Variable Name?

    Did you actually try that? It won't work.

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Append Number to Variable Name?

    Maybe

    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  9. #9
    Registered User
    Join Date
    03-31-2009
    Location
    Auburn Alabama
    MS-Off Ver
    Microsoft Office Excel 2007 (Windows)
    Posts
    99

    Re: Append Number to Variable Name?

    Hello all,
    My thanks to all of you for your helpful feedback!

    I apologize for the hurried code and my faulty string declaration. The program in which I faced this problem is much larger, the situation more complex. I was trying to simplify both in an effort to be clear, but did so in haste.

    On another note, Pike's example was just what I was looking for! I won't bore you with a lengthy example as to why an array wouldn't suffice in this case - I'll just say that it would have cost me much time in the way of recoding my modules. On top of that, Pike's helpful example has also greatly reduced the final line count in my code!

    Thanks again to all of you! And I think I owe Pike a special thanks for his outside-of-the-box solution! Thanks Pike!
    Last edited by CrazyFileMaker; 05-14-2010 at 12:44 AM.

+ 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