30

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

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Tim.DeVries
  • 711
  • 2
  • 6
  • 21
  • 3
    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](http://www.mrexcel.com/forum/excel-questions/412566-simple-commenting-multiple-lines-visual-basic-applications-editor.html) – Mark C. Jun 02 '14 at 19:08
  • http://stackoverflow.com/a/12933280/3198973 – RubberDuck Jun 02 '14 at 19:11

3 Answers3

45

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!

LimaNightHawk
  • 5,623
  • 1
  • 36
  • 57
6

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

David Zemens
  • 51,213
  • 11
  • 70
  • 118
  • 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. – Tim.DeVries Jun 02 '14 at 19:11
  • 1
    Doesn't exist the way you want it @Tim.DeVries. – RubberDuck Jun 02 '14 at 19:11
  • 2
    @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 Jun 02 '14 at 19:13
  • 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... – David Zemens Jun 11 '14 at 16:48
  • 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. http://stackoverflow.com/a/19747128/4842964 http://stackoverflow.com/questions/8526946/commenting-multiple-lines-in-dos-batch-file – Some_Guy Jan 19 '17 at 10:36
  • @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: http://i.imgur.com/Z2WksP0.png – David Zemens Jan 19 '17 at 14:45
  • That's just because you're missing beans.dll – Some_Guy Jan 19 '17 at 15:24
  • 1
    @DavidZemens That's just because you're missing beans.dll – Some_Guy Jan 19 '17 at 15:31
  • 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. – Some_Guy Jan 19 '17 at 15:31
  • @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... – David Zemens Jan 19 '17 at 15:49
  • 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 – neo7bf Nov 07 '20 at 12:04
4

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.

syanzy
  • 99
  • 2