vba Delete row if value doesn't exist in a range

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I have 2 worksheets (Sheet1 and sheet2). I am trying to write a macro that will look for the cell value of Sheet1 A2 in sheet2 column C. If that value doesn't exist in sheet2 column c then delete row 2 on sheet1.
This needs to happen for all rows on sheet1. The amount of rows on sheet1 and sheet2 varies.

Thank you!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Function Highlighter()
Dim k As Long
Dim lrow As Long
lrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
lrow1 = Sheet2.Range("C" & Rows.Count).End(xlUp).Row


For d = 2 To lrow
On Error Resume Next
Sheet1.Range("B" & d).Value = WorksheetFunction.VLookup(Sheet1.Range("A" & d).Value, Sheet2.Range("C1:C1048576"), 1, 0)
Next
Sheet1.AutoFilterMode = False
Sheet1.Range("A1:b1048576").AutoFilter Field:=2, Criteria1:=""
Sheet1.Range("A2:B" & lrow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
Sheet1.AutoFilterMode = False
Sheet1.Range("b2:b" & lrow).ClearContents






End Function
 
Upvote 0
Another option
Code:
Sub DelRows()

   Dim Cl As Range
   Dim Rng As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sheets("Sheet2").Range("C2", Sheets("Sheet2").Range("C" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl.Offset(, 1).Value
      Next Cl
      For Each Cl In Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            If Rng Is Nothing Then
               Set Rng = Cl
            Else
               Set Rng = Union(Rng, Cl)
            End If
         End If
      Next Cl
   End With
   Rng.EntireRow.Delete
End Sub
 
Upvote 0
Hi Fluff,

This solution works. I am getting an error whenever there is no rows to delete. I.E. every value exists on sheet 2. Is there a way around this?
 
Upvote 0
Yup, just change the last line to
Code:
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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