Creating macro to delete rows

WM25

New Member
Joined
Mar 11, 2011
Messages
3
I'm trying to create a macro to delete rows that contain the value "0" only in a certain column. Unfortunately other columns contain 0 as well and the codes I've been using have deleted those rows as well. Here's an example:

Column A Column B Column C Column D
text 0 0 0
text 3 text text
text 1 text text
text 0 0 100
text 0 0 0

Column D is based off a true/false formula hence the 0. I just need a code to delete the "0" and not the text (that contains 0) or 100.

I've actually never worked with macros before and nothing I find is helpful.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the forums!

Try:

Code:
Public Sub CleanSheet()
Dim i   As Long, _
    LR  As Long
LR = Range("D" & Rows.Count).End(xlUp).row
Application.ScreenUpdating = False
For i = LR To 1 Step -1
    If Range("D" & i).Value = 0 Then Rows(i).Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
It shouldn't be erroring (works fine on my end). Did you adjust the code at all? If so, please post your adjusted code.
 
Upvote 0
I've actually never worked with macros before and nothing I find is helpful.
In that case do you really need a macro. Assuming your data has headings (if not you could temporarily add one) you could use AutoFilter to filter column D for 0 values then just manually select all the visible rows below the heading and delete them. Then remove the AutoFilter.

If you really do need a macro, here is one that does what I've described above (assumes headings in row 1)

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Del_Rows()<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Range("D1", Range("D" & Rows.Count).End(xlUp))<br>        .AutoFilter Field:=1, Criteria1:="=0"<br>        .Offset(1).EntireRow.Delete<br>        .AutoFilter<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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