Comment Out VBA Code Blocks

  • We all know that adding a ' (single quote) before the contents of a line, or part of a line, in the VBE will make it a comment (shows in green by default).

    It is often useful when testing your code to comment-out various bits of code so that they don't run. When I first started VBA coding I would scroll down adding one quote at a time to each line concerned. (And then deleting each quote after testing).

    There is a much easier way (isn't there always?) available from the Edit menu commands but the options aren't shown by default. There are 'Comment Block' and 'Uncomment Block' options that let you add or delete comment indicators to selected blocks of code.

    To make these easily accessable try this:
    1. In the VBE go to View/Toolbars/Customise.
    2. Select the Commands tab and then the Edit category.
    3. Scroll down the list of commands until you see Comment Block and Uncomment Block.
    4. Press and hold the left mouse button when over the command and drag it to your Standard toolbar (and then let go of the mouse button!).
    5. Repeat for the other option.

    Of course you could just make the Edit toolbar visible - it depends on how much screen area you have available. Either way, these commands are very useful.

  • Re: Comment Out VBA Code Blocks

    wow, thanks for that!

    It's sad how a person can use a product for so long (feels like centuries) and be annoyed every time that I've put the comments in individually (cursing that I can't use something like /* */ to surround entire blocks and right in the middle of the options list there they were; Mr & Mrs comment and uncomment :)