{"id":68,"date":"2011-10-10T16:32:52","date_gmt":"2011-10-10T16:32:52","guid":{"rendered":"http:\/\/www.edcortes.ph\/?p=159"},"modified":"2021-07-11T08:00:12","modified_gmt":"2021-07-11T08:00:12","slug":"getting-started-with-vba-in-excel-2010-part-2","status":"publish","type":"post","link":"https:\/\/emcortes.com\/?p=68","title":{"rendered":"Getting Started with VBA in Excel 2010 (Part 2)"},"content":{"rendered":"<p>Now that you know something about how Microsoft Excel 2010 exposes its object model, you can try calling object methods and setting object properties. To do so, you must write your code in a place and in a way that Office can understand; typically, by using the Visual Basic Editor. Although it is installed by default, many users do not know that it is even available until it is enabled on the ribbon.<\/p>\n<h4>Developer Tab<\/h4>\n<p>All Office 2010 applications use the ribbon. One tab on the ribbon is the <strong>Developer<\/strong> tab, where you access the Visual Basic Editor and other developer tools. Because Office 2010 does not display the <strong>Developer<\/strong> tab by default, you must enable it using the following procedure:<\/p>\n<h3>To enable the Developer tab<\/h3>\n<div>\n<ol>\n<li>On the <strong>File<\/strong> tab, choose <strong>Options<\/strong> to open the <strong>Excel Options<\/strong> dialog box.<\/li>\n<li>Click <strong>Customize Ribbon<\/strong> on the left side of the dialog box.<\/li>\n<li>Under <strong>Choose commands from<\/strong> on the left side of the dialog box, select <strong>Popular Commands<\/strong>.<\/li>\n<li>Under <strong>Customize the ribbon<\/strong> on the right side of the dialog box, select <strong>Main tabs<\/strong>, and then select the <strong>Developer<\/strong> check box.<\/li>\n<li>Click <strong>OK<\/strong>.<\/li>\n<\/ol>\n<\/div>\n<p>After Excel displays the <strong>Developer<\/strong> tab, note the location of the <strong>Visual Basic<\/strong>, <strong>Macros<\/strong>, and <strong>Macro Security<\/strong> buttons on the tab.<\/p>\n<div><a href=\"http:\/\/www.edcortes.ph\/wp-content\/uploads\/2011\/10\/Developer-Tab-in-Excel-2010.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-160\" title=\"Developer Tab in Excel 2010\" src=\"http:\/\/www.edcortes.ph\/wp-content\/uploads\/2011\/10\/Developer-Tab-in-Excel-2010.jpg\" alt=\"\" width=\"742\" height=\"167\" \/><\/a><\/div>\n<div>\u00a0<\/div>\n<div>\n<h4>Security Issues<\/h4>\n<div>\n<p>Click the <strong>Macro Security<\/strong> button to specify which macros can run and under what conditions. Although rogue macro code can seriously damage your computer, security conditions that prevent you from running helpful macros can seriously undermine your productivity. Macro security is a complex and involved topic that you should study and understand if you work with Excel macros.<\/p>\n<p>For the purposes of this article, be aware that if the <strong>Security Warning: Macros have been disabled<\/strong> bar appears between the ribbon and the worksheet when you open a workbook that contains a macro, you can click the <strong>Enable Content<\/strong> button to enable the macros.<\/p>\n<p>Also, as a security measure, 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.<\/p>\n<\/div>\n<h4>Visual Basic Editor<\/h4>\n<p>This following procedure shows you how to create a new blank workbook in which to store your macros. You can then save the workbook in the .xlsm format.<\/p>\n<h3>To create a new blank workbook<\/h3>\n<div>\n<ol>\n<li>Click the <strong>Macros<\/strong> button on the Developer tab.<\/li>\n<li>In the <strong>Macro<\/strong> dialog box that appears, type, <strong>Hello<\/strong> under <strong>Macro Name<\/strong>.<\/li>\n<li>Click the <strong>Create<\/strong> button to open the Visual Basic Editor with the outlines of a new macro already typed in.<\/li>\n<\/ol>\n<\/div>\n<p>VBA is a full-featured programming language with a correspondingly full-featured programming environment. This article examines only those tools that you use to get started with programming, and that excludes most of the tools in the Visual Basic Editor. With this caveat, close the <strong>Properties<\/strong> window on the left side of the Visual Basic Editor, and ignore the two dropdown lists that appear above the code.<\/p>\n<div><a href=\"http:\/\/www.edcortes.ph\/wp-content\/uploads\/2011\/10\/VBA-Editor.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-161\" title=\"VBA Editor\" src=\"http:\/\/www.edcortes.ph\/wp-content\/uploads\/2011\/10\/VBA-Editor.jpg\" alt=\"\" width=\"811\" height=\"177\" \/><\/a><\/div>\n<div>\u00a0<\/div>\n<p>The Visual Basic Editor contains the following code.<\/p>\n<div><script type=\"text\/javascript\" src=\"\/Areas\/Global\/Content\/CodeSnippet.js\"><\/script><\/p>\n<div>\n<div dir=\"ltr\">\u00a0<\/div>\n<\/div>\n<div>\n<div id=\"CodeSnippetContainerCode_73edccaa-b61d-46c8-994f-853d6cb608b6\" dir=\"ltr\">\n<div>\n<pre>Sub Hello()\r\n\r\nEnd Sub<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><em>Sub<\/em> stands for <em>Subroutine<\/em>, which you can define for now as &#8220;macro&#8221;. Running the <code>Hello<\/code> macro runs any code that is between <code>Sub Hello()<\/code> and <code>End Sub<\/code>.<\/p>\n<p>Now edit the macro so that it looks similar to the following code.<\/p>\n<div><script type=\"text\/javascript\" src=\"\/Areas\/Global\/Content\/CodeSnippet.js\"><\/script><\/p>\n<div>\n<div dir=\"ltr\">\u00a0<\/div>\n<\/div>\n<div>\n<div id=\"CodeSnippetContainerCode_fe508906-b971-43fd-9124-fb5c7e76a9e4\" dir=\"ltr\">\n<div>\n<pre>Sub Hello()\r\n   MsgBox (\"Hello, world!\")\r\nEnd Sub<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Go back to the <strong>Developer<\/strong> tab in Excel and click the <strong>Macros<\/strong> button again.<\/p>\n<p>Select the <strong>Hello<\/strong> macro in the list that appears and then click <strong>Run<\/strong> to display a small message box that contains the text, &#8220;Hello, world!&#8221;<\/p>\n<p>You just created and implemented custom VBA code in Excel. Click <strong>OK <\/strong>in the message box to close it and finish running the macro.<\/p>\n<p>If the message box does not appear, check your macro security settings and restart Excel.<\/p>\n<\/div>\n<div>Source: <a href=\"http:\/\/msdn.microsoft.com\/\">http:\/\/msdn.microsoft.com<\/a>\u00a0<\/p>\n<div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Now that you know something about how Microsoft Excel 2010 exposes its object model, you can try calling object methods and setting object properties. To do so, you must write your code in a place and in a way that Office can understand; typically, by using the Visual Basic Editor. Although it is installed by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,4],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-excel","category-programming-notes"],"_links":{"self":[{"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/emcortes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":1,"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/emcortes.com\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/emcortes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emcortes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emcortes.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}