In this session, you will learn how to add comments to a VBA code. Comments are essential for keeping your code organized and making it easier to understand, both for yourself and others. We'll demonstrate two main methods to insert comments and explain why it is beneficial to include them in your macros.

What will you learn

  • How to add comments using apostrophes in VBA.
  • How to use the REM keyword to insert comments in VBA.
  • The importance of using comments in your code.
  • How comments can help segregate different sections or activities in a macro.
  • How comments enhance code readability and maintainability.

Takeaway notes

  • Comments can be added using an apostrophe ('), and the text following it will be recognized as a comment.
  • Comments can also be added using the REM keyword followed by the comment text.
  • Comments in VBA automatically turn green, making them easily distinguishable from the executable code.
  • Adding comments can help programmers keep track of the code and make it easier for others to understand or modify the code later.

Practice questions

  1. How do you add a comment to a line of VBA code?
  2. What is the alternative keyword to adding comments in VBA apart from using the apostrophe?
  3. Why is it important to add comments to your VBA code?
  4. Write a VBA line of code with a comment added using an apostrophe.
  5. Write a VBA line of code with a comment added using the REM keyword.
  6. Can comments be added in the middle of a line of code in VBA?
  7. How does Excel VBA distinguish a line of code as a comment?
  8. Create a macro that adds a given number of comments using both the apostrophe and REM keyword.
  9. Describe a scenario where adding a comment in a VBA macro would be particularly useful.
  10. What color are comments in the VBA editor by default?
  11. How can adding comments to VBA code assist when debugging?
  12. Write a VBA code snippet to demonstrate the use of comments for separating different sections of tasks.
  13. Why might comments be helpful when multiple programmers are working on the same VBA code file?
  14. Explain how comments can make the VBA code easier to maintain over time.
  15. How can comments aid in documenting changes made to a VBA macro?

Mark Lesson Complete (How to Add Comments in VBA Macros)