Does VBA contain a comment block syntax?

147,888

Solution 1

Although there isn't a syntax, you can still get close by using the built-in block comment buttons:

If you're not viewing the Edit toolbar already, right-click on the toolbar and enable the Edit toolbar:

enter image description here

Then, select a block of code and hit the "Comment Block" button; or if it's already commented out, use the "Uncomment Block" button:

enter image description here

Fast and easy!

Solution 2

prefix the comment with a single-quote. there is no need for an "end" tag.

'this is a comment

Extend to multiple lines using the line-continuation character, _:

'this is a multi-line _
   comment

This is an option in the toolbar to select a line(s) of code and comment/uncomment:

enter image description here

Solution 3

There is no syntax for block quote in VBA. The work around is to use the button to quickly block or unblock multiple lines of code.

Share:
147,888
Tim.DeVries
Author by

Tim.DeVries

I am a college student studying computer science with concentrations in computer programming and internet security.

Updated on June 21, 2020

Comments

  • Tim.DeVries
    Tim.DeVries almost 4 years

    In VBA is there a short way to comment out a block of code the same way java uses /*...*/?

    • Mark C.
      Mark C. almost 10 years
      Yes and it's called "Comment Block" AND, it may not be present on your toolbar. If not, right click a toolbar in the VBE and choose "Customize". On the Commands tab, choose "Edit" and scroll down the Commands list until you find it. Drag to a toolbar. Do the same for the "Uncomment Block" icon from here
    • RubberDuck
      RubberDuck almost 10 years
  • Tim.DeVries
    Tim.DeVries almost 10 years
    This works but I am looking for something i can put at the end and at the start of an already existing block of code rather than something on every line.
  • RubberDuck
    RubberDuck almost 10 years
    Doesn't exist the way you want it @Tim.DeVries.
  • David Zemens
    David Zemens almost 10 years
    @Tim.DeVries there is no such functionality in VBA. Your best alternative is to simply use the cursor to select multiple lines of code and press the button for "Comment Block" from the menu bar. There is no syntax that does this.
  • David Zemens
    David Zemens almost 10 years
    Curious why you'd un-accept my answer (two weeks later!) in favor of a nearly-identical answer that was posted ~2 hours after mine... I have no need for the karma here, I'm just trying to find out why this answer is not good for you now, when it was originally...
  • Andre
    Andre about 8 years
    This answer doesn't add anything to the accepted answer.
  • Some_Guy
    Some_Guy over 7 years
    If you really want to comment out a code block without changing each line, you can just put a goto before it and a label after it. A bit ugly but its done all the time in batch and VBScript. stackoverflow.com/a/19747128/4842964 stackoverflow.com/questions/8526946/…
  • David Zemens
    David Zemens over 7 years
    @Some_Guy that may or may not work depending on circumstances (and many people also don't like peppering their code with GoTo statements, as a matter of style). At the very least, that method still requires the full "commented" block must compile, otherwise it's useless. try it: i.imgur.com/Z2WksP0.png
  • Some_Guy
    Some_Guy over 7 years
    That's just because you're missing beans.dll
  • Some_Guy
    Some_Guy over 7 years
    @DavidZemens That's just because you're missing beans.dll
  • Some_Guy
    Some_Guy over 7 years
    But good point, as I said, it's ugly. People hate goto but GoTo Commented_Out doesn't make for unclear control flow, and allows you to quickly comment out a block of (compilable) code. But yeah, it's a bit ugly, and entirely down to preference. Commenting out code is generally bad style anyway, but if you want to as a quick and dirty solution then a goto works just fine, and doesn't require you to mess around with the crap IDE or adding stuff to each line.
  • David Zemens
    David Zemens over 7 years
    @Some_Guy I actually like the idea and it's never occurred to me to do that before :) just pointing out some limitations . And you're right, I am missing the beans.dll...
  • Forcuti Alessandro
    Forcuti Alessandro over 3 years
    I don't understand why multiline comments are implemented in this way. If I must repeat "_" character ad the end of new line then I use the ' character