+ Reply to Thread
Results 1 to 9 of 9

Looping through cells in Range

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Looping through cells in Range

    Hi,

    I can't tell why this is only looping through part of the range. I have tried resizing but still not working. The range is 5 * 10 and therefore the last x should be 50 but it only goes up to 10 making me think that the array is not two-dimensional. Any thoughts?

    Please Login or Register  to view this content.
    abousetta
    Last edited by abousetta; 12-11-2011 at 02:09 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,310

    Re: Looping through cells in Range

    Hi,

    What is the last value in colum E? If you have blank cells at the bottom of the column E then you won't get all that (I think) you want.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Looping through cells in Range

    Hi MarvinP,

    No, I tested it with a mock data set and still doesn't work:

    Please Login or Register  to view this content.
    Its only counting up to 10.

    abousetta

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Looping through cells in Range

    This is weird... The 10 being looped are actually Range("A1:E2")??? Why??? Even when I hard cody it as Range("A1:E10") it still gives me the same results.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,946

    Re: Looping through cells in Range

    From Help:
    Syntax;
    UBound(arrayname[, dimension])
    If dimension is omitted, 1 is assumed. You have a two dim array of 10 rows X 5 columns, [cl : Variant/Variant(1 to 10, 1 to 5)] therefore UBOUND defaults to 10. But even if you had said: For x = LBound(cl,1) To UBound(cl,2), Ubound would have returned 5 since that's the number of columns. Did you want to use something like:
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Last edited by protonLeah; 12-11-2011 at 01:44 AM. Reason: code tags not working correctly
    Ben Van Johnson

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Looping through cells in Range

    Thanks Ben. That did the trick. Also now I understand my misconception about 2 dimensional arrays. What I should have done is:

    Please Login or Register  to view this content.
    That gave me the same results as:

    Please Login or Register  to view this content.
    I have a love/hate relationship with arrays... I love to try using them, but I hate that I keep on failing

    Thanks again.

    abousetta

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

    Re: Looping through cells in Range

    Three methods to choose from:

    Please Login or Register  to view this content.



  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Looping through cells in Range

    Thanks snb. Method #1 and #3 are clear to me. Method #2 is a little bit more mysterious. Could you explain how this works:

    Please Login or Register  to view this content.
    abousetta

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

    Re: Looping through cells in Range

    It loops through the whole range, using only on loop.
    The whole array size is rows*columns
    you can check using
    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