MS Excel VBA - How to skip WorksheetFunction.Match if error pops up

时间:2016-10-20 19:18:35

标签: excel vba excel-vba

Hello StackOverFlow Community, I currently have a bit of code that is looking at a value in my active workbook and is basically doing a left lookup to grab a value from another workbook. Here is the code:

'Left lookup to match earn code
                 lookFor1 = .Cells("12", currColIn).Value   ' value to find
                 Set lookforin1 = ws4.Range("C:C")
                 wb2.Worksheets(1).Cells(currRowOut, "E").Value = Application.WorksheetFunction.Index(ws4.Range("J:J"), WorksheetFunction.Match(lookFor1, lookforin1, 0))

When this function runs and cannot propery match it throws a Run-Time error 1004: Unable to get match property of the WorksheetFunction class which is understandable since it cant find what it's looking for

Upon this happening I would like the code to skip it and continue to the next operation without throwing this error. I have done something like this when looking for a record in MS Access where if it cant find what it's looking for it will proceed to the next line without errors with this code:

 If rs.EOF = False And rs.BOF = False Then       'This is a failsafe so the code does not throw an error due to an entered "ID" not existing in the report.
    rs.MoveFirst   
End If

Is there any way I can do this in MS Excel? Thank you in advance for taking the time to read through this!

0 个答案:

没有答案