{"id":52,"date":"2019-04-04T20:38:05","date_gmt":"2019-04-04T20:38:05","guid":{"rendered":"https:\/\/keithratner.live\/optionexplicit\/?p=52"},"modified":"2021-08-03T12:42:59","modified_gmt":"2021-08-03T12:42:59","slug":"notes-on-style-vba-coding-style","status":"publish","type":"post","link":"https:\/\/keithratner.live\/optionexplicit\/notes-on-style-vba-coding-style\/","title":{"rendered":"Notes on VBA Coding Style: Maximizing Scalability and Readability"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Establishing and adhering to a VBA coding style guide enables increased project reusability and scalability. It makes code more readable and, by extension, the coding experience far more enjoyable.<\/p>\n\n\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"55\" data-permalink=\"https:\/\/keithratner.live\/optionexplicit\/notes-on-style-vba-coding-style\/notes-on-style\/\" data-orig-file=\"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?fit=724%2C962&amp;ssl=1\" data-orig-size=\"724,962\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Notes on Style\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?fit=724%2C962&amp;ssl=1\" class=\"alignnone wp-image-55 size-full\" src=\"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?resize=724%2C962&#038;ssl=1\" alt=\"Notes on VBA Coding Style\" width=\"724\" height=\"962\" srcset=\"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?w=724&amp;ssl=1 724w, https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?resize=226%2C300&amp;ssl=1 226w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/p>\n<h3>Minimize Horizontal Scrolling<\/h3>\n<p>Split lines (use the underscore!) and indent. My rationale is that horizontal scrolling takes too long. You want to be able to scan through your code quickly. You also want to be able to view code modules side-by-side, which ties into another upcoming post (TODO&#8230;) regarding code modularity &#8211; specifically, if your procedure is going to be lengthy, consider placing it in its own module, and ALWAYS use a naming convention. This applies to variables and modules. Pick a convention and stick to it. The VBE (Visual Basic Editor) has split screen, but that only works with a horizontal separator. It is helpful to be able to view code side-by-side, particularly if you&#8217;re writing class definitions that may have similar implementations. If your lines are short, you can view more modules side-by-side.<\/p>\n<p>The screenshot illustrates the following conventions that I like to use:<\/p>\n<h4>Split line after open parentheses and each parameter<\/h4>\n<p>This includes the first line of any procedure definition (Sub, Property, Function).<br \/>Each parameter should be on its own line. Be mindful of proper syntax with regards to commas. This will drive you crazy if you&#8217;re not careful. The debugger is your friend. So, for instance:<\/p>\n<pre>Function MyFunction( _\n  strParamOne as String, _\n  strParamTwo as String, _\n  intParamCounter as Integer, _\n  strLastParam as String _\n)<\/pre>\n<p>Note the <em><strong>comma-space-underscore<\/strong><\/em> syntax for all parameters except the last one. The debugger will yell at you about this, but do yourself a favor and form the habit.<\/p>\n<h4>Split line after Dim statement when declaring multiple variables<\/h4>\n<p>This ties into my convention for splitting lines after multiple parameters. It may not be necessary for single parameters, but consider that <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/visual-basic\/programming-guide\/language-features\/declared-elements\/declared-element-names\" target=\"_blank\" rel=\"noopener noreferrer\">VBA permits you to use up to 1,203 characters to name your variables<\/a>. That enables you to be descriptive with your variable names, which you should be, because someone will probably have to decipher that code of yours eventually &#8211; and it might just be you. NEVER use non-descriptive variable names such as a, b, c or x, y, z. Just don&#8217;t do it. So, conserve your horizontal space on each line.\u00a0 For instance:<\/p>\n<pre>Dim strMyVariable as String<\/pre>\n<p>Keeping a single variable declaration on one line may be acceptable. It&#8217;s up to you. Just make a decision and stick to it. If you have a long variable name, you might want to conserve some space like this:<\/p>\n<pre>Dim _\n  strWorksheetNameConcatenatedFromSourceWorkbook as String<\/pre>\n<p>When declaring multiple variables, I like to give them each their own indented line:<\/p>\n<pre>Dim _\n  strMyFirstVariable as String, _\n  strMySecondVariable as String, _\n  intMyThirdVariable as Integer, _\n  strMyLastVariable as String<\/pre>\n<h4>Split lengthy strings into separate lines<\/h4>\n<p>Your mileage may vary on this, but I think 80 characters is a good limit to set. Split your strings and concatenate them with an ampersand, then use <em><strong>ampersand-space-underscore<\/strong><\/em> to split your lines:<\/p>\n<pre>strMyLongString = _\n  \"This will be a \" &amp; _\n  \"run-on sentence with \" &amp; _\n  \"a few variables thrown in \" &amp; _\n  \"here: \" &amp; _\n  strMyFirstConcatenatedStringElement &amp; _\n  \" and here: \" &amp; _\n  strMySecondConcatenatedStringElement &amp; _\n  \" to properly illustrate my \" &amp; _\n  \"point.\"<\/pre>\n<p>Note that there is a limit on VBA line continuations (see <a title=\"Microsoft Office Dev Center Article\" href=\"https:\/\/docs.microsoft.com\/en-us\/office\/vba\/language\/reference\/user-interface-help\/too-many-line-continuations\" target=\"_blank\" rel=\"noopener noreferrer\">Microsoft Office Dev Center Article<\/a>). Per the specs: Your code should have no more than 25 physical lines joined with line-continuation characters or more than 24 consecutive line-continuation characters in a single line. Make some of the constituent lines physically longer to reduce the number of line-continuation characters needed, or break the construct into more than one statement.<\/p>\n<h4>Be Verbose<\/h4>\n<p>Function and Object names get compiled down so their length does not affect performance. VBA is meant to enable verbosity. Use camel case and name your functions and objects explicitly. Establish a naming convention early on if possible. For example, the following variable has a three letter prefix indicating its data type (string, in this case), then a concise camel case description of the variable&#8217;s purpose.\u00a0<\/p>\n<pre>strWorksheetName<\/pre>\n<p>Whether you use this VBA coding style guide or build your own, the important thing is to adhere to it throughout your project, especially if you are working with a team. It helps to speak the same language, both programmatically and stylistically. The less guesswork is involved, the more efficiently you can get things done.<\/p>\n<pre><br \/><br \/><\/pre>","protected":false},"excerpt":{"rendered":"<p>Establishing and adhering to a VBA coding style guide enables increased project reusability and scalability. It makes code more readable and, by extension, the coding experience far more enjoyable. Minimize Horizontal Scrolling Split lines (use the underscore!) and indent. My rationale is that horizontal scrolling takes too long. You want to be able to scan [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":55,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[111],"tags":[136],"class_list":["post-52","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-article","tag-style"],"krwpengineoptions-featuredonwpengineblog":"","jetpack_featured_media_url":"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/notes-on-style.png?fit=724%2C962&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgsIIA-Q","jetpack-related-posts":[{"id":319,"url":"https:\/\/keithratner.live\/optionexplicit\/why-object-oriented-vba\/","url_meta":{"origin":52,"position":0},"title":"Why Object-Oriented VBA?","author":"Keith","date":"March 23, 2023","format":false,"excerpt":"Object-oriented programming is an essential concept in modern software engineering that encapsulates data and functionality within a single entity called an object. Microsoft's Visual Basic for Applications (VBA) is a popular programming language used in creating software for Microsoft Office applications like Excel and PowerPoint. Although VBA has its roots\u2026","rel":"","context":"In &quot;Article&quot;","block_context":{"text":"Article","link":"https:\/\/keithratner.live\/optionexplicit\/category\/article\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":563,"url":"https:\/\/keithratner.live\/optionexplicit\/update-access-vba-saved-imports-exports\/","url_meta":{"origin":52,"position":1},"title":"Update Access VBA Saved Imports Exports: A Step-by-Step Guide","author":"Keith","date":"May 3, 2024","format":false,"excerpt":"Updating Access VBA saved imports exports is essential when dealing with external data sources. This step-by-step guide will show you how to update Access VBA saved imports exports by dynamically changing the file path within your import\/export specifications. In this article, we'll walk through a powerful VBA script that allows\u2026","rel":"","context":"In &quot;Article&quot;","block_context":{"text":"Article","link":"https:\/\/keithratner.live\/optionexplicit\/category\/article\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2024\/05\/Screenshot-2024-05-03-133800.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":160,"url":"https:\/\/keithratner.live\/optionexplicit\/edge-git-vscode\/","url_meta":{"origin":52,"position":2},"title":"Edge, Git and VS Code Integration","author":"Keith","date":"May 23, 2019","format":false,"excerpt":"In my VBA project, on common UserForms, I have three labels with click event handlers attached: First is a pause button, which just triggers a Stop command, launching the debugger\/VBA IDE. Second is a save button, which will save ThisWorkbook. Third triggers the ExportModules procedure. Source file updates are reflected\u2026","rel":"","context":"In &quot;Article&quot;","block_context":{"text":"Article","link":"https:\/\/keithratner.live\/optionexplicit\/category\/article\/"},"img":{"alt_text":"VS Code Git Updates","src":"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/05\/vscode_git_updates.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":316,"url":"https:\/\/keithratner.live\/optionexplicit\/the-importance-of-visual-basic-for-applications-vba\/","url_meta":{"origin":52,"position":3},"title":"The Importance of Visual Basic for Applications (VBA)","author":"Keith","date":"March 23, 2023","format":false,"excerpt":"Visual Basic for Applications (VBA) is a programming language that is used to automate tasks in Microsoft Office applications, including Excel, Word, and PowerPoint. VBA allows users to create customized macros, automate repetitive tasks, and interface with external software, making it an essential part of many businesses' software workflows. In\u2026","rel":"","context":"In &quot;Article&quot;","block_context":{"text":"Article","link":"https:\/\/keithratner.live\/optionexplicit\/category\/article\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":35,"url":"https:\/\/keithratner.live\/optionexplicit\/export-modules-to-folder\/","url_meta":{"origin":52,"position":4},"title":"Export Modules to Folder","author":"Keith","date":"April 3, 2019","format":false,"excerpt":"This will create a folder using the base name of your Excel file, which is the filename without its extension, along with a vba subfolder. Your VBA modules will be placed there. Pass UseTimestamp:=True to put each export in its own timestamped subfolder so repeated runs don't overwrite earlier exports.\u2026","rel":"","context":"In &quot;Snippet&quot;","block_context":{"text":"Snippet","link":"https:\/\/keithratner.live\/optionexplicit\/category\/snippet\/"},"img":{"alt_text":"Export Modules","src":"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/4-4-2019-2-16-36-AM.png?fit=665%2C496&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/4-4-2019-2-16-36-AM.png?fit=665%2C496&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/keithratner.live\/optionexplicit\/wp-content\/uploads\/sites\/29\/2019\/04\/4-4-2019-2-16-36-AM.png?fit=665%2C496&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":322,"url":"https:\/\/keithratner.live\/optionexplicit\/why-use-vba-in-excel\/","url_meta":{"origin":52,"position":5},"title":"Why Use VBA in Excel?","author":"Keith","date":"March 23, 2023","format":false,"excerpt":"Excel VBA, or Visual Basic for Applications, is a programming language that allows developers to automate tasks and build applications within the Excel environment. Utilizing Excel VBA can dramatically improve your productivity and increase your Excel skillset, as well as provide you with the opportunity to create customized macros for\u2026","rel":"","context":"In &quot;Article&quot;","block_context":{"text":"Article","link":"https:\/\/keithratner.live\/optionexplicit\/category\/article\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":10,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":231,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/posts\/52\/revisions\/231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/media\/55"}],"wp:attachment":[{"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/keithratner.live\/optionexplicit\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}