Joe4 posted the following code to delete rows based on cell value. I have two problems.
#1 I need to run this from a button on another Sheet. How do I make it run on the desired sheet?
#2 If the user runs it twice it deletes all data on sheet except for first row. How do I have it check if there are no "0" then exit ?
#1 I need to run this from a button on another Sheet. How do I make it run on the desired sheet?
#2 If the user runs it twice it deletes all data on sheet except for first row. How do I have it check if there are no "0" then exit ?
VBA Code:
Sub MyDeleteMacro()
'
Dim lr As Long, lr2 As Long
Application.ScreenUpdating = False
' Find last row with data in column P
lr = Cells(Rows.Count, "P").End(xlUp).Row
' Hide all rows not equal to zero
Columns("P:P").AutoFilter
ActiveSheet.Range("$P$1:$P$" & lr).AutoFilter Field:=1, Criteria1:="0"
' Find last row in column P with data after filter
lr2 = Cells(Rows.Count, "P").End(xlUp).Row
' Exit sub if no data to delete data (only header visible)
If lr2 = 2 Then Exit Sub
' Delete unhidden data
Application.DisplayAlerts = False
ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).Rows.Delete
Application.DisplayAlerts = True
' Remove filter
Range("P1").AutoFilter
Application.ScreenUpdating = True
End Sub