Comments are an important part of VBA programming. A comment is text written inside VBA code to explain what the code does.
VBA ignores comments when the program runs. Comments are written mainly for programmers so that the code is easier to understand, maintain, and modify.
A comment is a line of text in VBA code that explains the purpose or meaning of the code.
For example:
' Display a welcome message
The line starts with an apostrophe, so VBA treats it as a comment.
Comments help programmers understand code more easily.
They are useful for:
The most common way to create a comment in VBA is to use an apostrophe (').
' This is a comment
VBA ignores everything after the apostrophe on that line.
Consider the following program:
Sub Welcome()
' Display welcome message
MsgBox "Welcome to VBA"
End Sub
The line ' Display welcome message explains what the next statement does.
Comments are not executed when the VBA program runs.
Sub Test()
' This line is only a comment
MsgBox "Hello"
End Sub
Only the MsgBox statement is executed.
A comment can be placed before a VBA statement.
Sub CalculateTotal()
' Add two numbers
Range("A1").Value = 10 + 20
End Sub
The comment explains the purpose of the statement below it.
A comment can also be placed after a VBA statement on the same line.
Sub CalculateTotal()
Range("A1").Value = 10 + 20 ' Calculate total
End Sub
Everything after the apostrophe is treated as a comment.
You can use multiple comments in one Sub Procedure.
Sub StudentDetails()
' Enter student ID
Range("A1").Value = 101
' Enter student name
Range("B1").Value = "Amit"
' Enter course
Range("C1").Value = "ADCA"
End Sub
Each comment explains the purpose of the following statement.
Comments can explain the purpose of variables.
Sub StudentMarks()
Dim marks As Integer ' Stores student marks
marks = 85
MsgBox marks
End Sub
This makes it easier to understand what the variable is used for.
Comments can explain calculations.
Sub Percentage()
' Calculate percentage
Range("C2").Value = Range("B2").Value / 500 * 100
End Sub
The comment tells the reader what calculation is being performed.
Comments can explain operations performed on worksheet cells.
Sub WriteStudent()
' Write student name into cell B2
Range("B2").Value = "Rahul"
End Sub
This is especially useful in larger Excel automation projects.
Comments can describe formatting instructions.
Sub FormatHeading()
' Make heading bold
Range("A1:C1").Font.Bold = True
' Increase font size
Range("A1:C1").Font.Size = 14
End Sub
Comments can explain the purpose of a loop.
Sub PrintNumbers()
' Repeat the process from 1 to 10
Dim i As Integer
For i = 1 To 10
' Display the current number
Cells(i, 1).Value = i
Next i
End Sub
Comments can make loop logic easier to understand.
Comments can explain the purpose of an If statement.
Sub CheckMarks()
Dim marks As Integer
marks = Range("A1").Value
' Check whether the student passed
If marks >= 40 Then
MsgBox "Pass"
End If
End Sub
You can add a comment at the beginning of a procedure to describe its overall purpose.
Sub GenerateReport()
' This procedure creates a simple student report
Range("A1").Value = "Student Report"
Range("A2").Value = "Total Students"
Range("B2").Value = 50
End Sub
Comments can be used as section headings inside a larger procedure.
Sub StudentReport()
' ===== Student Information =====
Range("A1").Value = "Rahul"
' ===== Marks =====
Range("B1").Value = 85
' ===== Result =====
Range("C1").Value = "Pass"
End Sub
Section comments make large procedures easier to navigate.
A line of VBA code can be temporarily disabled by adding an apostrophe before it.
Sub Test()
'MsgBox "This message is disabled"
MsgBox "This message will appear"
End Sub
The first MsgBox does not run because it has been converted into a comment.
Comments can be useful while testing and debugging a VBA program.
For example, if you want to temporarily stop one instruction from running, you can comment it out:
'Range("A1").Value = 100
The instruction remains in the code but is not executed.
When you record a macro, Excel can generate several VBA statements. Comments can be added later to explain important parts of the recorded code.
Sub FormatTable()
' Select the table heading
Range("A1:C1").Select
' Apply bold formatting
Selection.Font.Bold = True
End Sub
Comments are especially useful in student projects.
For example:
Sub CalculateResult()
' Store marks
Dim marks As Integer
marks = Range("B2").Value
' Check the result
If marks >= 40 Then
MsgBox "Pass"
Else
MsgBox "Fail"
End If
End Sub
Readable code is easier to understand and maintain. Comments can provide additional information when the purpose of a statement is not immediately obvious.
For example:
' Calculate the percentage based on total marks of 500
percentage = marks / 500 * 100
The comment explains the formula clearly.
Comments should provide useful information.
Useful comment:
' Calculate final percentage
percentage = total / 500 * 100
Less useful comment:
' Set percentage equal to total divided by 500 multiplied by 100
percentage = total / 500 * 100
Avoid writing comments that simply repeat obvious code without adding useful information.
A large VBA project may contain many modules and procedures. Comments can help explain the purpose of important sections.
' ==================================
' Student Management System
' ==================================
Sub AddStudent()
' Add a new student record
End Sub
This type of documentation can make a project easier to maintain.
When a VBA program is modified later, comments can help explain why particular code exists.
For example:
' Use column D because column C contains the course name
Range("D2").Value = 85
Such information can help when another person works on the project.
Comments can also explain how a procedure is intended to be used.
Sub GenerateReport()
' Run this procedure after entering all student marks
MsgBox "Report Generated"
End Sub
This gives the programmer useful information about the procedure.
Beginners sometimes make mistakes when using comments.
Keep comments accurate, useful, and easy to understand.
Let's create a simple student result program with comments.
Sub StudentResult()
' Store student marks
Dim marks As Integer
marks = Range("B2").Value
' Check whether the student has passed
If marks >= 40 Then
' Display pass message
MsgBox "Student Passed"
Else
' Display fail message
MsgBox "Student Failed"
End If
End Sub
The comments explain the purpose of each important section.
A simple approach to using comments in VBA is:
A VBA comment is text used to explain VBA code. The most common way to create a comment is to use an apostrophe (').
Comments are ignored by VBA during execution. They can be used to explain variables, calculations, conditions, loops, formatting, procedures, and larger sections of a VBA project.
Good comments improve code readability and make VBA projects easier to understand and maintain.
Question: Which symbol is commonly used to create a comment in VBA?