Excel VBA error handling not working for second error

Viewed 8157

In the below code Errorhandler correctly takes care of first error when I enter a workbook that is not open or any random string. But when I click on retry and again enter a random name I get "Subscript out of Range" error @ Workbooks(workbookname). Activate.

Can anyone help me why it is happening and how can I make it work. I have tried a lot of things. But nothing is working. This code is part of a larger program.

Sub test()

Dim workbkname As String
On Error GoTo Errorhandler
Retry:

workbookname = InputBox("Enter workbook name:", _
                        "Name Enrty")
If StrPtr(workbookname) = 0 Then
    MsgBox ("Aborting Program")
    End
End If

Workbooks(workbookname).Activate
Exit Sub

Errorhandler:
Response = MsgBox("Workbook " & workbookname & " not found", vbRetryCancel)
If Response = 4 Then
    GoTo Retry

End If

End Sub
1 Answers
Related