+ Reply to Thread
Results 1 to 6 of 6

For Loop, jumpoing back all of a sudden

Hybrid View

  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.





     For Each cell In Range("A2:A15")
            If cell.Value <> cell.Offset(1, 0).Value Then
                cell.Offset(1, 0).EntireRow.Insert
                Set cell = cell.End(xlDown)
            End If
        Next cell

  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
    Option Explicit
    
    Sub test()
        Dim lr As Long, i As Long
        Dim rng As Range
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Set rng = Range("A1:A" & lr)
        With rng
            For i = lr To 2 Step -1
                If Not rng(i).Value = rng(i).Offset(-1, 0).Value Then
                    rng(i).EntireRow.Insert
                End If
            Next i
        End With
    End Sub
    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
    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
    Option Explicit
    
    Sub test()
        Dim lr As Long, i As Long
        Dim rng As Range
        lr = Range("A" & Rows.Count).End(xlUp).Row
        Set rng = Range("A1:A" & lr)
        With rng
            For i = lr To 2 Step -1
                If Not rng(i).Value = rng(i).Offset(-1, 0).Value Then
                    rng(i).EntireRow.Insert
                End If
            Next i
        End With
    End Sub
    Hello Jaslake. I don't understand the purpose of "For i = lr To 2"

  4. #4
    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.

  5. #5
    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:
    
    Sub Better()
        Dim i As Long
        'inserting/deleting rows works a LOT simpler if you loop bottom to top..
        For i = 15 To 3 Step -1  'don't bother to check the top cell, unless you really want an extra row
            If Cells(i, 1) <> Cells(i - 1, 1) Then
                Worksheets("Sheet2").Cells(i, 1).EntireRow.Insert
            End If
        Next i
    End Sub
    To see what's going on in your code, add a couple of DEBUG.Print lines:
    Sub Old()
    
        For Each cell In Range("A2:A15")
            Debug.Print cell.Address
            If cell.Value <> cell.Offset(1, 0).Value Then
                cell.Offset(1, 0).EntireRow.Insert
                Set cell = cell.End(xlDown)
                Debug.Print cell.Address
            End If
        Next cell
    End Sub
    You'll see that cell goes to a7 like you want, but the loop through the collection then goes to the next item on it's list, which is a6

  6. #6
    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:
    
    Sub Better()
        Dim i As Long
        'inserting/deleting rows works a LOT simpler if you loop bottom to top..
        For i = 15 To 3 Step -1  'don't bother to check the top cell, unless you really want an extra row
            If Cells(i, 1) <> Cells(i - 1, 1) Then
                Worksheets("Sheet2").Cells(i, 1).EntireRow.Insert
            End If
        Next i
    End Sub
    To see what's going on in your code, add a couple of DEBUG.Print lines:
    Sub Old()
    
        For Each cell In Range("A2:A15")
            Debug.Print cell.Address
            If cell.Value <> cell.Offset(1, 0).Value Then
                cell.Offset(1, 0).EntireRow.Insert
                Set cell = cell.End(xlDown)
                Debug.Print cell.Address
            End If
        Next cell
    End Sub
    You'll see that cell goes to a7 like you want, but the loop through the collection then goes to the next item on it's list, which is a6
    Hello Contracting. Thank you for your reply. What is the purpose of "For i = 15 To 3 "

+ 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