+ Reply to Thread
Results 1 to 6 of 6

For Loop, jumpoing back all of a sudden

  1. #1
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    For Loop, jumpoing back all of a sudden

    In cells A2:A15 I have:

    0801
    0801
    0801
    0801
    0803
    0803
    0804
    0804
    0805
    0805
    0807
    0807
    0809
    0809

    I am trying to insert a row wherever there is a blank. I am using the Step Into and the Local Window. On the 4th iteration, the code enters the IF statement. When I press F8 on the Set statement and the cursor moves to END IF, at this point, the value of "cell" is 0803 on row 7. This is what I am expecting to see at this point. Now press F8 two more times and the cursor lands on the IF statement. At this point however, the value of "cell" is empty on the 6th row.

    If cell was on row 7, then the next iteration of the For loop occurs, how is it that cell is jumping back to row 6? I want it to stay on row 7.





    Please Login or Register  to view this content.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: For Loop, jumpoing back all of a sudden

    Hi Excel_vba

    When adding or deleting rows it's better to iterate through the cells from the Bottom to the Top, like so
    Please Login or Register  to view this content.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Registered User
    Join Date
    01-14-2013
    Location
    somewhere. in a building.
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: For Loop, jumpoing back all of a sudden

    Hi,
    Generally speaking, when you're inserting or deleting rows on a worksheet, it's a LOT easier to loop bottom to top. Otherwise you'll drive yourself crazy trying to keep track of the things to 'skip'.

    Try this code:
    Please Login or Register  to view this content.
    To see what's going on in your code, add a couple of DEBUG.Print lines:
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: For Loop, jumpoing back all of a sudden

    Quote Originally Posted by jaslake View Post
    Hi Excel_vba

    When adding or deleting rows it's better to iterate through the cells from the Bottom to the Top, like so
    Please Login or Register  to view this content.
    Hello Jaslake. I don't understand the purpose of "For i = lr To 2"

  5. #5
    Forum Contributor
    Join Date
    12-15-2009
    Location
    Herndon, VA
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: For Loop, jumpoing back all of a sudden

    Quote Originally Posted by contracting View Post
    Hi,
    Generally speaking, when you're inserting or deleting rows on a worksheet, it's a LOT easier to loop bottom to top. Otherwise you'll drive yourself crazy trying to keep track of the things to 'skip'.

    Try this code:
    Please Login or Register  to view this content.
    To see what's going on in your code, add a couple of DEBUG.Print lines:
    Please Login or Register  to view this content.
    Hello Contracting. Thank you for your reply. What is the purpose of "For i = 15 To 3 "

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: For Loop, jumpoing back all of a sudden

    Hi Excel_vba

    The Code is saying to start at the Last Row (defined as lr) and look at each Cell...if the Cell above it is a different Value, Insert a New Row. Keep doing this until you get to Row 2 then stop.

+ 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. [SOLVED] do - loop until won't stop going back to beginning
    By Dabbler39 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-11-2013, 01:25 AM
  2. [SOLVED] Loop till end of the sheet and loop back to first column again
    By smlim7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2012, 10:03 AM
  3. Moving from For...Next loop to subroutine and back to loop
    By zabrahamson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2011, 01:26 PM
  4. Excel 2007 : Sudden "freeze" while executing vba-loop
    By JohnFred in forum Excel General
    Replies: 2
    Last Post: 05-31-2010, 07:49 AM
  5. Help With Basic Feed Back Loop
    By STAPLER2 in forum Excel General
    Replies: 2
    Last Post: 11-05-2008, 02:07 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