I've been following the example from this solution Excel vba - Compare two ranges and find non matches and tried to combine it with this solution http://dailydoseofexcel.com/archives/2004/05/18/listing-unique-items-with-collections/
However, when I run the code included below, I get a run-time error '1004': Unable to get the Vlookup property of the WoorkSheetClass. How do I get past that error, so that I can remove the rows that does not exist in my cUniqueInput?
Sub RemoveYdelser()
'Get Range From Datark To get range of Ydelses Group
Set YdelsesStart = Worksheets("DATAARK").Range("O8")
Set YdelsesSlut = Worksheets("DATAARK").Range("P8")
Dim LastRow As Long
LastRow = Worksheets("INPUT_MASTERDATA").Range("L" & Rows.Count).End(xlUp).Row
'Variable to get ydelser from Input_masterdata
Dim cUniqueInput As Collection
Dim Rng As Range
Dim Cell As Range
Dim shInput As Worksheet
Dim vNum As Variant
Set shInput = Worksheets("INPUT_MASTERDATA")
Set Rng = shInput.Range("L2:L" & LastRow)
Set cUniqueInput = New Collection
'Get unique values from Input_masterdata, column L
On Error Resume Next
For Each Cell In Rng.Cells
cUniqueInput.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
'Add exceptions for master ydelser not listed in column L
cUniqueInput.Add "x", CStr("x")
cUniqueInput.Add "R63011209", CStr("R63011209")
cUniqueInput.Add "R63011206", CStr("R63011206")
'Variable to get ydelser from Summary
Dim cUniqueYdelser As Collection
Dim RngYdelser As Range
Dim CellYdelser As Range
Dim shOpsumering As Worksheet
Dim xNum As Variant
Set shOpsumering = Worksheets("Summary")
Set RngYdelser = shOpsumering.Range("C" & YdelsesStart, "C" & YdelsesSlut)
Set cUniqueYdelser = New Collection
'Get unique values from Summary, column C
On Error Resume Next
For Each CellYdelser In RngYdelser.Cells
cUniqueYdelser.Add CellYdelser.Value, CStr(CellYdelser.Value)
Next CellYdelser
On Error GoTo 0
'Does the values from cUniqueInput exist in cUniqueYdelser
For y = 1 To cUniqueYdelser.Count
'If the value of a row in cUniqueYdelser does not exist in cUniqueInput, then remove entire row
'This is where I get the runtime error
If Application.WorksheetFunction.VLookup(cUniqueYdelser(y), cUniqueInput, 1, False) = "#N/A" Then
With Worksheets("OPSUMMERINGSARK").Cells
.AutoFilter Field:=3, Criteria1:=cUniqueYdelser(y)
.Range("C" & YdelsesStart, "C" & YdelsesSlut).EntireRow.Delete
End With
End If
Next y