Macro Mondays – Commenting Out A Block Of Excel VBA Code.


Hello, Excellers.  Welcome if you are new to my Excel Blog and welcome back if you are a regular reader.   So, today I will show you how to comment out a block of Excel VBA Code. Or in other words, make comments on the lines of code in your Excel macro.

Excel Macro

Why Make Comments In Excel VBA Code.

First, let’s look at why you would make comments in your VBA code. Why not just write the code and let it be?. There a few reasons that it is considered best practice to write comments in your VBA code. I will cover them below.

Reason 1. Thought Processes.

So, you understand your code now, but what if someone picks up your code a year or two down the line?. You may come back to the code in six months. Can you really 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 created for many different people.  I find it good practice to always comment on my code.

Reason 2. It Helps When Testing Your Code

By converting the code to comments you can actually test removing an 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. The line of code 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 apostrophe’s at the beginning of each line to ensure Excel sees it is commentary.  Alternatively, you can comment out a whole block of VBA code using this method here.

Reason 3. Clearer Thought Processes

Next, Commenting on your code as you write it can really help to think about what you are writing.  By commenting as you go, you think about what you want to achieve, then go ahead and are more likely to translate this into the code you are writing. You should obviously 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 especially useful with more complex code.

Commenting On Lines Of VBA Code.

The regular way to comment  VBA code is to type an apostrophe before the line of code or words that you want Excel to treat as comments only. This disregards them as code.  Usually, your commentary may look something like this.

Commenting on VBA code

Commenting On A Block Of Code.

However, you want to take a whole block of VBA code and force Excel to treat the whole block as commentary so the code is not executed, then there is a quick way to do this by using a feature called comment block.  I find this useful if I have found some code to use online, but need to either tidy it up or shorten it, without deleting and therefore losing any of the code.

Enabling The Correct Toolbar,

To be able to use this feature you do need to ensure that the correct toolbar is enabled in the Excel Developer Tab. You need to Edit Toolbar.
Here is how to enable it.

Right-click on any toolbar. Next, select Edit. The toolbar you need is now present.

Finally, select all or part of the lines of code you want to block as commentary. Select Comment Block from the new Edit Menu Bar that you just added. Excel then blocks out all of the lines or partial lines selected. It places an apostrophe which subsequently turns the code into comments. The code, therefore, does not run.

So, this saves manually putting an apostrophe at the start of each line of code you want to be treated as comments.

Click The Link Below To See All Blog Posts In The Macro Monday Series Below

How To Excel At Excel – Blog Posts.

Do You Need Help With An Excel Problem?.

Finally, I am pleased to announce I have teamed up with Excel Rescue,  where you can get help with Excel FAST.

20 PerCent Off Sale

Barbara

Barbara is an experienced data analyst with over 20 years of experience. Author of the How To Excel At Excel Top 50 Excel tips Ebook, YouTuber and Excel Blogger.

Recent Posts