+ Reply to Thread
Results 1 to 4 of 4

Loop for Deleting Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    04-07-2009
    Location
    Isarel
    MS-Off Ver
    Excel 2003
    Posts
    13

    Loop for Deleting Rows

    Hello Experts

    Whats wrong with my code ?
    for i=3 to tblrows2
      if range ("a" & i) = 1 then
      rows ("i:i").select
      selection.delete shift:=xlup
      end if
    next i
    Thanks
    Sharky
    Last edited by Sharky Amit; 04-20-2009 at 05:36 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Loop for Deleting Rows

    When you delete rows you should work in reverse - bottom up...

    If you consider you have rows 2:10 and you loop from 2 to 10, when you delete row 3 then what was row 4 is now 3 yet you miss it as you go to 4 (which was 5), make sense ?

    Better then to adopt something along the lines of:

    For i = tblrows2 to 3 step -1
        If Cells(i,"A") = 1 then Rows(i).Delete
    Next i

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop for Deleting Rows

    Exactly right...and if you're deleting a LOT of rows, then the code will execute much faster if you turn off the screenupdating:
    Application.ScreenUpdating = False
        For i = tblrows2 to 3 step -1
            If Cells(i,"A") = 1 then Rows(i).Delete
        Next i
    Application.ScreenUpdating = True
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Loop for Deleting Rows

    If speed were a concern then I would make the point that as much as ScreenUpdating will slow code, given Deletion of Rows is a Volatile action if you have Volatile functions you're best served forcing Calculation to Manual prior to implementation of Deletion Loop and restoring Calc to it's former setting on completion ... and if you do that you may as well do all three, ie: screenupdate, calculation & events.

    An example below:

    Public Sub Example()
    Dim xlCalc As XlCalculation, tblRows2 As Long, i As Long
    On Error GoTo Handler
    tblRows2 = Cells(Rows.Count, "A").End(xlUp).Row
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        For i = tblRows2 To 3 Step -1
            If Cells(i, "A") = 1 Then Rows(i).Delete
        Next i
    ExitPoint:
        .Calculation = xlCalc
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Exit Sub
    
    Handler:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
    Resume ExitPoint
    
    End Sub

+ 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