+ Reply to Thread
Results 1 to 6 of 6

Looping Thru Cells in a Discontiguous Range

  1. #1
    Forum Contributor
    Join Date
    01-28-2008
    MS-Off Ver
    Excel 2016
    Posts
    151

    Looping Thru Cells in a Discontiguous Range

    I have a range, "MyRange", that consists of discontiguous cells, such as
    A1, C1, D1, E1. (All cells are in a single row.) I can use:

    For Each MyCell in MyRange
    ...
    Next MyCell

    However, I prefer to use MyRange.cells(1) or My.cells(1,1). This doesn't work. Any idea how to specify a value for the 3rd cell in MyRange?

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    You can't index a multi-area range. If the cells are always every other cell in a row, you can step by two across a contiguous range, or loop through the areas, or ...

    Please edit your post to add code tags.
    Last edited by shg; 12-10-2008 at 02:29 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Larry.LeBlanc@O,

    When referring to disparate ranges, you access each sub-range by using the Areas property. The first area is one. In your example, you would access the third item this way...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  4. #4
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Quote Originally Posted by Leith
    In your example, you would access the third item this way...
    ... assuming each area has exactly one cell.

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,575
    More work but this makes it a bit easier if you like arrays.
    Please Login or Register  to view this content.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Larry,

    If you are interested in a method to access cells regardless of range type, contiguous or disparate, this method will do the job.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross
    Last edited by Leith Ross; 12-10-2008 at 04:20 PM. Reason: Correct typo

+ 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