在 VBA 上使用註釋
Glen Alfaro
2023年1月30日
VBA
VBA Comment

註釋是描述程式碼邏輯和演算法的人類可讀文字。在程式碼塊上新增註釋,你正在編寫/除錯是一種很好的編碼實踐。
它將幫助你和未來的開發人員輕鬆理解程式碼的邏輯。多產的程式設計師通過構建他們的指令碼並在每個程式碼塊上放置描述性註釋來區分。
本文將演示如何有效地在 VBA 上建立註釋。
在 VBA 中,我們只使用單行註釋或者只對單行程式碼有效的註釋。
撇號 ('
) 用於指示註釋行的開頭。
VBA 中處理註釋的三種方法如下。
- 在註釋行的開頭手動新增
'
- 利用 VBA 上的
Comment Block
按鈕 - 利用 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
按鈕。
如果評論塊
按鈕不在工具欄中,你可能需要執行以下步驟:
- 轉到
檢視
然後工具欄
- 選擇
編輯
。將出現一個帶有評論塊
和取消評論塊
按鈕的新工具欄。
例子:
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