+ Reply to Thread
Results 1 to 6 of 6

A Loop does not loop throught. It stops after two cells.

  1. #1
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    A Loop does not loop throught. It stops after two cells.

    I have relly got weird problem with the attached sheet. I set up a last row and try to loop through from 1 to last row. For some unknow reason, the last row shows the correct result,but when it loops throught, instead of looping from 1 to 24, the loop stops at 1, or 2. To test my syntax, I set up a different module and tests it using msgboxes. The magboxes show the corrrect result,but if I use the same syntax on the actual sheet, the loop stops at 1, or 2.

    Please Login or Register  to view this content.
    The last row, ie LR2 is 24, but when it loops from 1 to LR2, instead of giving the result of 1 to 24, I have got 1 to 1.
    I am sure, There is an error on the above line, but having done a test, I am unable to see it. Please a person with eye for details help.
    Attached Files Attached Files

  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
    48,120

    Re: A Loop does not loop throught. It stops after two cells.

    This first one works, but note the variable you use is not the same as the one you Dim:

    Please Login or Register  to view this content.
    Second one works fine. Third one works fine but, again, not the variable in the loop vs the variable Dimmed

    Please Login or Register  to view this content.

    This last one, copied from your post and edited counts from 1 to 24.


    I'm not entirely sure what you're doing wrong. These all seem to work as expected, barring the incorrect variable usage. Use Option Explicit to avoid this issue.


    Regards, TMS
    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
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: A Loop does not loop throught. It stops after two cells.

    Thanks TMS!
    If I have now corrected the variable names. If you look at modules Test1 and test 2. There are 24 rows on column "B", but on test2 module, the loop returns 25. So,if the last row is 24 and you loop through from 1 to last row, I thought, you get 24 rows, not 25.
    Attached Files Attached Files

  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
    48,120

    Re: A Loop does not loop throught. It stops after two cells.

    No. You increment X from 1 to LR (24) and it will drop out of the loop when X is greater than 24. 25 is greater than 24 so it drops out of the loop and displays X = 25 and LR = 24. It has to increment X in order to test it against the upper limit.

    It's rare that you would want to do anything with X once it has served its purpose as a loop counter.

    Regards, TMS

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: A Loop does not loop throught. It stops after two cells.

    Thanks TMS! I have got it.

  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
    48,120

    Re: A Loop does not loop throught. It stops after two cells.

    You're welcome.

+ 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