如何在调用代码时强制VBA编辑器进入中断模式?

end*_*and 5 ide excel vba editor excel-vba

我希望能够在调用VBA代码时打开/关闭选项以进入中断模式.我知道这样做的唯一方法是在代码的所有"入口点"设置断点,或者实际上这些方法中的每一个都调用一个单独的函数来进行调试.

"入境点"可能是按钮点击或工作表事件,其中有相当多的.

例如,我可以这样做:

Private Sub bt1Click()
    callThisOnEveryMethod
    'other code
End Sub
Private Sub bt2Click()
    callThisOnEveryMethod
    'other code
End Sub
'etc, repeat 100 times
Private Sub callThisOnEveryMethod()
    'set breakpoint on this method
End Sub
Run Code Online (Sandbox Code Playgroud)

这不是很理想,因为我依赖于我将其添加到每个方法和每个后续方法.我真的不相信自己能够以这种方式获得100%的支持,而且仅仅为了调试目的而且很混乱.我也可以在这里添加其他代码,甚至将它包装在一个if MY_GLOBAL_DEBUG_BOOLEAN then类型语句中,但我仍然需要将这个代码(或调用方法)添加到我编写的每个可以启动VBA执行的方法中.

想象一下,我可能有100个方法可能是VBA代码执行的开始.

每次我想要这样做时,在每个方法上设置和删除断点并不理想,因为要打开/关闭的数字.

我想要的是以某种方式告诉VBA"无论何时开始执行代码,不管断点,断言,并且不要求每个方法都有大量额外代码,立即中断并进入调试模式."

这可能吗?

Kaz*_*wor 10

有两种方法可以破解代码并进入调试模式:

a)简单的Stop指示:

Sub MyProcedure()
   '...any code here
   Stop     'execution will stop here, debugging will start here
   '...the rest of the code
End sub
Run Code Online (Sandbox Code Playgroud)

b)以Debug.Assert False这种方式:

Sub MyProcedure()
   '...any code here
   Debug.Assert False     'execution will stop here, debugging will start here
   '...the rest of the code
End sub
Run Code Online (Sandbox Code Playgroud)

但是,您可以使用任何条件Debug.Assert Condition- 每次条件将返回False代码将停止.一个样本:

Dim A
A=10
Debug.Assert A<>10   'stopping execution here... entering debugging mode
Run Code Online (Sandbox Code Playgroud)