Delete rows with Macro

Horby

New Member
Joined
Jun 13, 2014
Messages
22
Hi, How do I delete a set of rows automatically. See the picture
delete.jpg
 
Yes, All cells. 1 to last cell. A:A
Filling all those cells with zeroes seem wasteful and inefficient; however, if that is what you have, then give this code a try...

Code:
Sub DeleteRows()
  If Application.CountIf(Columns("A"), 0) = Rows.Count Then Range("A6:D" & Rows.Count).ClearContents
End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Filling all those cells with zeroes seem wasteful and inefficient; however, if that is what you have, then give this code a try...

Code:
Sub DeleteRows()
  If Application.CountIf(Columns("A"), 0) = Rows.Count Then Range("A6:D" & Rows.Count).ClearContents
End Sub
Unfortunately does not work! i need is if total column A = 0
If this condition is met is executed code.
 
Upvote 0
Unfortunately does not work! i need is if total column A = 0
If this condition is met is executed code.

But that is what my code does.. if every cell in Column A equals a zero (from A1 down to A1038576), then A6:D1038576 is cleared, otherwise nothing happens. If that is not working for you, then there is something different about your cells that you are not telling us. Another possibility, I guess, is if you have a Change event procedure that fills in blank cells... that would kick in after my macro runs and "undo" things.
 
Upvote 0
But that is what my code does.. if every cell in Column A equals a zero (from A1 down to A1038576), then A6:D1038576 is cleared, otherwise nothing happens. If that is not working for you, then there is something different about your cells that you are not telling us. Another possibility, I guess, is if you have a Change event procedure that fills in blank cells... that would kick in after my macro runs and "undo" things.
I want total cells, not every cell. if SUM(A:A)=0


See the picture.
delete.jpg
 
Last edited:
Upvote 0
Sorry, I misunderstood your response to my question. Give this code a try then...

Code:
Sub DeleteRows()
  If Application.Sum(Columns("A")) = 0 Then Range("A6:D" & Rows.Count).ClearContents
End Sub
 
Upvote 0
Sorry, I misunderstood your response to my question. Give this code a try then...

Code:
Sub DeleteRows()
  If Application.Sum(Columns("A")) = 0 Then Range("A6:D" & Rows.Count).ClearContents
End Sub
Excellent, works successfully!

Finally
Can I show an alert message if the code is not executed?
 
Upvote 0
After this Modified:
Code:
Sub DeleteRows()
  If Application.Sum(Columns("A")) = 0 Then
  Range("A6:D" & Rows.Count).ClearContents
  Else
 MsgBox ("No deletion!")
  End If
End Sub

Works successfully!

Thank you for your help, and thanks to all.
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

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