1

This is a basic question, but I haven't found any solutions after a Google search.

Here's what I'm trying to do:

I can't get the MsgBox to launch properly: it's highlighted in red. I think this is a syntax issue.

Sub DataProcessingExperiment7
On Error GoTo ErrorHandler
...
ErrorHandler
MsgBox("Error detected" & vbNewLine & "Error" & Err.Number & ": " & Err.Description, vbCritical, "Error Handler: Error" & Err.Number, , , asVbMsgBoxResult)
1

3 Answers 3

2

You call shouldn't be in parenthesis (brackets) unless you're wanting to return a value. Remove the brackets after msgbox

Sub DataProcessingExperiment7
On Error GoTo ErrorHandler
...

ErrorHandler:
MsgBox "Error detected" & vbNewLine & "Error" & Err.Number & ": " & Err.Description, vbCritical, "Error Handler: Error" & Err.Number, , 

Edit:

And you have too many arguments. Remove one

Sign up to request clarification or add additional context in comments.

2 Comments

, asVbMsgBoxResult - that bit shouldn't be there.
Apologies, it's the way I copied the text into VBE. Please ignore that part!
1

You are missing a colon it should be ErrorHandler:

1 Comment

Thanks, but that doesn't fix the MsgBox syntax problem.
0

If you just want to show the user a message with an ok button then use:

MsgBox "This is the message", vbCritical, "This is the title"

The only bit you need is the message bit. You can use named parameters, like this (makes it easier to know which bit is what):

MsgBox Prompt:="This is the message", Buttons:=vbCritical, Title:="This is the title"

If you want to give the user a Yes/No, Ok/Cancel choice, then you use MsgBox as a function and store or use the result.

If vbNo = MsgBox(Prompt:="Do you want to continue?",Buttons:=vbQuestion + vbYesNo, Title:="Are you sure?") Then

For your code use:

ErrorHandler:
MsgBox Prompt:="Error detected" & vbNewLine & "Error" & Err.Number & ": " & Err.Description, Buttons:=vbCritical, Title:="Error Handler: Error" & Err.Number

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.