VBA code to delete row if cell does not match following cell

mfh1287

New Member
Joined
Mar 13, 2013
Messages
28
I am trying to figure out a VBA code to look at cell C2 and C3 to see if they are the same - if they are not, I want row 2 to be deleted. Sometimes there are three cells in a row that contain the same value and I would want to keep those.

I hope that this makes sense - if anyone can help I would greatly appreciate it. Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Is This What You Want?
Code:
Sub DeleteRows()
If Range("c2").Value <> Range("c3").Value Then
   Range("2:2").Delete
End If
End Sub
ZAX
 
Upvote 0
Is This What You Want?
Code:
Sub DeleteRows()
If Range("c2").Value <> Range("c3").Value Then
   Range("2:2").Delete
End If
End Sub
ZAX


Yes, thank you. Could you loop it so that it will perform this until the end of the data?
 
Upvote 0
Could you loop it so that it will perform this until the end of the data?
Yes, I Can.
Code:
Sub DeleteRows()
Set data = Range("c2:c" & Cells(Rows.Count, 3).End(xlUp).Row)
For Each cell In data
    If cell.Value <> cell.Offset(1, 0).Value Then
       cell.EntireRow.Delete
    End If
Next
End Sub

ZAX
 
Upvote 0

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

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