Deleting a row based on criteria

ukkz001

Board Regular
Joined
Apr 12, 2005
Messages
199
I'm trying to write a macro that will delete the row if both columns M and N are equal to 0.00.

I also need the macro to look through each line of data until the criteria is met.


Thanks,
Kevin
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
One way:

Code:
Sub DeleteRows()
Dim i As Long, Lastrow As Long

Lastrow = Range("M65536").End(xlUp).Row

For i = Lastrow To 1 Step -1
    If Cells(i, "M").Value = 0 And Cells(i, "N").Value = 0 Then
        Rows(i).Delete
    End If
Next i

End Sub
 

ukkz001

Board Regular
Joined
Apr 12, 2005
Messages
199
I couldn't figure out how to have it look in both M and N columns.
This is what I was using:

Range("M1:N1").Select
Do Until IsEmpty(ActiveCell.Value)
If ActiveCell.Value = "0.00" Then
ActiveCell.Row.Delete
Else 'Do nothing
'Move down one cell
ActiveCell.Offset(1, 0).Select
Loop

I will try your code.

Thanks,
Kevin
 

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
To be honest, you would run into problems with your code other than trying to check both M and N.

If you loop through the rows from 1 down and delete rows during that, the code will end up skipping over some rows and not checking them at all.

What I posted looks from the last row up, so when a row is deleted it does not affect the loop.
 

ukkz001

Board Regular
Joined
Apr 12, 2005
Messages
199
Good point. I didn't think of that.
Thanks a lot for your help.


Kevin
 

Watch MrExcel Video

Forum statistics

Threads
1,118,673
Messages
5,573,588
Members
412,537
Latest member
Mohamed_5966
Top