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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
 

WM25

New Member
Joined
Mar 11, 2011
Messages
3
Thanks. I tried it but I got an error for:

If Range("D" & i).Value = 0 Then
 

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
It shouldn't be erroring (works fine on my end). Did you adjust the code at all? If so, please post your adjusted code.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
54,999
Office Version
  1. 365
Platform
  1. Windows
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>
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,942
Messages
5,856,428
Members
431,813
Latest member
seconduser

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
Top