Macro code to include header row

Moogie

New Member
Joined
Sep 16, 2002
Messages
47
Greetings, I have the following macro used to delete rows with 0 in column E. However, this macro is assuming there is a header row and there is none. Can someone please tell me how to tweak it so it will include the header row? Thanks a bunch.

Sub DeleteRows()

Application.ScreenUpdating = False
Dim e As Range, rng
Range("e65536").End(xlUp).Select
lastrow = Selection.Row
Set rng = Range("E1:e" & lastrow)
For Each e In rng
If e.Value = 0 Then
e.Offset(0, 1).Value = "Delete"
End If
Next e
With Range(Range("f1"), Range("f" & Rows.Count).End(xlUp))
.AutoFilter Field:=1, Criteria1:="Delete"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Note that this code first tags the row with "delete", and that works for the header row, it is the code that actually deletes the row that skips the header row.

Thanks
 
Upvote 0
Try...

Code:
Sub DeleteRows()

Application.ScreenUpdating = False

Dim i As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, "E").End(xlUp).Row

For i = LastRow To 1 Step -1
    If Cells(i, "E") = 0 Then
        Rows(i).Delete
    End If
Next

Application.ScreenUpdating = True
   

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,222
Members
448,877
Latest member
gb24

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