Adding comments in VBA Excel helps document your code for yourself and others, making it easier to maintain, debug, and collaborate. Comments are ignored by the compiler and do not affect code execution. You don’t need any special tools or add-ins—just the VBA Editor built into Excel. Comments improve readability and are essential for professional code. This guide shows how to insert them correctly using both the apostrophe and Rem statement methods.

What You'll Need

  • Microsoft Excel (2010 or later)
  • A workbook with VBA code open in the Visual Basic Editor (VBE)
  • Basic familiarity with writing or editing macros

Step-by-Step

  1. Open the Excel workbook that contains your VBA code. Press Alt + F11 to open the Visual Basic Editor (VBE). If you don’t see the Project Explorer window, press Ctrl + R to display it.
  2. In the Project Explorer, locate the module or worksheet where your code is stored. Double-click the module (e.g., Module1) or the worksheet (e.g., Sheet1) to open its code window.
  3. Place your cursor on a blank line where you want to add a comment, or at the end of an existing line of code.
  4. Type a single apostrophe (') followed by your comment text. For example: ' This subroutine clears all data in Column A. Everything after the apostrophe on that line becomes a comment and appears in green text by default.
  5. Alternatively, you can use the Rem keyword at the start of a line. For example: Rem This is a Rem comment. Note that Rem must be the first word on the line and cannot be placed after code on the same line.
  6. To comment out an entire line of executable code, place the apostrophe at the beginning of the line. For example, change Range("A1").Clear to 'Range("A1").Clear. This disables the line without deleting it.
  7. To comment multiple consecutive lines, select them all, then press Ctrl + Shift + A. This automatically adds a leading apostrophe to each selected line. (This shortcut works only if you have the VBA Editor’s default keyboard mapping enabled.)
  8. To uncomment a line, delete the apostrophe at the start, or select multiple commented lines and press Ctrl + Shift + A again to remove the apostrophes.
  9. Save your workbook as a macro-enabled file (.xlsm) if it isn’t already. Go to File > Save As, then choose Excel Macro-Enabled Workbook (*.xlsm) from the file type dropdown.
  10. Close the VBA Editor and return to Excel. Your comments are now saved and will appear every time you reopen the VBA Editor.

Common Mistakes

  • Using double apostrophes ('') instead of a single one. Only one apostrophe is valid.
  • Trying to use Rem after code on the same line. Rem only works as the first word on a line.
  • Forgetting to save the file as .xlsm. Comments will disappear if you save as .xlsx because macros (and their comments) are stripped out.
  • Using forward slashes (//) like in JavaScript or C#. VBA does not recognize them as comments.
  • Commenting only part of a line without ending the comment properly. If you write Range("A1").Value = 10 ' Set value, it works—but if you accidentally add a quote inside the comment that breaks a string, it can cause errors.
  • Assuming comments are visible in Excel cells. Comments in VBA are only visible in the code editor, not in the worksheet.

Tips

  • Use comments to explain why code exists, not just what it does. For example, instead of ' Clear cell, write ' Clear cell to reset filter criteria before refresh.
  • Use consistent formatting. Start all comments with a capital letter and end with a period for professionalism.
  • Comment before complex logic blocks, such as loops or conditional statements, to help others follow your intent.
  • Use '*** or '================================ as visual separators between sections for better readability.
  • Remove outdated or redundant comments. Old comments that no longer match the code are misleading and worse than no comment at all.
  • Use the VBA Editor’s syntax highlighting: comments appear in green by default. If they don’t, check your editor theme under Tools > Options > Editor Format.
  • When sharing code, always include a header comment block at the top of each module with author name, date, and purpose. Example: ' Author: John Doe | Date: 2024-05-15 | Purpose: Automate monthly report export.

Troubleshooting

  • Comment doesn’t turn green: Make sure you typed a single apostrophe (') and not a quotation mark (") or another symbol. Check your keyboard layout. If using a non-US keyboard, ensure you’re typing the correct apostrophe character.
  • Ctrl+Shift+A doesn’t work: This shortcut may be disabled. Go to Tools > Options > Editor and ensure Use Control+Shift+A to Comment/Uncomment is checked. If it’s not listed, your VBA Editor version may not support it—use manual apostrophes instead.
  • Comments disappear after reopening: You likely saved the file as .xlsx. Reopen the file, press Alt + F11, verify your comments are still there, then save again as .xlsm.
  • Code won’t run after commenting: If you accidentally commented out a required line (like a Sub or End Sub), the code will fail to compile. Check for missing procedure boundaries. Use the Debug > Compile VBAProject menu to catch syntax errors.
  • Comment appears in red or wrong color: This may be due to a custom theme or corrupted VBA Editor settings. Reset by closing Excel, then reopening. If the issue persists, try resetting VBA preferences by deleting the Excel registry key for VBA (advanced users only).

Frequently Asked Questions

Can I use comments in Excel cell formulas?

No. Comments in VBA only work within the code editor. Excel cell formulas do not support comments. Use cell notes or data validation input messages for documentation within the worksheet.

Do comments slow down my VBA code?

No. Comments are completely ignored during execution. They have zero impact on performance, speed, or memory usage.

Can I insert multiline comments in VBA?

VBA doesn’t support block comments like /* */ in other languages. You must use a single apostrophe on each line. Select multiple lines and use Ctrl + Shift + A to comment them all at once.

What if I accidentally delete a line of code while commenting?

Immediately press Ctrl + Z to undo. Always test changes on a backup copy of your workbook before applying edits to production files.

Can I password-protect my VBA comments?

Yes, you can lock the VBA project from viewing by going to Tools > VBAProject Properties > Protection tab and checking Lock project for viewing. Set a password. This hides all code and comments from others.

Why are my comments not showing up in the VBA editor after pasting code?

If you copied code from a website or PDF, the apostrophes may have been converted to smart quotes (’) or Unicode characters. Retype the apostrophes manually. Always paste into Notepad first to strip formatting, then copy into VBA.