Delete rows, speed up?

shadow12345

Well-known Member
Joined
May 10, 2004
Messages
1,238
Hi all,

I am using the below code to delete rows from an excel 2007 sheet.... trouble is it take forever! I have 38000 + rows.

Is there anything I can do to speed up more?

code/ Sub Firstdel()

Dim i As Long
Application.ScreenUpdating = False
For i = Cells(Rows.Count, 1).End(xlUp).Row To 4 Step -1
If Range("AB" & i).Value = "Delete" Then Range(i & ":" & i - 1).Delete Shift:=xlUp
Next i


End Sub \code
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Give this a shot:

Code:
Sub Firstdel()
Dim i As Long
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
For i = Cells(Rows.Count, 1).End(xlUp).row To 4 Step -1
    If Range("AB" & i).Value = "Delete" Then Range(i & ":" & i - 1).Delete Shift:=xlUp
Next i
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub

Also, for the code tags, you need to put brackets around it so it looks like [CODE]'your code here[/CODE]
 
Upvote 0
Code tags [ code ] then end them by using the the same but putting a / after the opening bracket.

Anyhow....on to the code, why not try using autofilter to filter column AB on the value "Delete" then delete those rows. It's a single operation rather than cycling through each cell.
 
Upvote 0
Ahh, better but still take about 3 or 4 minutes to process. (mine was 15 minutes and still going!)

Anything else i can do, im just try to say if the value in ab is Delete, then delete the row.
 
Upvote 0
Ahh, better but still take about 3 or 4 minutes to process. (mine was 15 minutes and still going!)

Anything else i can do, im just try to say if the value in ab is Delete, then delete the row.

Consider doing what Dave suggested. Autofilter based on "Delete" and then delete these rows. It will be faster to do it manually like that than it would take to run your current macro. The problem with your macro is that it is looking at 38,000 rows. Not a whole lot you can do to speed it up beyond what has already been done...

I doubt this will be any faster, but give it a shot:
Code:
Sub Firstdel()
Dim i As Long
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
For i = Cells(Rows.Count, 1).End(xlUp).row To 4 Step -1
    If Range("AB" & i).Value = "Delete" Then Rows(i).Delete
Next i
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Upvote 0
Using autofilter method

Code:
Sub Macro1()
Dim lst As Long
lst = Range("AB" & Rows.Count).End(xlUp).Row
    Range("$A$3:$AC$" & lst).AutoFilter Field:=28, Criteria1:="Delete"
    Range("$A$4:$AC$" & lst).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlUp
    Range("$A$3:$AC$" & lst).AutoFilter
End Sub
 
Upvote 0
I tried using the autofilter method and get and error message saying Microsoft Excel cannot create or use the data range reference because it is too complex...

it debugs at

Code:
    Range("$A$4:$AC$" & lst).SpecialCells(xlCellTypeVisible).EntireRow.Delete xlUp

any ideas?
 
Last edited:
Upvote 0
The Shift:= declaration was omitted, I beleive

Code:
Range("$A$2:$AC$" & lst).SpecialCells(xlCellTypeVisible).EntireRow.Delete Shift:=xlUp
 
Upvote 0
nope still the same error. i tried pasting the values in AB instead (they were forumla results but still no luck
 
Upvote 0
This however does appear to be working..... I guess i could just run it a number of times with different ranges till it works?


Code:
Sub Macro1()

    Range("$A$1:$AC$10000").AutoFilter Field:=28, Criteria1:="Delete"
    Range("$A$2:$AC$10000").SpecialCells(xlCellTypeVisible).EntireRow.Delete xlUp
    Range("$A$1:$AC$10000").AutoFilter
End Sub

What do you think, could I just use that?
 
Upvote 0

Forum statistics

Threads
1,225,204
Messages
6,183,574
Members
453,170
Latest member
sameer98

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