For Excel senior user, they always create some VBA codes to solve the complex problems. But, sometimes, they want to protect the VBA code from being viewed or destroyed by others. Just like you can use password to protect workbooks and worksheets, you can also set a password for protecting the macros in Excel. This series of blogs explains why, how and when to comment in VBA. Why Write Comments in Excel Visual Basic; Adding Comments in VBA macros for Excel; Commenting Out Code in Excel Visual Basic (this blog) This blog is part of our Excel macros online tutorial series. Alternatively, come on one of our VBA classroom training courses. VBA Editor, Comment and Uncomment Blocks of Code; If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com.
- Comment A Block In Vba In Excel Using
- Comment A Block In Vba In Excel Cell
- Comment A Block In Vba In Excel Function
- Comment Block In Vba Excel
You can add VBA comments with either a single apostrophe (‘) at the beginning of the line or by clicking the comment block icon in the menu bar.
VBA comments will show as green text in the VBA editor.
Sometimes you want to write a funny message to your future self. Other times you’re more practical and add commentary to help yourself or another reader understand what is going on. This is where adding comments to your code comes in.
You can comment out code or messages in VBA on a line by line basis. Building upon the example from the last section, let’s add another MsgBox and run it commented and non-commented.
Although we have a second MsgBox in our code, the VBA engine completely ignores it. If you were to remove the apostrophe before the second MsgBox and run the program, you would get two message boxes to pop up.
The green text within your VBA Development environment means that the code is commented out.
Comment A Block In Vba In Excel Using
Comment A Block In Vba In Excel Cell
Single line comments serve their purpose. However, if you have many lines of code that you need to comment out, inserting a single apostrophe for every line can be quite tedious. In this case we can use the comment block action.
A block of code can be defined as multiple lines of code. You have the option to comment out code line by line. Alternatively, you can comment out multiple lines all with the click of a button. By default, my version of the Excel VBA Development Environment did not have this option on the main taskbar. Adding it is straight forward. Using the image below as a reference, click on View -> Toolbars -> Edit.
Comment A Block In Vba In Excel Function
Click and drag the new bar into your main menu bar. Afterwards, your menu bar should look like the following. Note that I have highlighted the Comment Block and Uncomment Block icons in the image below.
Comment Block In Vba Excel
. To use Comment/Uncomment block, select a body of code within your VBA editor and click either Comment or Uncomment. Any lines of code with at least one character highlighted will be commented out. Uncomment works the same way. First, highlight the block of code then click uncomment block.