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.
 

Some videos you may like

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

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
47,926
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>
 

Watch MrExcel Video

Forum statistics

Threads
1,122,371
Messages
5,595,784
Members
414,020
Latest member
Meghdad

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