How Can I Delete Row Based on Criteria of Cell - VBA?

coltonspe

New Member
Joined
Nov 21, 2012
Messages
9
Does anyone know the VBA to delete a row based on the contents of a particular cell?

I have the following formula comparing to another sheet to see if there is a duplicate:

=IF(ISERROR(MATCH(B7,Data!A:A,0))=FALSE, "Y","N")

How can I write the VBA to then take the cells that have a "Y" and delete those rows, leaving only the rows with a "N" in them.

Thanks in advance

(Excel 2010)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
what column are your formulas in and what is the starting cell?
 
Upvote 0
Hi,
Assuming:
(1) The rows you want to delete are on the active sheet and (2) The "Y" and "N" values are in (say) column D, then you can try something like this.
Code:
Sub DeleteTheYs()
Dim LstRw As Long, Rw As Long
LstRw = Cells(Rows.Count, "D").End(xlUp).Row
For Rw = LstRw To 2 Step -1
  If Cells(Rw, "D").Value = "Y" Then Rows(Rw).Delete
Next Rw
End Sub

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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