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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

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
 
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,164,019
Messages
5,834,983
Members
430,331
Latest member
Syed Yasir Hannan

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