如何在循环中调试 loop/moving 转发? (VBA)

How to debug a loop/moving forward in loop? (VBA)

我正在尝试调试 VBA 中的多层循环。想看循环的一步步过程,但是只能很好的进入循环。 (即,循环从 1 到 120,我希望在 i = 110 时看到它 运行)

我知道我可以在中断模式下使用 F8 键,但是有没有一种方法可以在不按住 F8 键的情况下继续循环,直到到达我需要的循环点?

谢谢

只是一些选择:

  1. 添加观察值并打破值(必须以正确的值为目标)
  2. 从 110 开始相关循环
  3. 在相关循环中添加带有STOPIf子句;仅举几例。

带有 If 子句的示例:

Option Explicit

Public Sub test()
    Dim i As Long
    For i = 1 To 120
        If i = 110 Then
            Debug.Print i
            Stop
        End If
    Next i
End Sub

如果我没理解错的话,也许你可以这样做:

Sub loop120()
Dim i As Integer

i = 0
Do
i = i + 1

    If i = 110 Then
        'Code goes here for when the loop has executed 120 times
        MsgBox "This is the 110th loop"
        '<---- stick a break point on your next line of code
    End If


Loop

End Sub

这样,您将得到指示第 110 次循环的消息框。当您按确定时,您的代码将到达断点,您将能够单步执行。