I think this is your problem: the result of the .Find
method is a range object, which can be Nothing
. You are trying to evaluate Nothing.Row
which raises an error.
Rather than deal with messy error handlers and confusing GoTo
statements, it's best to simply trap that error and deal with it properly.
First, declare a range object and use that to return the result of the .Find
.
Dim rngFound as Range
'## Attempt the lookup in Column C:
Set rngFound = Workbooks(WB2).Sheets("5727").Range("C:C").Find( _
what:=Workbooks(WB1).Main.Cells(Row, 2).Value2, _
lookat:=xlWhole, searchorder:=xlByRows, MatchCase:=True)
Then, you can deal with this rngFound
variable, and test whether it's Nothing
. If it is, then do another Find
against column D:
'## If not found, look for it in column D:
If rngFound Is Nothing Then
Set rngFound = Workbooks(WB2).Sheets("5727").Range("D:D").Find( _
what:=Workbooks(WB1).Main.Cells(Row, 2).Value2, _
lookat:=xlWhole, searchorder:=xlByRows, MatchCase:=True)
End If
If the second find also fails, then you do something else which you already know how to do:
If rngFound Is Nothing Then
'## DO SOMETHING ELSE ##
End If
Then, you can assign to your PLRow
variable
If rngFound Is Nothing then
PLRow = Empty '## Or modify as needed.
Else:
PLRow = rngFound.Row
End If
As a best practice, you should avoid using On Error GoTo...
statements whenever possible, especially when the error can be suitably trapped without an error handler. Also, within your error handlers (if you absolutely must use them for some other reason), you should probably do Err.Clear
and also Resume Next
instead of GoTo VREFLookup
.