Question

I searched for the answer to this question, and came VERY CLOSE with

VBA: need decide if result of FIND method is NOTHING or "" (empty)

but I couldn't quite understand how to fix my problem. It is pretty much the same, I am using find to look up something in another workbook, and if the find() can't find what I'm looking for, it throws an error. I tried the suggestions in the link above, but I agree with https://stackoverflow.com/users/478884/tim-williams that since my object is still empty, that the IIF() will error still.

    Do Until Row > LastRow
       On Error GoTo MFGPNError
       PLRow = Workbooks(WB2).Sheets("5727").Range("C:C").Find(what:=Workbooks(WB1).Main.Cells(Row, 2).Value2, lookat:=xlWhole, searchorder:=xlByRows, MatchCase:=True).row

    VREFLookup:
       On Error GoTo VREFError
       PLRow = Workbooks(WB2).Sheets("5727").Range("D:D").Find(what:=Workbooks(WB1).Main.Cells(Row, 2).Value2, lookat:=xlWhole, searchorder:=xlByRows, MatchCase:=True).row

Then my error trapping:

    Exit Sub
    MFGPNError:
    If PLRow Is Empty Then
       Workbooks(WB1).Main.Cells(Row, 3) = ""
    'If IIf(PLRow Is Empty, "", PLRow) = "" Then
    'setting MPN to be "" and moving to VREF lookup    
    On Error GoTo -1
    GoTo VREFLookup
    End If
    '-----
    Exit Sub
    VREFError:
    If IIf(PLRow Is Nothing, "", PLRow) = "" Then
    'setting MPN to be "" and then adding cleaned up pn to array?
    WB1.Main.Cells(Row, 3) = ""
    On Error GoTo -1
    GoTo CleanPN
    End If

On my first Error Trap, I commented out the IIF() because it threw an error, and tried a regular IF(), still an error..

My question is, how can I keep going through my macro, if my Find() throws an error? I would like to just skip that particular Find(), and move on to the next row.

Also, is my error-handling any good? I've never really had to do much of it (mainly do to my macros being very simple)

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top