Hello Excellers. Welcome if you are new to my Excel Blog and welcome back if you are a regular reader. If you want to see all of the blog posts in the Macro Mondays series, they are all listed on this page right here.
Today let’s look at how to comment out a block of Excel 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 and not as code. Usually your commentary may look something like this. Which would help as a explanation as the to logic of your code for yourself at a later date or someone else picking up your code at some point in the future.
If 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.
In order for you to use this feature you do need to ensure that the correct toolbar is enabled in the Excel Developer Tab ad the Edit Toolbar. Here is how to enable it-
- Right Click on any toolbar
- Select Edit
- You now have the the toolbar you need to block comment on code.
- Select all or part of the lines of code you want to block as commentary.
- Then select Comment Block from the new Edit Menu Bar you just added.
- Excel will then block out all of the lines or partial lines selected and place an apostrophe which turns the code into comments and the code does not run. This saves manually putting an apostrophe at the start of each line of code you want to be treated as comments.
Dont forget to SUBSCRIBE to the How To Excel At Excel Newsletter for more tips the first Wednesday of the month.