On Error GoTo的问题,在线等。
VB中On Error GoTo的错误处理当执行语句执行吗?为什么下面两段代码总是弹出error对话框呢?不是有错误才会进入err:里面去吗
Private Sub CommandButton1_Click()
On Error GoTo err
err:
MsgBox "some error happened", vbCritical, "error"
End Sub
Private Sub CommandButton2_Click()
On Error GoTo err
dim str As String
str ="test"
MsgBox str
err:
MsgBox "some error happened", vbCritical, "error"
End Sub
[解决办法]
Option ExplicitPrivate Sub CommandButton1_Click()On Error GoTo errDebug.Print 1 / 0Exit Suberr: MsgBox "some error happened", vbCritical, "error"End SubPrivate Sub CommandButton2_Click()On Error GoTo errDim str As Stringstr = "test"MsgBox strDebug.Print 1 / 0Exit Suberr: MsgBox "some error happened", vbCritical, "error"End Sub
[解决办法]
private sub subName()
on error goto errSub:
exit sub
errSub:
end sub
[解决办法]
errsub只是一个标签,程序会接着往下执行的,如不想执行需在前面加exit sub
[解决办法]
需加exit sub