调试VBA、定位问题及排查方法[关闭]

2024-06-19

有哪些方法调试VBA代码?

具体来说:

  • 单步执行代码
  • 断点和停止命令
  • TheDebug command
  • 当地人和观察窗
  • 调用栈

调试 VBA 代码

本页介绍了调试 VBA 代码的方法。


介绍

调试程序是软件开发中最重要的步骤之一。了解 VBA 的调试工具可以使调试变得更容易、更高效。本页介绍了在测试和调试应用程序时可以使用的几个 VBA 内置调试​​工具。


单步执行代码

One of the first methods to debug code is to step through the code one line at a time. To step through code, put the cursor on the first line of code to be analyzed and press F8 or choose Step Into on the Debug menu. The next line of code to be executed will be displayed in yellow background with a black font. Note that the highlighted line of code has not yet been executed -- it is the next line to execute.

If your code calls another procedure, stepping through the code with F8 will cause execution to enter the called procedure in a line-by-line sequence. If you want to execute the called procedure without stepping through it, press Shift+F8. This will execute the called procedure and then pause on the line of code after calling the procedure. If you are already stepping through a procedure, you can press Ctrl+ F8 to resume code execution line-by-line. At any time you are paused either in step-by-step mode or at a breakpoint (see below), you can press F5 or Continue from the Run menu to cause VBA to run to completion or until a pause statement is encountered.

每当您在逐步模式下暂停时,您都可以从即时窗口.


断点和停止命令

A breakpoint is a marker placed on a line of code that causes execution to pause immediately before executing that line. You can add a breakpoint to a line of code by putting the cursor on the line of code in question and pressing F9, choosing Toggle Breakpoint on the Debug menu, or clicking in the left margin next to the line of code. When a breakpoint is set, the line is displayed in brick-red background with a white font. When you run the code, execution will pause immediately before the line of code with the breakpoint and will display it in yellow background with a black font. Note than the line in yellow has not yet been executed -- it is the next line of code to run.

While the code is paused at the breakpoint, you can issue commands in the Immediate window to change or query a variable's value. To view the content of a variable, enter a ? character followed by the name of the variable and then press Enter. You can change a variable's value by entering VariableName = NewValue in the Immediate window and pressing Enter.

If the Immediate window is not visible (typically at the bottom of the VBA Editor screen), press Ctrl+G or choose Immediate Window from the View menu to make the window visible.

To remove a breakpoint, put the cursor on the line of code and press F9. You can clear all breakpoints by choosing Clear All Breakpoints from the Debug menu or pressing Ctrl+Shift+F9. VBA also provides the Stop command. This simply stops code execution on that line of code and enters break mode.

Once you are finished debugging the code, be sure to go back and clear all breakpoints (choose Clear All Breakpoints from the Debug menu or press Ctrl+Shift+F9) and be sure to remove or comment out all Stop statements.

当您在断点处或在单步模式下暂停时,您可以更改要执行的下一行,可以在当前行之前重新运行一段代码,也可以在该行之后跳过语句。右键单击要恢复执行的行,然后右键单击并选择设置下一条语句或选择设置下一条语句来自Run菜单。执行将在选定的代码行处恢复。


调试命令

VBA 提供了一个具有两个属性的 Debug 对象:Print 和 Assert,您可以使用它们显示变量的值并控制程序流程。Debug.Print会将其后面的内容写入立即窗口。代码执行不会被中断。在立即窗口中显示文本后,代码执行继续运行。您可以将文字文本与变量名称混合在一起Debug.Print陈述。例如,

Debug.Print "The value of variable X is: " & X

您可以在“立即”窗口中同时显示多个变量,并用逗号分隔它们。例如,

Debug.Print X, Y, Z

The Debug.Assertcommand 是一个条件断点,如果 Assert 语句后面的表达式为 False,则该断点将导致执行在 Debug 语句上暂停。例如,

Debug.Assert Var >= 0

这将暂停在Debug.Assert声明如果Var >= 0 is False;也就是说,如果Var是负数。当条件为 False 而不是 True 时暂停执行,这似乎是倒退的,但 Assert 方法是从 C 语言中采用的,其用法与 C 中相同。

请务必删除或注释掉Debug.Print and Debug.Assert调试完成后的语句。您通常不希望这些语句在应用程序的正常使用期间起作用。


当地人之窗

当您单步执行过程时,“局部变量”窗口允许您查看过程中所有变量的值。要显示本地窗口,请选择当地人之窗来自View菜单。使用“局部”窗口比检查“立即”窗口中的值更容易显示变量值。对于简单变量类型(例如,长整型变量和字符串变量),值显示在一行上。对于复杂类型或对象(例如 Range 变量),其属性显示在可折叠的树状结构中。


观察窗

监视窗口显示所有有效的监视。您可以通过选择来显示监视窗口观察窗来自View菜单。监视是 VBA 的一条指令,用于在表达式为 True 或正在监视的变量更改值时暂停代码。要在变量上创建监视,请打开“监视”窗口并在“监视”窗口中右键单击并选择添加手表...从弹出菜单中或选择添加手表...从调试窗口。在“添加监视”对话框中,在“表达式”文本框中输入要监视其值的变量名称。然后选择当值改变时中断。当您运行代码时,执行将在修改变量值的行之后暂停。当代码暂停时,变量的值将已经更新。

要删除手表,请在“手表”窗口中右键单击它,然后选择删除观察从弹出菜单中。要修改监视,请在监视窗口中右键单击它并选择编辑手表从弹出菜单中。


调用堆栈

调用堆栈是由 VBA 维护的数据结构,用于跟踪哪个过程调用了另一个过程。例如,如果程序AAA calls BBB哪个调用CCC,“调用堆栈”窗口将显示从最近的过程开始的过程列表,其下方是为到达当前位置而执行的过程链。您可以通过选择查看调用堆栈调用栈来自View菜单。这对于跟踪在当前位置结束的执行流程很有用。不幸的是,没有编程方法可以从调用堆栈中获取信息。


Source: Chip Pearson at Pearson Software Consulting: http://www.cpearson.com/Excel/DebuggingVBA.aspx http://www.cpearson.com/Excel/DebuggingVBA.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

调试VBA、定位问题及排查方法[关闭] 的相关文章

随机推荐

  • 如何通过异步调用更新列表框?

    我开发了一个 Windows 窗体 C 应用程序 我只想通过分拆另一个线程来更新主窗体中列表框中的项目 而不阻塞 GUI 窗体 由于线程无法访问列表框等表单实体 因此我想到使用委托 下面的代码显示了我如何使用委托来完成该任务 但它阻止了 G
  • 下载 .NET 3.5 的实体框架

    哪个版本的实体框架 EF 我可以用在 NET 3 5我可以在哪里下载这个旧版本 对于 Net 3 5 您可以使用 EF v1 您是否尝试从以下位置下载 Microsoft NET Framework 3 5 Service Pack 1 h
  • 错误代码:InvalidIntentSamplePhraseSlot -

    我收到错误代码Error code InvalidIntentSamplePhraseSlot当我使用新的技能控制台构建模型时 完整的错误消息是 Sample utterance AddBookmarkIntent i am at page
  • iFrame 未扩展至 100% 高度

    我有这个下面的html 我希望 iFrame 能够 100 覆盖屏幕的其余部分 我在高度属性中尝试了 100 和 但不起作用 这是为什么 谢谢 div img height 35 width 84 alt Kucku src Content
  • Passport-local-mongoose:createStrategy 不是函数/authenticate 不是函数

    我正在构建这个启动项目 https github com cj wang mean start tree 424e6056e33bb16874ae808daf3780d53309296f并尝试添加用户登录护照本地猫鼬 https www n
  • 位操作,排列位

    我正在尝试创建一个循环 循环遍历所有不同的整数 其中最后 40 位中的 10 位设置为高 其余设置为低 原因是我有一个包含 40 个不同值的地图 并且我想对其中 10 个值相乘的所有不同方式求和 这只是出于好奇 所以真正感兴趣的是 bitm
  • 如何同时运行多个功能[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有以下代码 my func1 my func2 my func3 my func4 my func5 是否可以同时计算函数的数据 而
  • 在 iOS 5 中,我们可以邀请人们使用我们的应用程序或通过 Facebook 从应用程序发送好友请求吗?

    考虑X and Y是朋友Facebook他们都在各自的手机中安装了一个应用程序 但他们在这一点上并不是朋友应用程序的好友列表 那么现在 可以X 发送好友请求 Use Facebook sdk 3 1 https developers fac
  • 如果使用 jQuery 添加字段,Rails 嵌套表单属性不会保存

    我有一个带有嵌套表单的 Rails 表单 我使用 Ryan Bates 嵌套表单和 jquery 教程 并且就动态添加新字段而言 它工作得很好 但是当我提交表单时 它不会保存任何关联的属性 但是 如果在表单加载时构建部分 则它会很好地创建属
  • @OneToMany 与 @JoinTable 错误

    我试图理解 OneToMany with JoinTable 对于这样的场景 我正在使用 JPA 2 1 Hibernate 5 0 4 和 Oracle 11 XE 当我打电话时userDao save user 下面的代码 我有 jav
  • 将其从 Google Maps API v2 转换为 v3

    这适用于 Google Maps API v2 现在如何将其转换为 API v3 谢谢 Radar 1 Overlay Tiles var radar layer new GTileLayer new GCopyrightCollectio
  • 从内存地址创建python对象(使用gi.repository)

    有时我需要调用仅存在于 C 中的 gtk gobject 函数 但返回一个具有 python 包装器的对象 之前我使用过基于 ctypes 的解决方案 效果很好 现在我从 PyGtk import gtk 切换到 GObject intro
  • DynamodB:如何更新排序键?

    该表有两个键 filename 分区键 和eventTime 排序键 我要更新eventTime对于某些filename Tried put item and update item 发送相同的filename与新的eventTime但这些
  • 如何通过aws-sdk(javascript或node)获取s3存储桶大小

    我尝试使用 javascript nodejs aws sdk 查找 获取 s3 存储桶信息 但没有找到这样的 api 如何通过 aws sdk javascript 或 node api 获取 s3 存储桶大小 信息 每天一次向 Clou
  • JavaScript 中的“new”关键字是什么?

    The newJavaScript 中的关键字第一次遇到时可能会很混乱 因为人们倾向于认为 JavaScript 不是面向对象的编程语言 它是什么 它解决什么问题 什么时候合适 什么时候不合适 它做了 5 件事 它创建一个新对象 这个对象的
  • 如何实现通用 switch/case,它也适用于一般 C++ 类型并且语法相似?

    在 C C 中 switch case仅将整型类型与编译时常量进行比较 不可能使用它们来比较用户 库定义的类型 例如std string与运行时值 为什么 switch 语句不能应用于字符串 https stackoverflow com
  • 在 Samsung Galaxy S 上调用 RingTonePreference 时出现 NullPointerException

    在 Samsung Galaxy 上调用 RingTonePreference 时 我收到以下消息 在其他手机上一切正常 java lang RuntimeException Unable to start activity Compone
  • ASP.NET Web API ActionFilter 示例

    我对整个 MVC 很陌生 正在考虑使用 ASP NET Web API 重新实现一些 WCF 服务 作为其中的一部分 我想实现一个操作过滤器来记录所有操作和异常以及计时 因此我认为我应该从操作过滤器开始 但是过滤器没有被调用 public
  • 如何识别 Java 中的不可变对象

    在我的代码中 我正在创建一个对象集合 这些对象将由各种线程以只有在对象不可变的情况下才安全的方式访问 当尝试将新对象插入到我的集合中时 我想测试它是否是不可变的 如果不是 我将抛出异常 我能做的一件事是检查一些众所周知的不可变类型 priv
  • 调试VBA、定位问题及排查方法[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 有哪些方法调试VBA代码 具体来说 单步执行代码 断点和停止命令 TheDebug command 当地人和观察窗 调用栈 调试 VB