Lesson 24 of 60 – VBA Comments
40%

VBA Comments

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.

Note: Comments do not affect the execution of your VBA program.

1. What is a Comment?

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.

2. Why Use Comments?

Comments help programmers understand code more easily.

They are useful for:

  • Explaining what code does
  • Describing important calculations
  • Documenting a project
  • Making code easier to maintain
  • Helping beginners understand programs

3. Apostrophe for Comments

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.

4. Simple Comment Example

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.

5. Comments Are Ignored by VBA

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.

6. Comment Before a Statement

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.

7. Comment After a Statement

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.

8. Multiple Comments

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.

9. Commenting Variables

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.

10. Commenting Calculations

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.

11. Commenting Cell Operations

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.

12. Commenting Formatting Code

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

13. Commenting Loops

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.

14. Commenting Conditional Statements

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

15. Commenting a Complete Procedure

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

16. Commenting Sections of Code

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.

17. Temporarily Disabling Code

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.

18. Commenting During Debugging

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.

19. Commenting Recorded VBA Code

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

20. Comments in Student Projects

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

21. Comments and Code Readability

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.

22. Good Comments vs Unnecessary Comments

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.

23. Commenting a VBA Project

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.

24. Comments and Maintenance

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.

25. Commenting User Instructions

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.

26. Common Mistakes with Comments

Beginners sometimes make mistakes when using comments.

  • Forgetting the apostrophe.
  • Writing comments that are too long.
  • Adding comments to every obvious line.
  • Leaving outdated comments after changing code.
  • Using comments instead of explaining the actual logic clearly.

Keep comments accurate, useful, and easy to understand.

27. Practical Example

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.

28. Best Practices for VBA Comments

  • Write comments that explain the purpose of code.
  • Keep comments short and clear.
  • Use comments for complex calculations.
  • Use comments to separate logical sections.
  • Update comments when the code changes.
  • Use comments to document important project information.

29. Complete Commenting Workflow

A simple approach to using comments in VBA is:

  1. Write the VBA procedure.
  2. Identify important or complex code.
  3. Add comments using the apostrophe.
  4. Explain calculations and important logic.
  5. Use section comments for larger procedures.
  6. Test the code.
  7. Update comments when the code changes.

30. Complete Understanding of VBA Comments

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.

📌 Key Points

  • Comments are used to explain VBA code.
  • The apostrophe (') is commonly used to create a comment.
  • VBA ignores comments during program execution.
  • Comments can be placed before a statement.
  • Comments can also be placed after a statement.
  • Comments are useful for explaining calculations and logic.
  • Comments can temporarily disable a line of code.
  • Good comments improve code readability.
  • Comments are useful for debugging and maintaining projects.
  • Comments should be clear, useful, and accurate.

🧠 Quick Quiz

Question: Which symbol is commonly used to create a comment in VBA?