My code opens a user-selected workbook and attempts to select the worksheet name that matches the current sheet in the original workbook. For some reason, the error handler doesn't get triggered if it doesn't find a matching sheet. I have copied the code to a new workbook, and it works fine there. Does anyone have ideas on what might nullify the error handler? In the code below, the FindSheetFailed is the one that won't work. Instead it gets a Subscript Out Of Range error if it doesn't find the sheet.
Sub CopyAllComments()
iResult = vbOK
Restart:
Call SetDefaultPath
PathName = Application.GetOpenFilename("Microsoft Office Excel Workbook(*.xls),*.xls", 2, MsgTitle)
On Error GoTo OpenFailed
Workbooks.Open PathName
On Error GoTo 0
On Error GoTo FindSheetFailed
ActiveWorkbook.Sheets(ThisWorkbook.ActiveSheet.Name).Select
On Error GoTo 0
Exit Sub
OpenFailed:
If PathName <> False Then
MsgBox "Unable to open " & PathName, vbOKOnly, MsgTitle
End If
iResult = vbCancel
Exit Sub
FindSheetFailed:
MsgBox ThisWorkbook.ActiveSheet.Name & " sheet not found on " & ActiveWorkbook.Name
ThisWorkbook.Activate
iResult = vbCancel
End Sub
Bookmarks