Комментарии в тексте макроса начинаются с апострофа на каждой строчке программы, а существует возможность закомментировать сразу целый блок? Например, как в других языках — /*
— начало блока, */
-конец блока?
P.S. Я нашла ответ, что
в редакторе VBA на панели Edit есть кнопки Comment Block, Uncomment
Block
но я у себя не нахожу подобного:
задан 28 окт 2016 в 11:08
5
View
-> Toolbars
-> Customise
Commands tab
-> Edit menu
Две иконки- Comment Block
и Uncomment Block
.
Перетащите из в тулбар Edit menu
.
Profit.
ответ дан 28 окт 2016 в 11:14
Среда разработки макросов в Excel Microsoft Visual Basic for Applications, мягко говоря, не является самой удобной средой разработки. Сравнивать его с Visual Studio просто нельзя – это как сравнить текстовые редакторы Блокнот и Word. Но пользоваться Microsoft Visual Basic for Applications иногда нужно, потому приходится адаптироваться.
Столкнулся с необходимостью закомментировать порядка 20 строчек кода. Ставить знак ‘ в начале каждой строки очень не хочется, к тому же потом еще и раскомментировать нужно.
По умолчанию, панель с кнопкой комментирования кода отключена. Для начала нужно включить панель редактирования, для этого в меню View / Toolbars нужно отметить пункт Edit (Рис.1).
Рис.1. Добавление тулбара в среду разработки Microsoft Visual Basic for Applications
Для того, чтобы закомментировать несколько строк кода, нужно в появившемся тулбаре нажать на кнопку «Comment Block» (Рис.2)
Рис.2. Закомментировать несколько строк кода
Для раскомментирования строчек нужно нажать на соседнюю кнопку «Uncomment block».
VBA Macro is for developers. Macro is a piece of code written in VBA. VBA is Microsoft’s programming language and it stands for Visual Basic for Applications. Let’s see how to set up our VBA Macro and how to add comments in a VBA in Excel.
VBA Macro
Excel is a very advanced tool that contains thousands of functionalities, but VBA Macro comes into existence when we have to do a repeated task. The complex repeated tasks can be automated with the help of VBA Macro.
Initial Set-Up
Go to Developer Tab. We will see that by default developer tab is not present in the menu bar.
Follow the steps:
Step 1: Right-click on any tab in the menu bar. Click on Customize the Ribbon. A dialogue box appears.
Step 2: In the Mains Tab, check the box Developer and click on Ok.
Step 3: Now the Developer Tab is visible.
Step 4: Go to Developer Tab and click on Visual Basic.
Step 5: Now, the VBA tab is opened. Click on Tools in the menu bar and then click on Macros. A dialogue box is open.
Step 6: Write the macro name and click on create.
A Macro is created.
VBA Comments
Comments are the lines in the code that are ignored while executing the code. These are represented as green text in the code. The comments help describe the written code. Knowing the correct use of comments is very important because while working with long and complex code, comments help us identify which part of code does what. It is very helpful for development purposes.
Adding Single Line comment in VBA
Step 1: Click on the line where you want to insert a comment.
Step 2: Type an Apostrophe( ‘ ) at the start of a line.
Step 3: Write the comment you want.
Step 4: Press Enter and you fill find the comment written to be green.
Adding Multi-Line comment in VBA
We can add comments in multiple lines. We use multi-line comments when we have to add points in our description or the description is long.
Step 1: Keep your cursor on the Tool Bar.
Step 2: Right-click on the Tool Bar and click on edit. An extended Tool Bar appears. Drag and place it in the already existing Tool Bar.
Step 3: Select the text you want to comment on and click on Comment Block.
The entire selected text got commented.
Using Buttons to add a comment
Step 1: Go to Toolbar and right-click on it. A menu appears.
Step 2: Click on Customize and a dialogue box appears.
Step 3: Go to edit in the left-side scrollable list.
Step 4: Find Comment Block and Uncomment Block in the right-side scrollable list.
Step 5: Click on Comment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 6: Click on Uncomment Block and drag it to the menu bar. It will look like a button in the menu bar.
Step 7: With the dialogue box opened. Go to the comment block and right-click on it. A menu appears.
Step 8: Click inside the Name and add a character & at the starting of Comment Block. Then click somewhere outside the appeared menu.
Step 9: Again, right-click on the Comment Block and select Image and Text.
Step 10: Repeat steps 7, 8, 9 for Uncomment Block i.e. right-click on the Uncomment Block and add & in the Name. Also, select the Image and Text in the appeared menu. At last, close the dialogue box.
Step 11: A shortcut for comment and uncomment has been created in the VBA code editor. To comment on a line the shortcut is Alt + C and to uncomment a line the shortcut is Alt + U. You can also use the Comment Block and Uncomment Block buttons to comment on a line. Enter the text you want to comment on.
Step 12: To comment on the written line. You can click Alt + C.
Step 13: To uncomment a line, you can press Alt + U.
Use Rem to Comment
At the start of the comment use the keyword Rem to comment on a line.
Formatting Comments
Step 1: Go to Tools Tab, and right-click on it.
Step 2: A menu appears, and click on Options… A dialogue box appears.
Step 3: Go to Editor Format.
Step 4: Select the Comment Text from the left scrollable list.
Step 5: You can change the color of the comment by selecting Foreground. For example, red. Click Ok.
Step 6: Now, all comments will have a font color of red.
Return to VBA Code Examples
This article will teach you how to comment a single line or multiple blocks of code in the VBA Editor. Instead, if you want to learn about how to interact with Excel Cell Comments using VBA read that article.
In Excel VBA, there are several ways to comment lines of a code:
- Single quotation (‘)
- Comment block button in the toolbar
- Adding the Rem keyword.
The easiest way to comment a line of a code is putting a single quotation at the beginning of the line:
'Sheet1.Range("A1").Value = "Test"
Notice that in VBA, comments are always displayed as green text.
As you can see in the example, we put a single quotation at the beginning of the first line in the procedure and commented it. If a quotation is put at the beginning of the line, the whole line is commented and will be skipped during execution of the code.
You can also comment part of the code if you put a single quotation somewhere in the line.
In that case code after a quotation will be skipped:
Sheet1.Range("A1").Value = "Test" 'The example of partial line commenting
Now we commented only part of the line. This is a good way for writing inline comments in a code.
The second way for commenting a line in a code is using the standard VBA button for comment in the toolbar. In order to display this button, you need to add it: View -> Toolbars -> Edit. Now you can see two buttons in the toolbar: Comment block and Uncomment block.
Simply highlight your desired line(s) of code and click one of the buttons. This will comment/uncomment entire lines. Please note that this method will not allow you to add a comment to the end of a line of code.
You can also use the keyword Rem. In order to comment a line, you need to put this keyword at the beginning of a line:
Rem Sheet1.Range("A1").Value = "Test"
Similarly to comment button, the Rem keyword allows you to comment just a whole line of a code, which means that you can put it only at the beginning of a line:
Apart from commenting a single line, we often need to comment multiple lines, a block of code. In order to do this, we can the same standard button Comment Block in the toolbar which we used for commenting a single line. First, we need to select all the lines that we want to comment and then click on the button:
Private Sub CommentEntireBlock()
' Sheet1.Range("A1").Value = "Test"
' If Sheet1.Range("A1") = "Test" Then
' MsgBox "The value of A1 cell is: Test"
' End If
End Sub
As a result, the whole block of code is commented.
Similarly, we can uncomment a block, by clicking on the Uncomment Block button in the toolbar:
Private Sub CommentEntireBlock()
Sheet1.Range("A1").Value = "Test"
If Sheet1.Range("A1") = "Test" Then
MsgBox "The value of A1 cell is: Test"
End If
End Sub
To enable keyboard shortcuts for commenting:
- Right-click somewhere on empty space in the toolbar.
- Choose Customize option and select the Edit under the categories.
- Find Comment Block in the Commands and drag and drop it next to the existing icons in the toolbar.
- Now you can see the newly added button in the toolbar
- Click on the Modify Selection and check option Image and Text.
- Click again on the Modify Selection and under Name add an ampersand (&) at the beginning of the name, so the name of the button is “&Comment Block”.
Now you can select a single line or a block of code and press Alt+C on your keyboard to comment.
To enable the same option for uncommenting a code, you can repeat the whole process for Uncomment Block command. The shortcut for uncommenting is ALT+U.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
0 / 0 / 0 Регистрация: 15.02.2009 Сообщений: 12 |
|
1 |
|
22.02.2009, 10:01. Показов 111438. Ответов 11
как VBA закомментировать сразу несколько строк, чтоб каждую не начинать ковычками?
0 |
Vasya Pupkin |
|
22.02.2009, 10:15 |
2 |
В редакторе VBA — Это ли тебе нужно? |
0 / 2 / 3 Регистрация: 27.03.2012 |
|
22.02.2009, 10:16 |
3 |
никак, это особенность языка
0 |
0 / 0 / 0 Регистрация: 15.02.2009 Сообщений: 12 |
|
22.02.2009, 14:45 [ТС] |
4 |
сама уже не знаю. что мне нужно… но за совет спавибо
0 |
master-neo |
|
26.07.2011, 17:28 |
5 |
Да, действительно чтобы сделать многострочный комментарий нужно поступить как сказал Вася Пупкин. Разве нельзя за столько лет дополнить поддержку многострочных комментариев. Для кого язык сделан? Кто на нем будет программировать через несколько лет? |
Заблокирован |
|
14.01.2019, 20:56 |
6 |
прошло уж 8 лет, а косяки остались)
0 |
es geht mir gut 11264 / 4746 / 1183 Регистрация: 27.07.2011 Сообщений: 11,437 |
|
14.01.2019, 21:48 |
7 |
прошло уж 8 лет, а косяки остались) Какие косяки ? Excel 2007 Миниатюры
5 |
es geht mir gut 11264 / 4746 / 1183 Регистрация: 27.07.2011 Сообщений: 11,437 |
|
14.01.2019, 21:49 |
8 |
Это быстрее и удобнее, чем теги ставить.
0 |
4038 / 1423 / 394 Регистрация: 07.08.2013 Сообщений: 3,541 |
|
14.01.2019, 21:58 |
9 |
всегда делал сначала вот так
2 |
es geht mir gut 11264 / 4746 / 1183 Регистрация: 27.07.2011 Сообщений: 11,437 |
|
14.01.2019, 22:00 |
10 |
Не по теме: snipe, что там в архиве ? Скачивать неохота
0 |
4038 / 1423 / 394 Регистрация: 07.08.2013 Сообщений: 3,541 |
|
14.01.2019, 22:01 |
11 |
маленький видос как вытащить эти кнопки что на рисунке
1 |
0 / 0 / 0 Регистрация: 13.11.2020 Сообщений: 1 |
|
15.11.2021, 17:05 |
12 |
snipe, Спасибо
0 |