{"id":2,"date":"2014-01-16T23:56:25","date_gmt":"2014-01-17T06:56:25","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=2"},"modified":"2016-08-22T13:01:32","modified_gmt":"2016-08-22T20:01:32","slug":"vba","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/","title":{"rendered":"Excel VBA"},"content":{"rendered":"<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-thumbnail wp-image-973\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/logo_excel-150x150.png\" alt=\"\" width=\"150\" height=\"150\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/logo_excel-150x150.png 150w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/logo_excel.png 256w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/>What is VBA?<\/strong><\/p>\n<p>Visual Basic for Applications, VBA, is a programming language included with MS Office. VBA is a fairly simple programming language, but it has most of the components of other larger languages: classes, objects and events, and a visual environment for development (VBE). For this reason VBA is a good\u00a0programming language to learn (or at least very good one to start learning).<\/p>\n<p><strong>Why VBA?<\/strong><\/p>\n<p>MS Excel is the most used spreadsheet application in businesses and as an OR practitioner you will use it frequently. Coding in VBA allows you to automate, simplify and customize tasks. Therefore, knowing how to use VBA for Excel modeling and applications development is valuable\u00a0skill in any company.<\/p>\n<p><strong>What is a Macro?<\/strong><\/p>\n<p>MS Excel consists \u000bof a number of <span style=\"color: #0000ff;\">objects,<\/span> or named parts of a computer\u2019s memory. Objects have <span style=\"color: #0000ff;\">properties<\/span>, and <span style=\"color: #0000ff;\">methods<\/span> that manipulate these properties. A <span style=\"color: #0000ff;\">macro<\/span> is a collection of instructions for the computer to handle these Excel objects. It is a small computer program that runs within MS Office.<\/p>\n<h2>Getting Started<\/h2>\n<p>Before starting you need to set up the developer tab and the security settings:<\/p>\n<p><strong>Developer Tab<\/strong>: Here you can access the Visual Basic Editor and other developer tools.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-318\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/developer-tab.jpg\" alt=\"Excel 2010 developer tab\" width=\"742\" height=\"167\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/developer-tab.jpg 742w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/developer-tab-300x67.jpg 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/developer-tab-624x140.jpg 624w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>Office 2010 does not display the Developer tab by default: you must enable it yourself.<\/p>\n<ul>\n<li><a href=\"http:\/\/office.microsoft.com\/en-ca\/excel-help\/show-the-developer-tab-HA101819080.aspx\" target=\"_blank\">Show developer tab in Excel 2010, 2013<\/a><\/li>\n<li><a href=\"http:\/\/office.microsoft.com\/en-ca\/excel-help\/show-the-developer-tab-or-run-in-developer-mode-HA010173052.aspx\" target=\"_blank\">Show developer tab in Excel 2007<\/a><\/li>\n<\/ul>\n<p><strong>Security issues<\/strong>: you cannot save a macro in the default Excel file format (.xlsx); instead, you must save the macro in a file with a special extension .xlsm. If the Security Warning bar appears when you open a workbook that contains a macro, you can click the &#8216;Enable Content&#8217; button to enable the macros. Click the Macro Security button on the developer tab to specify which macros can run and under what conditions.<\/p>\n<h2>Visual Basic Editor (VBE)<\/h2>\n<p>VBE is the environment in which you \u000bwork with VBA code. To access it you can either:<\/p>\n<ul>\n<li>Go to the menu Tools &gt; Macro &gt; Visual Basic Editor,<\/li>\n<li>Click the VBE icon from VBE Toolbar, or<\/li>\n<li>Press ALT + F11<\/li>\n<\/ul>\n<p>VBA has three main windows: project explorer, properties, and code. There are two other windows used for debugging: immediate and watch.<\/p>\n<p><strong>Project Explorer<\/strong><\/p>\n<p>Lists all projects in any open workbook. Each workbook has a project, and \u000beach project can have several parts: workbook and worksheets, modules and forms.<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-explorer.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-224 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-explorer.jpg\" alt=\"\" width=\"378\" height=\"307\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-explorer.jpg 378w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-explorer-300x243.jpg 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/a><\/p>\n<p>To add a new module or form to the current project use Insert &gt; Module or Insert &gt; User Form; or you can also use the icon from the standard toolbar.<\/p>\n<p><strong>Properties Window<\/strong><\/p>\n<p>Contains detailed information about any selected part of a project in the Project Explorer. Some basic naming and formatting properties can be modified for worksheets and workbooks. Properties are very important for user forms, for formatting, setting position, adding pictures, setting special behavior, and so forth.<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-properties.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-225 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-properties.jpg\" alt=\"\" width=\"300\" height=\"598\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-properties.jpg 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-properties-150x300.jpg 150w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Code Window<\/strong><\/p>\n<p>Displays the VBA code associated with the Excel object selected in the Project Explorer. It is the tool that we use to create macros and handle the corresponding object. You can also store the code in a separate module which you can use to refer to another object.<\/p>\n<p><a href=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-code.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-226\" src=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-code.jpg\" alt=\"\" width=\"781\" height=\"229\" srcset=\"https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-code.jpg 781w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-code-300x87.jpg 300w, https:\/\/blogs.ubc.ca\/coetoolbox\/files\/2014\/01\/vbe-code-624x182.jpg 624w\" sizes=\"auto, (max-width: 781px) 100vw, 781px\" \/><\/a><\/p>\n<p>At the top of the code editor there are two drop-down lists. The first one is the Class Name list, where you can select any of the objects located within the selected object, such as spreadsheet, buttons, etc. The second list is the Method Name list, which provides access to any event associated with the currently selected object. Use these lists to locate any code procedure.<\/p>\n<p>A nice feature of the code window is <span style=\"color: #0000ff;\">IntelliSense<\/span>, which provides dynamic assistance as you enter the code, displaying all the possible coding options available. IntelliSense displays automatically and you can press Ctrl + Space to manually load it.<\/p>\n<h2>Recording Macros<\/h2>\n<p>Macros are a way to automate a series of actions in a spreadsheet application. They can either be created directly with VBA code in the \u000bVisual Basic Editor, or recorded in Excel. To record a macro in Excel do the following steps:<\/p>\n<p><strong>1. Prepare<\/strong>: to record a macro, we must know exactly the actions we wish to perform and then use the Macro Recorder.<\/p>\n<p><strong>2. Record<\/strong>: click the record button from the developer tab. When the Record Macro dialog box appears, we enter a name for the macro.<\/p>\n<p><strong>3. Stop<\/strong>: once you begin recording, notice that the Record button transforms to a Stop button. After finishing the steps needed to copy and paste the information, you can stop recording.<\/p>\n<p><strong>4. Play<\/strong>: Once you have recorded a macro, you can play it to make sure it works correctly. Click the Play button from VBE Toolbar<\/p>\n<p><strong>5. Review the code<\/strong>: Each time a macro is recorded in Excel, VBA code is automatically generated in a new module. Go the VBE Project Explorer and select the module to see the code in the Code Window.<\/p>\n<p>Since we can study the VBA code generated when we record a macro, we can easily learn how to create a similar macro directly from VBA by copying the code generated.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is VBA? Visual Basic for Applications, VBA, is a programming language included with MS Office. VBA is a fairly simple programming language, but it has most of the components of other larger languages: classes, objects and events, and a visual environment for development (VBE). For this reason VBA is a good\u00a0programming language to learn [&hellip;]<\/p>\n","protected":false},"author":22979,"featured_media":0,"parent":0,"menu_order":1,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/2","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22979"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=2"}],"version-history":[{"count":22,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/2\/revisions"}],"predecessor-version":[{"id":1454,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/2\/revisions\/1454"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=2"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}