How can I comment a block of code in VBA with an example? 2

How can I comment a block of code in VBA with an example?

Commenting a block of code in VBA is a useful way to explain the purpose and functionality of the code to other programmers or to remind yourself of its purpose in the future. To comment a block of code in VBA, you can use the ‘ character at the beginning of each line or the Rem statement at the beginning of the block of code. For example, to comment a block of code that calculates the sum of two numbers, you can use the ‘ character as follows:

‘ This block of code calculates the sum of two numbers
Dim num1 As Integer
Dim num2 As Integer
Dim sum As Integer

num1 = 5
num2 = 10
sum = num1 + num2 ‘ Calculate the sum of num1 and num2

Alternatively, you can use the Rem statement as follows:

Rem This block of code calculates the sum of two numbers
Dim num1 As Integer
Dim num2 As Integer
Dim sum As Integer

num1 = 5
num2 = 10
sum = num1 + num2 ‘ Calculate the sum of num1 and num2

Using either method, the commented block of code will not be executed when the program is run, but can still be viewed and understood by other programmers or yourself.

Comment a Block of Code in VBA (With Example)


You can use a single quotation ( ) in VBA to comment out a single line of code.

However, there is no built-in option in VBA to comment out a block of code.

Fortunately, there is a neat way to create a shortcut key that you can use to comment out a block of code and the following step-by-step example shows how.

Step 1: Customize the Toolbar

Within the VB Editor, right click anywhere on the toolbar and then click Customize:

Step 2: Add Comment Block to Toolbar

Click the Commands tab, then under Categories click Edit, then under Commands click Comment Block.

Then drag the Comment Block option onto the Toolbar. An icon should now appear for the comment block on the toolbar:

Step 3: Create Comment Block Shortcut Key

Next, click the Comment block icon on the toolbar (a black box will appear around the icon).

In the Customize panel, click the Modify Selection dropdown, then in the Name field type an ampersand ( & ) symbol in front of Comment Block and press Enter:

Then click the Modify Selection dropdown again and click Image and Text from the options:

VBA comment block shortcut keys

You will now be able to highlight a block of code and press Alt + C to comment out the entire block.

Step 4: Use the Comment Block Shortcut Key

Suppose we would like to comment out the entire block of code in the following sub procedure:

Once we’ve highlighted this block of code, we can simply press Alt + C and the entire block will be commented out:

VBA comment block

Note that you can also create a shortcut key to uncomment a block of code by repeating the same steps above for the Uncomment Block option.

Once you’ve done so, you will then be able to highlight a block of code and press Alt + U to uncomment out the entire block.

Cite this article

stats writer (2024). How can I comment a block of code in VBA with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-comment-a-block-of-code-in-vba-with-an-example/

stats writer. "How can I comment a block of code in VBA with an example?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-comment-a-block-of-code-in-vba-with-an-example/.

stats writer. "How can I comment a block of code in VBA with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-comment-a-block-of-code-in-vba-with-an-example/.

stats writer (2024) 'How can I comment a block of code in VBA with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-comment-a-block-of-code-in-vba-with-an-example/.

[1] stats writer, "How can I comment a block of code in VBA with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I comment a block of code in VBA with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top