Home » excel » excel – Runtime Error with Workbook Auto Open + MsgBox as response for an user

excel – Runtime Error with Workbook Auto Open + MsgBox as response for an user

Posted by: admin April 23, 2020 Leave a comment

Questions:

I was trying to put MsgBox into my code. It should be shown only if call Table.auto_open doesn’t work.

In my final document are few of those Call statements and i would like to get only that msgbox, if one or few of Call statements doesn’t work.
For Example “Auto_open” will be changed to “auto_op” what naturally won’t be possible, because in real sheet it is “Auto_open”.
Or in another example that code from “Auto_open” is broken.

I need some help with that. It’s seems to be simple, but I think it’s not possible to put that “On Error GoTo” code in that place just like that, because Call doesn’t give me a real error?

Can someone say me what I’m doing wrong? I tried already all combinations of that Error handling, nothing works.

Private Sub Workbook_Open()

On Error GoTo Error
 Call Tabelle1.auto_open
Exit Sub
 Error:
 MsgBox "Failure"
Resume Next
End Sub
How to&Answers:

If you are calling a sub that does not exist you will get a Compile Error.

You can check for these errors by going to VBE>Debug>Compile VBAProject (or just try to run the macro)

Compile Errors, much like Syntax Errors, have to be handled before you can run a sub. Thus, these errors cannot be handled with code such as On Error GoTo EH or On Error Resume Next as these are only activated once the sub is actually running.

You can convince yourself of this by producing a common Compile error, or Syntax error, and trying to step through the code (F8). You will notice that the error occurs on your Sub [Name] () line, which indicates that you never actually entered your sub before the error occurred. Thus, you can intuitively see that your error handler will never actually be activated, resulting in an error message being displayed.

Once you have accounted for all Compile/Sytax Errors, you can check out this link, which will explain how you can handle Run Time Errors when you are calling other sub procedures from a sub.