{"id":17245,"date":"2021-11-14T11:36:56","date_gmt":"2021-11-14T10:36:56","guid":{"rendered":"https:\/\/ekiwi-blog.de\/?p=17245"},"modified":"2021-11-14T11:36:56","modified_gmt":"2021-11-14T10:36:56","slug":"vba-comment-multiple-lines","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/17245\/vba-comment-multiple-lines\/","title":{"rendered":"VBA comment multiple lines"},"content":{"rendered":"<p>Anyone who programs a lot in the Microsoft Office area with <abbr title=\"Visual Basic for Application\">VBA<\/abbr> and uses the associated VBA editor also wants to comment out entire blocks of code quickly.<\/p>\n<p>In some of our other posts we have described how to automatically process emails with macros when they arrive in the inbox. Or how to send e-mail attachments automatically to the printer. Such and other functions have to be tested a lot during development, and sometimes you want to comment whole methods and functions and then comment them back in.<\/p>\n<p>Unfortunately, VBA does not offer the possibility to comment the code as in other programming languages:<\/p>\n<pre><code><span style=\"color: #339966;\">\/*code line X<\/span>\r\n<span style=\"color: #339966;\">code line Y<\/span>\r\n<span style=\"color: #339966;\">code line Z*\/<\/span><\/code><\/pre>\n<p>Instead, in VBA you have to put the &#8220;apostrophe&#8221; in front of each line of code.<\/p>\n<pre><code><span style=\"color: #339966;\">'code line X<\/span>\r\n<span style=\"color: #339966;\">'code line Y<\/span>\r\n<span style=\"color: #339966;\">'code line Z<\/span><\/code><\/pre>\n<p>If you want to comment whole methods and functions, it can be very annoying to set or remove the &#8220;apostrophe&#8221; manually before each line.<\/p>\n<h2>Comment code block in VBA<\/h2>\n<p>But fortunately, the VBA editor offers a corresponding function via a toolbar called &#8220;Edit&#8221;. However, this toolbar is not shown by default. To show it, go to <em><strong>View =&gt; Toolbars =&gt; Edit<\/strong><\/em> in the menu.<\/p>\n<figure id=\"attachment_17238\" aria-describedby=\"caption-attachment-17238\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-17238 size-full\" title=\"toolbar Edit contains buttons for comment and uncomment multiple lines of code\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit.jpg\" alt=\"Screenshot VBA Editor Menu View Toolbars Edit\" width=\"600\" height=\"477\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit.jpg 600w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit-300x239.jpg 300w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit-100x80.jpg 100w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbars-edit-80x64.jpg 80w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-17238\" class=\"wp-caption-text\">Show Toolbar Edit<\/figcaption><\/figure>\n<p>If the Edit toolbar appears floating in the program, you can also pin it to the existing toolbar.<\/p>\n<figure id=\"attachment_17240\" aria-describedby=\"caption-attachment-17240\" style=\"width: 701px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbar-edit.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-17240 size-full\" title=\"Edit toolbar with buttons for comment and uncomment multiple lines (code block)\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbar-edit.jpg\" alt=\"Screenshot Toolbar Edit with tools to comment and uncomment multiple lines (code block)\" width=\"701\" height=\"73\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbar-edit.jpg 701w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-editor-toolbar-edit-300x31.jpg 300w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/a><figcaption id=\"caption-attachment-17240\" class=\"wp-caption-text\">Edit toolbar with buttons for comment and uncomment multiple lines (code block)<\/figcaption><\/figure>\n<p>There are now two icons here. One for commenting and one for uncommenting.<\/p>\n<p>To do this, mark the relevant lines of code and click on the respective symbol, as shown in the following two images. The &#8220;apostrophe&#8221; is then automatically prepended or removed for all marked lines.<\/p>\n<figure id=\"attachment_17242\" aria-describedby=\"caption-attachment-17242\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-comment-code-block.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-17242 size-full\" title=\"Icon to comment code block\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-comment-code-block.jpg\" alt=\"Screenshot VBA Editor comment multiple lines\" width=\"600\" height=\"232\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-comment-code-block.jpg 600w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-comment-code-block-300x116.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-17242\" class=\"wp-caption-text\">Mark code lines and click button for &#8220;Comment Block&#8221;<\/figcaption><\/figure>\n<figure id=\"attachment_17244\" aria-describedby=\"caption-attachment-17244\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-uncomment-code-block.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-17244 size-full\" title=\"Icon to uncomment code block\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-uncomment-code-block.jpg\" alt=\"Screenshot VBA-Editor Uncomment multiple lines of code\" width=\"601\" height=\"246\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-uncomment-code-block.jpg 601w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2021\/11\/vba-uncomment-code-block-300x123.jpg 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/a><figcaption id=\"caption-attachment-17244\" class=\"wp-caption-text\">Mark lines of code and click button for &#8220;Uncomment Block&#8221;<\/figcaption><\/figure>\n<p>So it is still possible to comment several lines in the VBA code in a halfway practicable way.<\/p>","protected":false},"excerpt":{"rendered":"<p>Anyone who programs a lot in the Microsoft Office area with VBA and uses the associated VBA editor also wants<\/p>\n","protected":false},"author":2,"featured_media":13630,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[1701,1721,1702,1857,1704,2168,1700,1861,1705,1706,1710],"class_list":["post-17245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-access-en","tag-comment","tag-excel-en","tag-microsoft-en-2","tag-microsoft-office-en","tag-office-en","tag-programming","tag-vba-en-2","tag-vba-en","tag-visual-basic-for-application-en","tag-word-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/17245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=17245"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/17245\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/13630"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=17245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=17245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=17245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}