+ Reply to Thread
Results 1 to 8 of 8

Assigning values to names in a for next loop

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Assigning values to names in a for next loop

    Hi,

    I need a little help here understanding how to write the below loop to assign values to the names created in the loop without using brackets in these names.

    From what i understand the following would create Freq(1), Trans(1), Delivery(1), Freq(2), Trans(2), Delivery(2)........

    I want to create Freq1, Trans1, Delivery1, Freq2, Trans2, Delivery2........

    Please Login or Register  to view this content.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assigning values to names in a for next loop

    I'm not sure why you wouldn't just create a single array with 3 fields per row, insert all values in the array all at once. This code shows how to instantly create an 3 column array of the 25 rows starting at the activecell, then an example of how easy it is to address a certain 'row' in the array, in this instance row 22:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Assigning values to names in a for next loop

    I've not used arrays before, would be something to use if starting from scratch, but the names without brackets above are used in several places in the code I'm adapting , so would have a lot of work to do to use MyArr(xx, x) in their place.

    Was really trying to understand why in the original code I can't seem to get the first part of the name and the value i without brackets. Perhaps its something simple I'm missing.
    Last edited by JBeaucaire; 03-09-2013 at 11:35 AM. Reason: Removed pointless QUOTE, just adds clutter. Use REPLY instead.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What you want to do isn't possible.
    If posting code please use code tags, see here.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Assigning values to names in a for next loop

    Then just dim your variant at the start big enough to hold all the variables you want. In this example I started with 25 rows, then showed an example of searching every used cell in column C for the value "M", and filled in a row of the array with the values of A:C whenever a row is found. Then at the end, the value of "i" is also the last row in the array with any data, useful for later.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Assigning values to names in a for next loop

    Hi JBeaucaire & Norrie,

    I guess what I should have asked is there a way to Concatenate a Variable name on the left hand side of the equals sign.

    For my current problem, I had to bite the bullet and edit the existing code to use "( )" around the value of "i" but a task made easier using the find and replace functions.

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

    Re: Assigning values to names in a for next loop

    No it isn't possible.

    Is there a reason you don't want to use arrays?

  8. #8
    Registered User
    Join Date
    02-14-2013
    Location
    London
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    15

    Re: Assigning values to names in a for next loop

    Quote Originally Posted by Norie View Post
    No it isn't possible.

    Is there a reason you don't want to use arrays?
    Was trying to avoid having to change a lot of variable names to tidy up some existing code.

+ 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