+ Reply to Thread
Results 1 to 9 of 9

Loop through non-continuous named range in vba

  1. #1
    Registered User
    Join Date
    06-14-2011
    Location
    New York, USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Loop through non-continuous named range in vba

    Is this possible?

    If I have a named range that covers A1:A10, A12:A15, A17:A20, can I use code that loops through? (Specifically, I'm using a "For each variable In variablelist.... Next".)

    I tried running it, and the code seems to end at A10 (last item in first segment of named range), so I'm assuming I either have to use a different type of loop, or some sort of workaround. It would be nice if these named ranges didn't have to be separated to be operated upon. (There are more segments than I listed)

    Thanks!
    Last edited by awan0126; 07-26-2011 at 02:27 PM.

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Loop through non-continuous named range in vba

    This structure will cycle through each area of the multi-area Named Range:
    Please Login or Register  to view this content.
    Does that get you headed in the right direction?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Loop through non-continuous named range in vba

    Hi awan0126, there are a couple of ways you can loop through.
    1. You can set your range
    Please Login or Register  to view this content.
    or, if you don't know the last row used, you can go with
    Please Login or Register  to view this content.
    Whole code:
    Please Login or Register  to view this content.
    EDIT: Sorry Ron to jump in, there was no answer when I was working on it!


    Regards:
    Last edited by Mordred; 07-26-2011 at 10:27 AM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

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

    Re: Loop through non-continuous named range in vba

    If the areas are irrelevant, and you really just want to use each "cell" in the whole named range once through the loop, something like:

    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!)

  5. #5
    Registered User
    Join Date
    06-14-2011
    Location
    New York, USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Loop through non-continuous named range in vba

    Thanks for so many responses! Really appreciated. I'm testing out the first suggestion now (with the areas)-- will respond after macro finishes running (takes a while).


    Jbeaucaire- unless I'm mistaken, I think the code I currently have is identical to the one you suggested (except instead of "Set rng = Range("MultiAreaRange"), I simply defined rng = Range("multiarearange").) Unless this makes the difference, the loop only went through the first segment of the range...

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop through non-continuous named range in vba

    I think JB is right: no need for areas (nor for variables if you use named ranges)

    Please Login or Register  to view this content.
    If you put a non-contiguous range into a Variant (array), it will only accept the first area.
    I you put it in an object variable (using Set =) the whole range will be accepted.
    Last edited by snb; 07-26-2011 at 10:44 AM.



  7. #7
    Registered User
    Join Date
    06-14-2011
    Location
    New York, USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Loop through non-continuous named range in vba

    You're all right- both codes work great! Thank you!!!

  8. #8
    Registered User
    Join Date
    06-14-2011
    Location
    New York, USA
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Loop through non-continuous named range in vba

    By the way-- for anyone looking to use this in the future: the "Set RNG = Range("MultiAreaRange")" is what made the difference. If you do not use it, only the first segment is called.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Loop through non-continuous named range in vba

    You are mistaken:
    Your question was about a named range.
    To loop through a named range you don't need a variable at all, like I showed you before:

    Please Login or Register  to view this content.

+ 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