VBA ErrorHandler MsgBox语法

这是一个基本的问题,但是Googlesearch之后我还没有find任何解决scheme。

这是我想要做的:

我无法使MsgBox正常启动:它以红色突出显示。 我认为这是一个语法问题。

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

你错过了一个冒号应该是ErrorHandler:

除非你想要返回一个值,否则你不应该在括号(括号)中。 删除msgbox后的括号

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

编辑:

而且你有太多争论。 删除一个

如果您只想向用户显示带有OKbutton的消息,请使用:

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

唯一需要的是消息位。 你可以像这样使用命名参数(更容易知道哪个位是什么):

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

如果你想给用户一个是/否,确定/取消select,那么你使用MsgBox作为一个函数,并存储或使用结果。

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

为了您的代码使用:

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