My question still remains as to why my code using On Error GoTo only seems to “catch an error” the first time around.
And my answer remains the same: I don't really know what's going on. If you use Application.WorksheetFunction.Match and the search value is not found, Excel will fail/crash. This can be trapped by using On Error. I would use On Error Resume Next and test the error number in the line following the line of code that may cause the error condition. In theory, you could/can use On error Goto Label and test the error number when you get there. That is then often followed by Resume Next. Either way, you would typically clear the error, either by letting err.number = 0 or, more commonly (I guess), using On Error Goto 0.
And that is what you did in your working code.
Conversely, you could just use Application.Match. That will not cause Excel to fail/crash if the search value is not found. It will return an error, "Error 2042", which you can test if you convert the error to a string using CStr. You do not, therefore, need to use error trapping (On Error ...) to trap the error as it just doesn't happen. If you don't change the value returned, and just output it to the worksheet, you'll get #N/A in the cell.
To test your code, I put Debug.Print after every line of code listing the counters and error value. With the original code, you can see that you get an error (1004) which seems to clear on the next On Error GoTo Label. But then, as you have observed, it doesn't seem to trap the next Match error.
Sub SimpleSpreadsheetSortWithMatchErrGotoExcelForum()
Dim sht1 As Worksheet, sht2 As Worksheet
Set sht1 = ThisWorkbook.Worksheets("MainSheet"): Set sht2 = ThisWorkbook.Worksheets("NewFood")
Dim MnCm As Long, NwRw As Long, HdNu 'Numbers for Main Sheet Column , New Sheet Row and , Heading Number
For NwRw = 1 To 20 'Go down at least enough New Product Rows
Debug.Print 1, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
For MnCm = 1 To 11 'Go Through at least enough Main Sheet Columns
Debug.Print 2, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
If sht2.Cells(NwRw, 1).Value <> "" Then 'Check to avoid looking for matching
Debug.Print 3, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
On Error GoTo Here
Debug.Print 4, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
Dim x: x = 0: x = Application.WorksheetFunction.Match(sht2.Cells(NwRw, 1).Value, sht1.Columns(MnCm), 0) ' <<< this fails
'Dim x: x = 0: x = Application.Match(sht2.Cells(NwRw, 1).Value, sht1.Columns(MnCm), 0)
Debug.Print 5, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
If CStr(x) <> "Error 2042" Then
If x > 0 Then 'Heading match conditions = 1, 2,, 3... etc. so:...
Debug.Print 6, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
Let sht1.Range("A12").Offset(0, MnCm - 1).Value = sht2.Cells(NwRw, 2).Value
Debug.Print 7, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
Here: 'Err.Clear
Debug.Print 8, "NwRw: "; NwRw, "MnCm: " & MnCm, "err.number: " & Err.Number
End If
Else 'No Heading Match condition, do Nothing
End If
Else 'No Entry in New Food Sheet
End If
Next MnCm
Next NwRw
End Sub 'SimpleSpreadsheetSortWithMatchErrGotoExcelForum
This is the output. You can see that error 1004 is generated and it is still present starting back through the loop ... but it is cleared between debug 3 and debug 4 ... the "On Error GoTo Here" line.
1 NwRw: 1 MnCm: 0 err.number: 0
2 NwRw: 1 MnCm: 1 err.number: 0
3 NwRw: 1 MnCm: 1 err.number: 0
4 NwRw: 1 MnCm: 1 err.number: 0
5 NwRw: 1 MnCm: 1 err.number: 0
6 NwRw: 1 MnCm: 1 err.number: 0
7 NwRw: 1 MnCm: 1 err.number: 0
8 NwRw: 1 MnCm: 1 err.number: 0
2 NwRw: 1 MnCm: 2 err.number: 0
3 NwRw: 1 MnCm: 2 err.number: 0
4 NwRw: 1 MnCm: 2 err.number: 0
8 NwRw: 1 MnCm: 2 err.number: 1004 Unable to get the Match property of the WorksheetFunction class
2 NwRw: 1 MnCm: 3 err.number: 1004 Unable to get the Match property of the WorksheetFunction class
3 NwRw: 1 MnCm: 3 err.number: 1004 Unable to get the Match property of the WorksheetFunction class
4 NwRw: 1 MnCm: 3 err.number: 0
Bookmarks