+ Reply to Thread
Results 1 to 6 of 6

Reference a Range Variable with a for next loop using the current iteration number

  1. #1
    Registered User
    Join Date
    04-16-2009
    Location
    Kilkenny, Ireland.
    MS-Off Ver
    Excel 365
    Posts
    47

    Reference a Range Variable with a for next loop using the current iteration number

    Hi, this is probably glaringly obvious/simple but I have not been able to figure it out.

    I've got some range and other variables for charts set as follows:

    Please Login or Register  to view this content.
    Later on then, I go to deploy these to build some charts within a For ... Next loop ( i = 1 to 3 )

    Please Login or Register  to view this content.
    The above shows just one failed attempt to call the first range variable, ChartSeriesName1, as the first step in building first chart.

    This one obviously converts the the set range variables to string variables and the plotting fails.

    I have tried several other iterations which I won't get into.

    So I believe I am missing a simple principle/technique to build each chart in turn by ref to the i in the loop, but I just can't find it.

    If anybody could help, would be very grateful.
    Last edited by Orson100; 02-17-2024 at 07:57 AM.

  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,477

    Re: Reference a Range Variable with a for next loop using the current iteration number

    I think, if you want to loop through the charts, you would be better off setting the parameters up in arrays rather than individual variables.

    For example (untested):

    Please Login or Register  to view this content.
    With this arrangement, you could create a loop to set up the array elements, if you wish (for example, if there are more than 3 )

    Once you have your arrays of parameters, you can loop through them.

    I've shown setting up a one dimensional array of names, but you could set up a two dimensional array and add the rest of the chart parameters in a single array.



    Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    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
    Registered User
    Join Date
    04-16-2009
    Location
    Kilkenny, Ireland.
    MS-Off Ver
    Excel 365
    Posts
    47

    Re: Reference a Range Variable with a for next loop using the current iteration number

    Many thanks for response Trevor.

    Yes setting up arrays was in the back of my mind failing a quick fix for my current setup.

    But I will go do so now, it will be worth it; I have many charts and multiple series with conditional parameters to compile and this approach is far more elegant and will provide much better structure and readability.

    Thanks again.
    Orson.

  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,477

    Re: Reference a Range Variable with a for next loop using the current iteration number

    You're welcome.



    Also, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  5. #5
    Registered User
    Join Date
    04-16-2009
    Location
    Kilkenny, Ireland.
    MS-Off Ver
    Excel 365
    Posts
    47

    Re: Reference a Range Variable with a for next loop using the current iteration number

    Oops, my bad, done now, thank you.

  6. #6
    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,477

    Re: Reference a Range Variable with a for next loop using the current iteration number

    Thank you

+ 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. Run an entire loop instead of iteration by iteration when stepping into code?
    By Norcal1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-24-2019, 04:20 PM
  2. [SOLVED] Go to next loop iteration if current loop has error
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-03-2016, 09:54 PM
  3. [SOLVED] change variable on every iteration in loop
    By JJGF in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2015, 10:00 PM
  4. [SOLVED] Insert a different named range into VBA loop with each iteration
    By Dr_Gzus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2013, 04:57 PM
  5. [SOLVED] For Each ... Next loop - need to reference the loop variable
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-13-2006, 01:15 PM
  6. [SOLVED] How to reference variable range in a loop
    By Samirkc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-13-2006, 11:15 AM
  7. [SOLVED] [SOLVED] Loop: Copy and paste range that changes with every iteration
    By Allen Geddes in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-15-2005, 09:10 AM

Tags for this Thread

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