How to check which line of VBA code is causing errors

╄→гoц情女王★ 提交于 2019-12-10 01:39:45

问题


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

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?


回答1:


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.




回答2:


Sub Main()

    Dim lNum As Long

    On Error GoTo ErrHandler

    lNum = 1 / 0

ErrExit:
    Exit Sub

ErrHandler:
    Debug.Print Err.Description
    Stop
    Resume

End Sub

When you get to Stop, then Step Into twice. If you don't have F8, you should have a menu item for stepping into a line. Resume will take you back to the line that caused the error.




回答3:


  1. Right click the toolbar.
  2. Choose "Customize..."
  3. Select "Debug"
  4. Drag "Step Into" into your toolbar.


来源:https://stackoverflow.com/questions/16398261/how-to-check-which-line-of-vba-code-is-causing-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!