Deleting Rows with VBA

browncountry

New Member
Joined
Feb 2, 2019
Messages
13
The code below works as is, but when I there are 40,000 to 50,000 lines of data, it tends to slow down a lot. In the code below, I'm checking for unwanted data in columns A and G. Is there anything I can do to speed this process up? Thanks!

VBA Code:
Sub DeleteEntireRow()

Dim RowToTest As Long
With Application
.Calculation = xlCalculationManual
        .ScreenUpdating = False

For RowToTest = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 1)
If .Value = "GRAND TOTAL" _
Then _
Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest

For RowToTest = Cells(Rows.Count, 7).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 7)
If .Value <> "P" _
Then _
Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest

.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
How about
VBA Code:
Sub browncountry()
   With ActiveSheet
      .Range("A1:G1").AutoFilter 1, "Grand total"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .ShowAllData
      .Range("A1:G1").AutoFilter 7, "<>P"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Well this will reduce the time by about half as it will only loop once through the rows:
VBA Code:
Sub DeleteEntireRow()
    Dim LastRow As Long
    Dim RowToTest As Long
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        If Cells(Rows.Count, 1).End(xlUp).Row > Cells(Rows.Count, 7).End(xlUp).Row Then
            LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        Else
            LastRow = Cells(Rows.Count, 7).End(xlUp).Row
        End If
        For RowToTest = LastRow To 2 Step -1
            If Cells(RowToTest, 1).Value = "GRAND TOTAL" Then Rows(RowToTest).EntireRow.Delete
            If Cells(RowToTest, 7).Value <> "P" Then Rows(RowToTest).EntireRow.Delete
        Next RowToTest

        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub browncountry()
   With ActiveSheet
      .Range("A1:G1").AutoFilter 1, "Grand total"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .ShowAllData
      .Range("A1:G1").AutoFilter 7, "<>P"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
Wow! That was great.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
You're welcome & thanks for the feedback
Fluff, I have one more question. In Column 6, I would like to delete 4 other items. I thought the code below would work, but it didn't. Should I filter and delete each one separately or is there a way to write this on one line?

VBA Code:
      .Range("A1:G1").AutoFilter 6, "3B", "SD", "SOM", "TAC"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .ShowAllData
 
Upvote 0
That needs to be like
VBA Code:
.Range("A1:G1").AutoFilter 6, Array("3B", "SD", "SOM", "TAC"), xlFilterValues
 
Upvote 0

Forum statistics

Threads
1,224,738
Messages
6,180,673
Members
452,993
Latest member
FDARYABEE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top