Why does my macro run so slow???

Rags

Board Regular
Joined
Oct 25, 2006
Messages
202
Hi

Why does this macro run so slow? It takes about 2 min 40 secs to run through a sheet of 170 columns by 2400 rows.

Code:
Sub Delete_Deleted_Schemes()

    Application.ScreenUpdating = False

    Dim LastRow As Long, c As Long
    LastRow = Range("A65536").End(xlUp).Row
    
    For c = LastRow To 2 Step -1
    If Cells(c, "C") <> "FY06/07 Programmed" Then
    Rows(c).Delete
    End If
    Next c
    
End Sub

Or is this normal?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
are there alot of formulas on the sheet?

deleting a row will cause the sheet to calculate...therefor calculating every time a row is deleted....

you can try turning off the auto calculate at the beginnging of macro..

Application.Calculation = xlManual

then turn it back on at the end of macro

Application.Calculation = xlAutomatic
 
Upvote 0
Could also write the macro to filter the data instead of looping.

For example,
- Filter to display data that does not contain "FY06/07 Programmed" in column C
- Delete visible rows
- Turn off filter.
 
Upvote 0
You are also scanning through 65536 rows instead of just the 2400 that are used.
 
Upvote 0
You are also scanning through 65536 rows instead of just the 2400 that are used.
LastRow = Range("A65536").End(xlUp).Row

this line only happens once in the code as it is outside the loop...and it doesn't take long (a split second) to do that code, so it's not a contributer of the 2 minutes 40 seconds described. This is common code for determining the last used row.
 
Upvote 0
are there alot of formulas on the sheet?

deleting a row will cause the sheet to calculate...therefor calculating every time a row is deleted....

you can try turning off the auto calculate at the beginnging of macro..

Application.Calculation = xlManual

then turn it back on at the end of macro

Application.Calculation = xlAutomatic

There are 4 columns of formulas. I turned off auto calculate and got the time down to 55 seconds.

Could also write the macro to filter the data instead of looping.

For example,
- Filter to display data that does not contain "FY06/07 Programmed" in column C
- Delete visible rows
- Turn off filter.

This is what I've done in the past and is probably the direction to head.

Thanks :biggrin:
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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