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
 

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
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

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