Delete entire rows if ...: how to speed up the process?

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I have 500k rows to check.
If cell "H" is not beginning for "CODE", then delete entire row.

How You can imagine, with a such amount of rows the process takes too long.

How can I speed up? May be with a REGEX strategy?


Code:
Sub deleterows()

Dim rw As Long

    With Worksheets(1)
        For rw = .Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Not Cells(rw, "H").Value Like "CODE*" Then
                .Rows(rw).EntireRow.Delete
            End If
        Next rw
    End With

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about
Code:
Sub nelson78()
   With worksheets(1)
      If .AutoFilterMode Then .AutoFilterMode = False
      .Range("A1:H1").AutoFilter 8, "<>code*"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
How about
Code:
Sub nelson78()
   With worksheets(1)
      If .AutoFilterMode Then .AutoFilterMode = False
      .Range("A1:H1").AutoFilter 8, "<>code*"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub

Something wrong.

I have to write code or CODE?

Anyway, error 1004 object-defined error or application-defined on the following line

Code:
.AutoFilter.Range.Offset(1).EntireRow.Delete
 
Upvote 0
Are you trying to run this on the first sheet in the workbook?
Also do you have a header row in row 1?

Autofilter is not case sensitive so it doesn't matter if you use CODE* or code*
 
Upvote 0
Are you trying to run this on the first sheet in the workbook?
Also do you have a header row in row 1?

Autofilter is not case sensitive so it doesn't matter if you use CODE* or code*

There is only 1 sheet in the workbook.
Columns are populated from A to P.
In row 1 yes, I have a header row.
 
Upvote 0
Is your data in a proper table?
 
Upvote 0
Is your data in a proper table?

The question is not so easy for me.

Anyway: the data comes from an export that produce a csv file, then I select column A and convert Text to Columns, then save in .xlsx format.
 
Last edited:
Upvote 0
How about
Code:
Sub nelson78()
   Dim UsdRws As Long
   
   With Worksheets("dup")
      If .AutoFilterMode Then .AutoFilterMode = False
      UsdRws = .Range("A" & Rows.Count).End(xlUp).Row
      .Range("A1:H" & UsdRws).AutoFilter 8, "<>code*"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
How about
Code:
Sub nelson78()
   Dim UsdRws As Long
   
   With Worksheets("dup")
      If .AutoFilterMode Then .AutoFilterMode = False
      UsdRws = .Range("A" & Rows.Count).End(xlUp).Row
      .Range("A1:H" & UsdRws).AutoFilter 8, "<>code*"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
End Sub

The workbook I was using before was in COMPATIBILITY MODE.
Than, I saved again in .xls format to inhibit that mode.

Now, processing your macro, it works fine (350597 record elaborated in 3 minutes).

Do you have any idea about the error I firstly mentioned?
 
Last edited:
Upvote 0
Glad to hear it's working, but I've no idea why you got that error before.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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