以编程方式在 VBA 代码中设置断点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11634957/
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-11 17:01:20  来源:igfitidea点击:

Set breakpoint in VBA code programmatically

vbaexcel-vbabreakpointsexcel

提问by SMir

I have a very large piece of code written in VBA (>50,000 lines - numerous modules). There is one array of interest to me, and I'd like to find all the conditions under which the value of any element of this array changes. The values can change in any module. Running the script line by line is not the most efficient option due to the size of the code.

我有一段用 VBA 编写的非常大的代码(> 50,000 行 - 大量模块)。我对一个数组感兴趣,我想找出该数组中任何元素的值发生变化的所有条件。这些值可以在任何模块中更改。由于代码的大小,逐行运行脚本并不是最有效的选择。

I am looking for better ways to solve this problem. Two ways that come to my mind is to programmatically set a breakpoint (which I am not sure if can be done) or programmatically insert an if-block after each assignment that somehow alerts me that the value has changed. (not preferred).

我正在寻找更好的方法来解决这个问题。我想到的两种方法是以编程方式设置断点(我不确定是否可以完成)或以编程方式在每次赋值后插入一个 if 块,以某种方式提醒我值已更改。(不是首选)。

So my question boils down to:

所以我的问题归结为:

  1. Is it possible to programmatically set breakpoints in VBA code?
  2. If the answer to the above question is No, what is an efficient way to solve this problem?
  1. 是否可以在 VBA 代码中以编程方式设置断点?
  2. 如果上述问题的答案是否定的,那么解决这个问题的有效方法是什么?

UPDATE:Thanks for the comments/replies. As I had implied, I am interested in the least amount of modification to the current code (i.e. inserting if-blocks, etc) and most interested in the break-point idea. I'd like to know if it's doable.

更新:感谢您的评论/回复。正如我暗示的那样,我对当前代码的最少修改(即插入 if 块等)感兴趣,对断点想法最感兴趣。我想知道它是否可行。

回答by Trace

Use the keyword STOPto break te code if a certain condition is true.

STOP如果某个条件为真,则使用关键字来破坏代码。

回答by Visitor

NB:I know this is an old topic but this could help others.

注意:我知道这是一个古老的话题,但这可以帮助其他人。

You could use Watches:

你可以使用手表:

Right click on the variables you wish to monitor -> Add Watch... In Watch Type: 'Break when value changes'

右键单击要监视的变量 -> 添加监视...在监视类型中:“值更改时中断”

While you run your code, you can check the status of your Watches thanks to the Watch Window (accessible from the 'View' menu)

在运行代码时,您可以通过 Watch Window(可从“查看”菜单访问)检查手表的状态

回答by user1956304

in the hope someone can benefit from this : In such situations regardless of the programming language used - writing a few lines of code either in Perl, AWK or even shell scripts can solve the problem : search for a regular expression containing the array name (ignoring case). Once you export all modules and classes in the Workbook(s) into a given directory - the scripts can search those for you.

希望有人能从中受益:在这种情况下,无论使用何种编程语言 - 在 Perl、AWK 甚至 shell 脚本中编写几行代码都可以解决问题:搜索包含数组名称的正则表达式(忽略案件)。一旦您将工作簿中的所有模块和类导出到给定目录中 - 脚本可以为您搜索这些。