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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19797828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:06:52  来源:igfitidea点击:

How to force VBA editor to enter break mode whenever code is called?

vbaexcel-vbaideeditorexcel

提问by enderland

I would like to be able to toggle on/off the option to enter break mode whenever my VBA code is called. The only way I know to do this is set breakpoints at all the "points of entry" into code or have literally every one of these methods call a separate function for purposes of debug.

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

The "points of entry" may be button clicks or worksheet events and there are a fair number of them.

“入口点”可能是按钮点击或工作表事件,并且有相当多的事件。

For example, I could do:

例如,我可以这样做:

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

This is not really ideal since I am depending on me adding that to each method and every subsequent method. I don't really trust myself to get 100% of them this way and it's a lot of clutter for debug purposes only. I can add other code here too and even wrap it in an if MY_GLOBAL_DEBUG_BOOLEAN thentype statement but I still need to add this code (or the calling method) to everymethod I write which could start VBA execution.

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

Imagine I might have 100 methods which could be the start of VBA code executing.

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

It is not ideal to setup and remove break points on EVERY method each time I want to do this, either, because of the number to turn on/off.

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

What I would like is to tell VBA somehow "whenever you start executing code, immediately break and go into debug mode regardless of breakpoints, assertions, and without requiring each method to have lots of extra code."

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

Is this possible?

这可能吗?

回答by Kazimierz Jawor

There are two options to break code and go into debug mode:

有两个选项可以中断代码并进入调试模式:

a) with simple Stopinstruction:

a) 简单的Stop指令:

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

b) with Debug.Assert Falsein this way:

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

However, you could use any condition working with Debug.Assert Condition- each time condition will return Falsethe code will stop. One sample:

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

Dim A
A=10
Debug.Assert A<>10   'stopping execution here... entering debugging mode

回答by Jean-Fran?ois Corbett

I'm afraid the answer is no, there is no way to do this without inserting code or breakpoints in each point-of-entry procedure.

恐怕答案是否定的,如果不在每个入口点过程中插入代码或断点,就无法做到这一点。

Why isn't there a way to do this? Well, the real question is, why shouldthere be a way to do this? What's the point? Perpetual debug mode makes no sense. The idea of debug mode is to enter it if and only if you need to debug a specific procedure or error. I'm not sure what you're trying to achieve exactly?

为什么没有办法做到这一点?那么,真正的问题是,为什么应该有办法做到这一点?重点是什么?永久调试模式没有意义。调试模式的想法是当且仅当您需要调试特定过程或错误时才进入它。我不确定你到底想要达到什么目标?

Anyhow, the way you suggest, with callThisOnEveryMethod, is probably the closest you can get. Or actually, no need to define a new procedure: just use

无论如何,您建议的方式callThisOnEveryMethod, 可能是您能得到的最接近的方式。或者实际上,不需要定义一个新过程:只需使用

Debug.Assert MY_GLOBAL_DEBUG_BOOLEAN

at the top of your point-of-entry procedures, where MY_GLOBAL_DEBUG_BOOLEANis a public module-level constant that must be set to False in order to enter debug mode.

在入口点程序的顶部,其中MY_GLOBAL_DEBUG_BOOLEAN是公共模块级常量,必须将其设置为 False 才能进入调试模式。