Hello, Excellers, it’s time for another blog post in the #MacroMonday series. Today, I will go through my top 3 reasons to use comments in your Excel VBA Code. This is sometimes also referred to as commenting out a block of code.
Top 3 Reasons to Use Comments In Your Excel VBA Code.
So, let’s look at my top 3 reasons to use comments or commentary in your Excel VBA code.
When looking at Excel VBA code, you may have noticed that some of the code is (usually) in green. These are the comments that the writer of the code has inserted when writing the code. These comments are completely ignored when the code is run. It does not affect the performance of your code. Any text that follows an apostrophe is considered a comment. So let’s run through my Top 3 reasons to use comments on your code.
Reminder Of Your Thought Processes.
So, you understand your code now, but what if someone picks up your code a year or two down the line, or you come back to the code in six months, can you remember exactly the stream of thought you had at the time you wrote the code?. Probably not. I know that I certainly do not, after all; I create a lot of Excel solutions and code for many different people. I find it good practice to always comment on my code.
It Helps When Testing Your Code.
By converting the code to comments, you can use this to test removing and adding pieces of code. For example, by adding an apostrophe character to the beginning of a line of code and hitting Enter, the code will turn green and will now be seen by Excel as a comment and not as code. If you have code over multiple lines, you will need to add apostrophes at the beginning of each line to ensure Excel sees it as commentary. Alternatively, you can comment out a whole block of VBA code using this method here.
Here is an example. I have recorded a Macro which turns the text in cells A2:B5 green. You can see that when I code using the Macro Recorder, I get a lot of extra details in the code that you may not want to include. By commenting out pieces, you can test if you need them, then decide to delete them or save them for a later date.
In this example, I have highlighted the extra coding below, which I added after creating my code using the Excel Macro Recorder.

To see if the Macro is affected by removing this extra code, we can put an apostrophe in front of that line of code.
It does not affect it. So, I can either leave it commented out or remove it altogether from the code. This time, I think I will leave it.
Clearer Thought Processes.
Use comments In Your Excel VBA code, as you write it, can help you think about what you are writing. By commenting as you go, you think about what you want to achieve. I believe you are more likely to translate this into the code you are writing. You should comment on any changes that you make at a later date, also to track development. Comments should be used to describe what you are doing, but probably more importantly, why you are doing it. They are beneficial with more complex code.
So, those are my Top 3 reasons for using code comments when writing VBA. Do you use comments when you write your VBA code?
IF you want to see all of the blog posts in the Macro Mondays Series, Click The Link Below