Comments in Code - Visual Basic (2024)

Table of Contents
In this article See also FAQs
  • Article

As you read the code examples, you often encounter the comment symbol ('). This symbol tells the Visual Basic compiler to ignore the text following it, or the comment. Comments are brief explanatory notes added to code for the benefit of those reading it.

It is good programming practice to begin all procedures with a brief comment describing the functional characteristics of the procedure (what it does). This is for your own benefit and the benefit of anyone else who examines the code. You should separate the implementation details (how the procedure does it) from comments that describe the functional characteristics. When you include implementation details in the description, remember to update them when you update the function.

Comments can follow a statement on the same line, or occupy an entire line. Both are illustrated in the following code.

' This is a comment beginning at the left edge of the screen.text1.Text = "Hi!" ' This is an inline comment.

If your comment requires more than one line, use the comment symbol on each line, as the following example illustrates.

' This comment is too long to fit on a single line, so we break ' it into two lines. Some comments might need three or more lines.

The following table provides general guidelines for what types of comments can precede a section of code. These are suggestions; Visual Basic does not enforce rules for adding comments. Write what works best, both for you and for anyone else who reads your code.

Comment typeComment description
PurposeDescribes what the procedure does (not how it does it)
AssumptionsLists each external variable, control, open file, or other element accessed by the procedure
EffectsLists each affected external variable, control, or file, and the effect it has (only if it is not obvious)
InputsSpecifies the purpose of the argument
ReturnsExplains the values returned by the procedure

Remember the following points:

  • Every important variable declaration should be preceded by a comment describing the use of the variable being declared.

  • Variables, controls, and procedures should be named clearly enough that commenting is needed only for complex implementation details.

  • Comments cannot follow a line-continuation sequence on the same line.

You can add or remove comment symbols for a block of code by selecting one or more lines of code and choosing the Comment (Comments in Code - Visual Basic (1)) and Uncomment (Comments in Code - Visual Basic (2)) buttons on the Edit toolbar.

Note

You can also add comments to your code by preceding the text with the REM keyword. However, the ' symbol and the Comment/Uncomment buttons are easier to use and require less space and memory.

See also

Comments in Code - Visual Basic (2024)

FAQs

Comments in Code - Visual Basic? ›

Use an apostrophe to mark the beginning of your comments.

How do you comment code in VB? ›

By using comment symbol ( ' ), we can comment on the code in our Visual Basic programming. The comment symbol ( ' ) will tell the Visual Basic compiler to ignore the text following it, or the comment.

How do you write comments in VBA? ›

To add a comment to your VBA macro code, simply type an apostrophe ( ' ) and start typing. All the remaining text in that code line will be considered a comment (non-executing code) and will typically turn a green color once you navigate to the next line.

How do you comment in Basic programming language? ›

C,C++,ActionScript, and Java

The single line comment is //. Everything from the // to the end of the line is a comment. To mark an entire region as a comment, use /* to start the comment and */ to end the comment.

How do you comment multiple lines in VB? ›

For a multiple-line comment, repeat the comment symbol ( ' ) on each line. Although placing each statement on a separate line is the recommended method, Visual Basic also allows you to place multiple statements on the same line.

How do I comment multiple lines in Visual Studio code? ›

Select the desired lines of code and then use Ctrl + / to comment them out in VS Code.

How do I comment and uncomment code in Visual Studio? ›

Ctrl + K + C : Comment out the selected code. Ctrl + K + U : Uncomment the selected code. Ctrl + / : Toggle line comments.

How do I add a comment button in Visual Studio? ›

Use the built in functionality in Visual Studio under Edit -> Advanced -> Comment Selection. The functionality is also available as toolbar buttons on the "Text Editor" toolbar, or via keyboard shortcuts [Ctrl+K, Ctrl+C] to comment and [Ctrl+K, Ctrl+U] to uncomment.

How do I comment in Visual Studio Code React? ›

To comment in React, you can use the syntax {/* */} . Anything you put between these two sets of curly braces will be treated as a comment and will not be executed by the JavaScript engine. Here's an example in action: https://codepen.io/shecodes/pen/dypYvXp.

What are the comments style used in VBA? ›

In VBA, comments must adhere to a specific format whereby each line of comment must commence with a ' symbol followed by text. Unfortunately, there is no streamlined approach for composing multiline comments in VBA.

How do you comment a few lines in VBA? ›

In Excel VBA, there are several ways to comment lines of a code:
  1. Single quotation (')
  2. Comment block button in the toolbar.
  3. Adding the Rem keyword.
Dec 15, 2021

How do you comment a whole paragraph in VBA? ›

How to comment a block of code in the Office VBA Editor
  1. Open the VBA Editor (Alt + F11).
  2. Right click anywhere on the toolbar.
  3. Select the “Edit” Toolbar.
  4. You will now be able to quickly and easily comment a block of code (or uncomment) using these new buttons on your toolbar:
  5. Done!
Nov 28, 2018

How do programmers comment their code? ›

Line comments either start with a comment delimiter and continue until the end of the line, or in some cases, start at a specific column (character line offset) in the source code, and continue until the end of the line. Some programming languages employ both block and line comments with different comment delimiters.

What is the comment function in code? ›

Code commenting is the practice of sprinkling short, normally single-line notes throughout your code. These notes are called comments. They explain how your program works, and your intentions behind it. Notice how the comment on the first line describes the line of code below it.

What is the example of comment? ›

A comment is something that you say which expresses your opinion of something or which gives an explanation of it. He made his comments at a news conference in Amsterdam. I was wondering whether you had any comments about that? There's been no comment so far from police about the allegations.

How do I comment multiple lines in VBScript? ›

Unfortunately, you cannot block comment VBScript like you can in other languages. You can comment out each line individually. Just put a single quotation mark at the start of the line you want to 'out' and do then do the same for each subsequent line.

What is the comment symbol in VB net? ›

Two comment symbols are used: the apostrophe ( ' ) and the Rem keyword. They may appear at any place within a line.

Top Articles
Latest Posts
Article information

Author: Virgilio Hermann JD

Last Updated:

Views: 6074

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Virgilio Hermann JD

Birthday: 1997-12-21

Address: 6946 Schoen Cove, Sipesshire, MO 55944

Phone: +3763365785260

Job: Accounting Engineer

Hobby: Web surfing, Rafting, Dowsing, Stand-up comedy, Ghost hunting, Swimming, Amateur radio

Introduction: My name is Virgilio Hermann JD, I am a fine, gifted, beautiful, encouraging, kind, talented, zealous person who loves writing and wants to share my knowledge and understanding with you.