在 VBA 上使用注释

Glen Alfaro 2023年1月30日 VBA VBA Comment
  1. 在 VBA 中在每行添加 ' 以进行注释
  2. 使用 VBA 上的 Comment Block 按钮
  3. 使用 VBA 上的 Uncomment Block 按钮
在 VBA 上使用注释

注释是描述代码逻辑和算法的人类可读文本。在代码块上添加注释,你正在编写/调试是一种很好的编码实践。

它将帮助你和未来的开发人员轻松理解代码的逻辑。多产的程序员通过构建他们的脚本并在每个代码块上放置描述性注释来区分。

本文将演示如何有效地在 VBA 上创建注释。

在 VBA 中,我们只使用单行注释或者只对单行代码有效的注释。

撇号 (') 用于指示注释行的开头。

VBA 中处理注释的三种方法如下。

  1. 在注释行的开头手动添加'
  2. 利用 VBA 上的 Comment Block 按钮
  3. 利用 VBA 上的 Uncomment Block 按钮

在 VBA 中在每行添加 ' 以进行注释

在此方法中,你可以通过在要成为评论的行的开头手动键入撇号'来创建评论。

例子:

Sub test()
'This is a comment.
End Sub

如果要将注释扩展到下一行,可以在行尾添加一个续行字符 (_)。

例子:

Sub test()
'This is a comment. _
This is still a comment.
End Sub

使用 VBA 上的 Comment Block 按钮

在此方法中,我们需要突出显示要转换为注释的代码,然后单击工具栏中的 Comment Block 按钮。

如果评论块按钮不在工具栏中,你可能需要执行以下步骤:

  1. 转到查看然后工具栏
  2. 选择编辑。将出现一个带有评论块取消评论块按钮的新工具栏。

例子:

Sub test()
This text should be highlighted, then click Comment Block Button to be converted to comments.
End Sub

输出:

Sub test()
'This text should be highlighted, then click Comment Block Button to be converted to comments.
End Sub

使用 VBA 上的 Uncomment Block 按钮

例子:

Sub test()
'This text should be highlighted, then click Uncomment Block Button to be converted to comments.
End Sub

输出:

Sub test()
This text should be highlighted, then click Uncomment Block Button to be converted to comments.
End Sub
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe