Choose your language

VBA Code Comments

Commenting on a code means inserting information inside the VBE that will not be executed (the information is used for guidance only).

This practice has some advantages:

• It helps other users to understand the code, saving time on explanations
• It helps to remember the purpose of a particular code
• It prevents Excel from executing unwanted parts of the code when testing it

To comment a code just use a simple apostrophe symbol. Everything that comes after the ' sign will be considered a comment:

    'This is a comment


We can also apply it to parts of the code to temporarily disable them:

Sub Comment()

'MsgBox "This will not run"
MsgBox "But this will run"

End Sub


When executing, the following text box should appear:

A comment may also be inserted into the same line after a code that will be executed.

Sub CommentAfter()

MsgBox "This will be shown" 'And this comment will not interfere

End Sub


After the apostrophe, the rest of the line will become comment.

Commenting on a Code Block

With the Edit Toolbar you can insert and remove comment blocks, just underline the code and use the buttons: Comment block and Uncomment block .

    'Sub CommentBlock()
'
'    MsgBox "This will not be shown" 'And this comment will not interfere
'
'End Sub


To add the Edit Toolbar in VBE go to: View $\rightarrow$ Toolbars $\rightarrow$ Edit.

Important Shortcuts in VBA Excel

Some shortcuts that will help save time:

• +: Show all macros
• +: Open VBE or return from VBE to Excel

Shortcuts that will only work in the VBE environment:

• +: Close the VBE and return to Excel
• : Display help
• : Object browser
• : Properties window
• : Run macro
• : Display code
• : Run a macro one step (line) at a time Important for learning and understanding
• +: Project Explorer
• +: Immediate window

SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.

Excel ® is a registered trademark of the Microsoft Corporation.

© 2019 SuperExcelVBA | ABOUT