gpt4 book ai didi

VBA OnError 异常处理不起作用

转载 作者:行者123 更新时间:2023-12-04 21:40:19 25 4
gpt4 key购买 nike

我的 VBA 中有以下示例代码。每当我遇到与系统相关的错误时,我想显示我自己的错误消息。但是下面的代码不起作用。

VBA 告诉

Type Mismatch



我想

Hello your date is invalid



我的代码
Sub good()

Dim date_ As Date
date_ = "hello"

On Error GoTo Err1:

Err1:
MsgBox "Hello your date is invalid"
' more code

End Sub

最佳答案

你需要把 On Error发生错误之前的语句,通常在过程的最开始。想想On Error语句作为指令告诉 VBA 如何处理过程中稍后遇到的任何错误。

Sub good()

On Error GoTo Err1

Dim date_ As Date
date_ = "hello"

Exit Sub 'Make sure you include this so successful executions
'don't continue into the error block.

Err1:
Select Case Err.Number
Case 101 'Not the actual error number!
MsgBox "Hello your date is invalid"
Case 102 'Not the actual error number!
MsgBox "Something else bad happened!"
Case Else
MsgBox "I don't know what happened - unexpected error!"
End Select
' more code

End Sub

关于VBA OnError 异常处理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15026930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com