Excel VBAdebugging方法

我目前有一个文本框和一个button的forms。 您可以在文本框中input任意数字,当您按下button时,它会在消息框中显示数字,但是如果出现错误,则会popup一条消息,您可以单击debuggingbutton以查看哪条线路发生了错误。 这将是代码:

On Error GoTo 0: 

但是,如果我将“error:”replace为0,它将带我到一个新的子例程,将发送给我一个关于这个问题的电子邮件,但它不会突出显示当我点击debuggingbutton时给我一个错误的行。

有没有办法,我可以发送自己的电子邮件,当我点击debuggingbutton,它会突出显示错误行?

 Private Sub CommandButton1_Click() 'on error goto error: On Error GoTo 0: Dim word As Double word = TextBox1.Text MsgBox word Exit Sub error: Call error() End Sub Sub error() Dim OutApp As Object Dim OutMail As Object Dim strbody As String Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) strbody = "error" With OutMail .To = "blahblah@hotmail.com" .CC = "" .BCC = "" .Subject = "error" .Body = strbody .Send End With Set OutMail = Nothing Set OutApp = Nothing Debug.Assert (Err.Number & Err.Description) End Sub 

下面的模式调用error()然后在导致错误的行上的IDE中断:

 Sub Foo() On Error GoTo ERR_HANDLER Dim i As Long i = i / 0 Exit Sub ERR_HANDLER: Call error() On Error GoTo 0 Resume End Sub