Speed up my VB code a little ?

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I have the following code simply to delete certain rows based on certain conditions. it all works according to plan, however when I have about 2,000 rows, in can take a couple of minutes. I am sure this is down to the way I have written the code. Can anyone see from the below a quicker way of achieving this please ?

----------------------------------------------------------------------------
For I = finalrow To 3 Step -1

'SOURCE CODE REMOVAL

If Cells(I, 13) = "AST" Or Cells(I, 13) = "SST" Then

Range(Cells(I, 2), Cells(I, 14)).Delete

End If

'PAGE SPLIT HEADERS REMOVAL

If Left(Cells(I, 2), 4) = "DATE" Or Left(Cells(I, 2), 6) = "TF BKG" Then

Range(Cells(I, 2), Cells(I, 14)).Delete

End If

'SUPPLIER CODE REMOVAL

If Cells(I, 9) = "C2A" Then

Range(Cells(I, 2), Cells(I, 14)).Delete

End If

'TOUR CODE REMOVAL

If Cells(I, 14) = "WEDING" Then

Range(Cells(I, 2), Cells(I, 14)).Delete

End If



Next I

--------------------------------------------------------------------------

Thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
If you have formulas on the sheet, add application.calculation=xlmanual at the beginning of the code and application.calculation=xlautomatic at the end.
 
Upvote 0
I only have a few formulas, so whilst it has sped it up a little, it's not made a massive difference
 
Upvote 0
You could also try putting all your IF statements together on one line as they all seem to be doing the same thing.
You may want to group them in brackets to separate them if necessary.
 
Upvote 0
Try this:
Code:
   Dim rngDel            As Range
   
   For I = finalrow To 3 Step -1

      If Cells(I, "M") = "AST" Or Cells(I, "M") = "SST" _
         Or Left(Cells(I, "B"), 4) = "DATE" Or Left(Cells(I, "B"), 6) = "TF BKG" _
         Or Cells(I, "I") = "C2A" _
         Or Cells(I, "N") = "WEDING" Then

         If rngDel Is Nothing Then
            Set rngDel = Range(Cells(I, 2), Cells(I, 14))
         Else
            Set rngDel = Union(rngDel, Range(Cells(I, 2), Cells(I, 14)))
         End If

      End If
   Next I
   If Not rngDel Is Nothing Then rngDel.Delete
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
Members
452,949
Latest member
beartooth91

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