如何检查VBA代码的哪一行导致错误 [英] How to check which line of VBA code is causing errors

查看:484
本文介绍了如何检查VBA代码的哪一行导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试我编写的长代码,我需要一步一步地进行操作.

I am trying to debug a long code I wrote and I need to step line by line.

问题是我在Mac上,在这种情况下不知道如何使用F8.谁能告诉我否则我该怎么办?我怎么知道哪一行导致执行问题?

The thing is I am on a mac and don't know how to use an F8 in that case. Could anyone tell me how can I do that otherwise and how do I know which line is causing problems with execution?

推荐答案

要检查哪行给您错误,可以使用ERL属性.请参见下面的示例代码.

To check which line is giving you the error, you can use the ERL property. See this sample code below.

Sub sample()
Dim i As Long

On Error GoTo Whoa

10    Debug.Print "A"
20    Debug.Print "B"
30    i = "Sid"
40    Debug.Print "A"

50    Exit Sub
Whoa:
    MsgBox "Error on Line : " & Erl
End Sub

要执行此操作,您将必须像上面一样对代码行进行编号.运行上面的代码,看看会发生什么.

For this to work, you will have to number the code lines as I have done above. Run the above code and see what happens.

这篇关于如何检查VBA代码的哪一行导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆