{"id":44,"date":"2025-04-21T03:26:05","date_gmt":"2025-04-21T03:26:05","guid":{"rendered":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/?post_type=chapter&#038;p=44"},"modified":"2025-06-12T12:23:22","modified_gmt":"2025-06-12T12:23:22","slug":"introduction-to-vba","status":"publish","type":"chapter","link":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/chapter\/introduction-to-vba\/","title":{"raw":"Introduction to VBA","rendered":"Introduction to VBA"},"content":{"raw":"<div class=\"textbox\">\r\n<p class=\"import-epf\">Once you become acquainted with VBA, you\u2019ll wonder how you ever got along without it.<\/p>\r\n<p class=\"import-ept\" style=\"text-align: right;\">Reed Jacobson<\/p>\r\n\r\n<\/div>\r\n<h1 class=\"import-ahaft\">9.1 My First Macro in Excel<\/h1>\r\n<p class=\"import-pf\">You have learned many new Excel functions, created a variety of different charts in Excel, and used Solver, Comparative Statics Wizard, Monte Carlo simulation, and FRED Excel add-ins. But those are all front-end Excel skills.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">For computer scientists, the front end is the presentation layer or the user interface of software. The front end is that part of the restaurant, where the diner sits down and orders food.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The back end of a restaurant is the kitchen, where the meal is actually made. Excel\u2019s back end is built on a computer language called Visual Basic. Excel and other Office products such as Word and PowerPoint run a version called Visual Basic Applications edition\u2014or VBA for short.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">It is in VBA that we write code, also known as macros, to perform especially complicated tasks. The code is written and stored in <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">modules<\/em><\/span>. The resulting Excel workbook is a combination of spreadsheets and modules that must be saved as a macro-enabled workbook with the extension <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.<\/em><em class=\"import-i\">xlsm<\/em><\/span>. Without macros, an Excel spreadsheet has the file extension <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.xlsx<\/em><\/span>.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP <\/em><\/strong><\/span>Excel warns you if you save a workbook with macros as a simple Excel workbook (.xlsx). If you ignore the warning, it will simply save the workbook without the macros, and your code will be lost.<\/p>\r\n\r\n<\/div>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is no question that a macro-enabled workbook is more powerful than a simple spreadsheet, but this power comes at a cost. Not only do you have to know how to write code in VBA, but your end user will probably have to enable macros when opening the file. Sometimes, security settings in a particular installation of Excel are set so high that the macros will not be allowed to run. The user has to change Excel\u2019s settings, adding another layer of difficulty, just to open the file.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP\u00a0<\/em><\/strong><\/span>If you can accomplish a task without macros, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">always<\/em><\/span> do so. Sometimes, however, VBA is the only solution.<\/p>\r\n\r\n<\/div>\r\n<h2 class=\"import-bh\">Hello, World!<\/h2>\r\n<p class=\"import-paft\">It is a tradition in computer science to introduce a new language by outputting \u201cHello, World!\u201d Let\u2019s do it.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Open Excel and save the file as <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span>, making sure to save it as a macro-enabled workbook with the .xlsm extension by clicking <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Save as type<\/em><\/span> in the save window and choosing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.<\/em><em class=\"import-i\">xlsm<\/em><\/span>. Click the <span class=\"import-ccust1\">Visual Basic<\/span> button in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Ribbon<\/em><\/span>. If the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab is not visible, press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">f<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">t<\/em><\/span> and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Customize Ribbon<\/em><\/span>, then check <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> in the list. You can also access Visual Basic by pressing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> (you may need to use the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> [<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">fn<\/em><\/span>] key) or right-clicking the sheet tab and selecting <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">View<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Code<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You have a new window on your screen with a lot of things you have never seen before. Welcome to Excel\u2019s kitchen! You are in the Visual Basic Editor (VBE).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Across the top there is a familiar menu of items. The top-left panel should be the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> (press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Ctrl<\/em><em class=\"import-i\">-r<\/em><\/span> if you do not see it). You may also see other panels.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>In the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel, scroll, if needed, to find your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span> workbook (it will be in parentheses after VBAProject), and select it (highlighted in blue). Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Insert<\/em><\/span> in the top menu and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Module<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You now see a blank window. This is where you will write your code. Notice also that your workbook in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel now has a new component, the module you just inserted.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>The cursor should be blinking in the blank window, but if not, click in the window. Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">sub<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myfirstmacro<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The text is transformed. The <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">S<\/em><\/span> is capitalized and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> is in blue, parentheses have been appended, and a new line <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> has been added. Apparently, VBE is a high-level editor with a great deal of support.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> stands for subroutine, a set of instructions or lines of code. The statements between the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> lines are the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">body<\/em><\/span> of the macro. You could pass <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">arguments<\/em><\/span> to your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> by entering them in the () on the first line.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>In the middle line (where the cursor is blinking), enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">msgbox<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cHello<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">World!\u201d<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">As you entered the text, you undoubtedly noticed the yellow pop-up showing the various options for the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">MsgBox<\/em><\/span> object. This shows again the strong support offered in the editor in the VBA environment. Also, this example reveals that VBA is an object-oriented programming language. We write code to apply different methods and options to the objects.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We have finished our first macro and are ready to run it.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Run<\/em><\/span> in the top menu and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Run<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub\/User<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Firm<\/em><\/span> (or press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F5<\/em><\/span>).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You did it! You are returned to the Excel spreadsheet, and it displays a message box with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cHello<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">World!\u201d<\/em><\/span> on it.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This is exciting, but how can we run the macro from the spreadsheet?<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span> to close the message box and click the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab. Click the <span class=\"import-ccust1\">Insert<\/span> button and select the top-left <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Button<\/em><\/span> icon. Click and drag in the spreadsheet to create a button. In the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">AssignMacro<\/em><\/span> dialog box, select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myfirstmacro<\/em><\/span> and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span>. Click on an empty cell in the spreadsheet.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You have added a button to the spreadsheet and attached a macro to it. When you click the button, the macro will run. Try it.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the button to see the message box pop up.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Now the user does not have to know how to run macros in VBA. By attaching the macro to the button, you have made it easy for the user to run your code.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP\u00a0<\/em><\/strong><\/span>Regular (left) clicks enable you to use objects in Excel. Right-clicks select objects so that you can modify them.<\/p>\r\n\r\n<\/div>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Right-click your button and replace the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Button 1<\/em><\/span> text with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Click Me<\/em><\/span>. Click the button to see that it works.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This shows that the caption of the button can be different from the name of the macro.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We used the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">S<\/em><em class=\"import-i\">croll <\/em><em class=\"import-i\">B<\/em><em class=\"import-i\">ar<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">C<\/em><em class=\"import-i\">ombo <\/em><em class=\"import-i\">B<\/em><em class=\"import-i\">ox<\/em><\/span> controls earlier, but they were not macro-enabled. By assigning macros to controls, we greatly expand the power of Excel.<\/p>\r\n\r\n<h2 class=\"import-bh\">Recording Macros<\/h2>\r\n<p class=\"import-paft\">VBA is hard at first because beginning users do not know any of the objects or commands. It is like learning a new spoken language: You know the words exist for what you want to say, but you do not know what they are.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">One way to start growing your VBA vocabulary is by recording macros. You turn on the recorder and do things in Excel, then examine the recorded code in VBA.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab and click the <span class=\"import-ccust1\">Record Macro<\/span> button. Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span> in the pop-up dialog box. Select cell A1 and enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">100<\/em><\/span>. Make the formatting $. Click the <span class=\"import-ccust1\">Stop Recording<\/span> button. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> to go to VBA.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is a new module sheet in your VBA project.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Double-click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Module2<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You are looking at the code needed to do the steps you did in Excel. You now know that <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Selection.Style<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cCurrency\u201d<\/em><\/span> applies currency formatting to the selected cell.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">100<\/em><\/span> in the recorded macro to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0.1<\/em><\/span> and change <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Currency<\/em><\/span> to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Percent<\/em><\/span>. Return to Excel by pressing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> (this toggles you between Excel and VBA). Right-click your button and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">AssignMacro<\/em><\/span>. Choose <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Macro1<\/em><\/span> (that you just recorded and edited) and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span>. Click an empty cell in the spreadsheet and click your button.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Your macro changed A1 to 0.1 and formatted it as percent! This demonstrates that you can definitely control Excel from VBA.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This example shows how you can record a macro to reveal the code needed to perform a task in Excel. The usual procedure is to record a series of steps and then edit the code, removing superfluous lines and changing values or other attributes.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Use the macro recorder to write a macro that takes a random number from a cell and pastes its value into the cell below it. Assign your macro to your button and click the button to run it. If you need help, see the appendix.<\/p>\r\n\r\n<div class=\"textbox textbox--key-takeaways\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Takeaways<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"import-paft\">In the late 20th century, Excel was in a race with other spreadsheets that you have never heard of because Excel completely overwhelmed them. In 1993, Excel 5.0 debuted with VBA, and Microsoft crushed the competition.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">VBA is an implementation of Visual Basic that runs within Excel. Macros are written in VBA in modules and then assigned to controls (such as buttons) on the spreadsheet.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">If you want to continue learning about VBA, Reed Jacobson\u2019s <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Microsoft Office Excel <\/em><em class=\"import-i\">2007 Visual Basic for Applications Step by Step<\/em><\/span> is a great place to start. The files needed were originally on a CD (an older technology that could store \u201chuge\u201d amounts of data), but they are available for download at <a href=\"http:\/\/dub.sh\/hbvba\">dub.sh\/hbvba<\/a>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You can also look at open-source VBA code. For example, the Comparative Statics Wizard and Monte Carlo simulation add-ins are freely accessible. So are the macro-enabled workbooks we have used. You can open their modules and inspect the code to learn VBA.<\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<div class=\"textbox textbox--examples\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">References<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"hanging-indent\">The epigraph is from the first chapter of Reed Jacobson\u2019s <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Microsoft Office Excel 2007 Visual Basic for Applications Step by Step<\/em><\/span> (Pearson Education), Kindle Edition. This book is out of print, but there are many copies available, and it remains a great way to learn how to write macros in Excel.<\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<div class=\"textbox textbox--exercises\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Appendix<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"import-paft\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the <span class=\"import-ccust1\">Record Macro<\/span> button and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span> in a cell. Copy the cell, select another cell, and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Paste Special<\/em><\/span> as <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Values<\/em><\/span>. Stop recording and go to VBA. The body of your recorded macro (the code between the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> lines) should look something like this:<\/p>\r\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">'<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">' Macro3 Macro<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">'<\/p>\r\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">'<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Select<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">ActiveCell.FormulaR1C1 = \u201c=RAND()\u201d<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Select<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Selection.Copy<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA4\u201d).Select<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">SkipBlanks :=False, Transpose:=False<\/p>\r\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA5\u201d).Select\\\\<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">These lines are the actual commands you gave Excel. Notice that an apostrophe at the start of a line comments out that line (it is not executed) and is displayed in green-colored text (e.g., the line Macro3 Macro). The underscore character, _, continues the current statement on the next line.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You could clean up this code so it looks like this:<\/p>\r\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">Range(\u201cA3\u201d).FormulaR1C1 = \u201c=RAND()\u201d<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Copy<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA4\u201d).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">SkipBlanks:=False, Transpose:=False<\/p>\r\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA5\u201d).Select<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You could comment out the first line, and it would still work since the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">RAND()<\/em><\/span> function is already in cell A3. Running this macro replaces the contents of cell A3 without any warning. You do not notice this because the code writes the same formula that was there.<\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<h1 class=\"import-ah\">9.2 Functions in VBA<\/h1>\r\n<p class=\"import-paft\">Attaching a macro to a button or other object allows you and your users to run VBA code, but there is another way to access code from an Excel workbook: a <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">user-defined<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> (UDF).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Unlike a native function, like =RAND() or =SUM(cell range), which is part of the Excel application itself, UDFs are functions that you write in VBA and are stored in a module in a macro-enabled workbook (file name extension .xlsm).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">To the user, UDFs work the same way as native functions, so they require no special knowledge. The only extra step is that the user has to enable the content in the workbook (or disable all security, which is not recommended).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">To demonstrate how UDFs work, we will create two user-defined functions, the first without any arguments and the second allowing the user to pass information to the function, making it more flexible and useful.<\/p>\r\n\r\n<h2 class=\"import-bh\">UDF with No Arguments<\/h2>\r\n<p class=\"import-paft\">Recall from our work on Monte Carlo simulation that we can create a 90% free throw shooter in Excel with the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=IF(RAND()&lt;0.9,1,0)<\/em><\/span>. Alternatively, we could create a function in VBA\u2014say, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span>\u2014that the user could enter in any cell. Ninety percent of the time it would produce a 1, and the remaining 10% would be 0.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Open your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span> macro-enabled workbook and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> to go to Visual Basic. Insert a new module sheet in this workbook. Type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span> (uppercase FT) and hit enter.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Excel\u2019s Visual Basic Editor capitalizes the F in <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span>, adds an <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> for you, and colors the text blue for the beginning and ending lines of the code.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Instead of the Excel function <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span>, VBA has its own random number generator <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span>. It produces random numbers in the interval from 0 to 1. It is supereasy to make our UDF output a random number.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>. Return to Excel (<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> is a toggle) and click on cell H1. Enter the function <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FT()<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Congratulations! You just wrote your first function in Excel and accessed it from Excel.<\/p>\r\n\r\n<div class=\"textbox\">\r\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP <\/em><\/strong><\/span>For UDFs, Excel remembers how you first entered the function. If you enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=ft()<\/em><\/span> (lowercase), it will keep using lowercase (even if you enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> in a different cell). This is not true for native functions: Enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=rand()<\/em><\/span> and Excel converts it to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span>.<\/p>\r\n\r\n<\/div>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is, however, a problem with our UDF.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times. Nothing happens to cell H1. It is not bouncing like <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">RAND()<\/em><\/span>, generating a new random number each time we recalculate the sheet.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The problem is that UDFs, by default, are nonvolatile functions. This means they do not get recalculated unless they depend on other cells that have changed. We must add code to make our function recalculate when <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> is pressed.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Return to your VBA code for the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> function. Click on the top line, after the close parenthesis, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span> so that you are on a new, blank line and enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Application.Volatile<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">True<\/em><\/span>. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>. Return to Excel and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times. Success!<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">But we do not want to simply output a random number; we want to see if we made (1) or missed (0) a free throw attempt. We need to add some code to do this. Like the Excel formula we used to model a free throw result, we need an <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement to separate made from missed free throws.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Go to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> code. Click after <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">True<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span> to create a new line after the volatile statement. Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">if <\/em><em class=\"import-i\">rnd<\/em><em class=\"import-i\"> &lt; 0.9 then<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">As usual, the editor capitalizes and colors the text for you. The next line of code defines what happens if the random number is less than 0.9. It is followed by lines of code for missed free throws.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Tab<\/em><\/span> key to indent, type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span>, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Type the word <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Else<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Tab<\/em><\/span>, type <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Type <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">end if<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Put a straight single quote (') in front of the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT=<\/em><em class=\"import-i\">Rnd<\/em><\/span> line to comment it out (so it does not get executed).<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Your masterpiece of code should look like this (with color added to keywords and lines):<\/p>\r\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">Function FT()<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Application.Volatile True<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">If Rnd &lt; 0.9 Then<\/p>\r\n<p class=\"import-cl1\" style=\"margin-left: 54pt; margin-right: 36pt;\">FT = 1<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Else<\/p>\r\n<p class=\"import-cl1\" style=\"margin-left: 54pt; margin-right: 36pt;\">FT = 0<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">End If<\/p>\r\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">'FT = Rnd<\/p>\r\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">End Function<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The macro draws a uniformly distributed random number on the interval from 0 to 1 (<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span>), and if it is less than 0.9, it goes to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> line. Since the function is called <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span>, it outputs the number 1 if <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span> &lt; 0.9. If the random number drawn is not less than 0.9, it goes to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> line and outputs a 0.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The code is easy to read, but does it actually work? Let\u2019s find out.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Return to Excel, fill cell H1 down to cell H10, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">That is pretty cool, but what if we wanted a more generalized version, where the user tells us the chances of success?<\/p>\r\n\r\n<h2 class=\"import-bh\">UDF with an Argument<\/h2>\r\n<p class=\"import-paft\">In native Excel functions\u2014like <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">SUM<\/em><\/span>, for example\u2014the arguments are passed in the parentheses: <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">SUM(A1:A3)<\/em><\/span>. UDFs work the same way. We will add an argument to our code in the parentheses and modify the code to enable it to incorporate the information provided by the user.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Copy the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> code and paste it below the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End Function<\/em><\/span> line. Since you cannot have two functions with the same name, change the name of the newly pasted function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> (for argument). In the parentheses, type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot as Double<\/em><\/span>. Replace the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0.9<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement line with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot<\/em><\/span>. Return to Excel and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FTARG(0.5)<\/em><\/span> in cell I1.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Excel displays an error message in cell I1. Can you figure out what is wrong with the UDF and fix it? If you cannot, take a look at the appendix.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We can make the function even more flexible by having it accept a value in another cell.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Copy the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> code and paste it below the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> line. Change the name of the newly pasted function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> (for argument from another cell). In the parentheses, change the text to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myShootCell<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">as<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Range<\/em><\/span>. Replace <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement line with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myShootCell.Value<\/em><\/span>. Change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> lines to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>. To see how this works, return to Excel and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FTARGCELL($K$1)<\/em><\/span> in cell J1. Fill it down to cell J10. In cell K1, enter a number from 0 to 1, such as 0.25. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">With a 25% chance of success, your 10 numbers in column J are bouncing around every time you press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span>, and you usually get 2 or 3 ones.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Is it better to allow the user to input a number as the argument or a cell address? That depends on the context of the problem, including the user\u2019s familiarity with Excel. In fact, if you do not need to change the success rate, our original UDF, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span>, might be the best choice.<\/p>\r\n\r\n<div class=\"textbox textbox--key-takeaways\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Takeaways<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"import-paft\">VBA code can be accessed by users from Excel\u2019s front end by attaching macros to buttons and other controls.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Another option is a UDF. The user enters a formula that runs the UDF code to do computations or other manipulations that are not available in native Excel functions.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Like native Excel functions, UDFs usually require arguments. These variables are declared in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> statement, inside the parentheses.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Writing code is science and art. Deciding whether arguments are needed and, if so, how to pass them (numbers or cell addresses, for example) requires knowledge of what needs to be done and who is going to do it. You need to know your audience.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Most people think of Excel as some kind of sophisticated adding machine or calculator. Excel can certainly do arithmetic and other mathematical operations, but its back end or kitchen opens up a whole new world of opportunities and possibilities.<\/p>\r\n\r\n<\/div>\r\n<\/div>\r\n<div class=\"textbox textbox--exercises\"><header class=\"textbox__header\">\r\n<p class=\"textbox__title\">Appendix<\/p>\r\n\r\n<\/header>\r\n<div class=\"textbox__content\">\r\n<p class=\"import-paft\">The <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> function fails when you change the name of the function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> but do not likewise update the name of the function in the code. You must change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> line to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> line to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>.<\/p>\r\n<p class=\"import-p\" style=\"text-indent: 36pt;\">When you write a UDF, you always use the function name to output a result. If you need more than one cell to output results, you can use an array function.<\/p>\r\n\r\n<\/div>\r\n<\/div>","rendered":"<div class=\"textbox\">\n<p class=\"import-epf\">Once you become acquainted with VBA, you\u2019ll wonder how you ever got along without it.<\/p>\n<p class=\"import-ept\" style=\"text-align: right;\">Reed Jacobson<\/p>\n<\/div>\n<h1 class=\"import-ahaft\">9.1 My First Macro in Excel<\/h1>\n<p class=\"import-pf\">You have learned many new Excel functions, created a variety of different charts in Excel, and used Solver, Comparative Statics Wizard, Monte Carlo simulation, and FRED Excel add-ins. But those are all front-end Excel skills.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">For computer scientists, the front end is the presentation layer or the user interface of software. The front end is that part of the restaurant, where the diner sits down and orders food.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The back end of a restaurant is the kitchen, where the meal is actually made. Excel\u2019s back end is built on a computer language called Visual Basic. Excel and other Office products such as Word and PowerPoint run a version called Visual Basic Applications edition\u2014or VBA for short.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">It is in VBA that we write code, also known as macros, to perform especially complicated tasks. The code is written and stored in <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">modules<\/em><\/span>. The resulting Excel workbook is a combination of spreadsheets and modules that must be saved as a macro-enabled workbook with the extension <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.<\/em><em class=\"import-i\">xlsm<\/em><\/span>. Without macros, an Excel spreadsheet has the file extension <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.xlsx<\/em><\/span>.<\/p>\n<div class=\"textbox\">\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP <\/em><\/strong><\/span>Excel warns you if you save a workbook with macros as a simple Excel workbook (.xlsx). If you ignore the warning, it will simply save the workbook without the macros, and your code will be lost.<\/p>\n<\/div>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is no question that a macro-enabled workbook is more powerful than a simple spreadsheet, but this power comes at a cost. Not only do you have to know how to write code in VBA, but your end user will probably have to enable macros when opening the file. Sometimes, security settings in a particular installation of Excel are set so high that the macros will not be allowed to run. The user has to change Excel\u2019s settings, adding another layer of difficulty, just to open the file.<\/p>\n<div class=\"textbox\">\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP\u00a0<\/em><\/strong><\/span>If you can accomplish a task without macros, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">always<\/em><\/span> do so. Sometimes, however, VBA is the only solution.<\/p>\n<\/div>\n<h2 class=\"import-bh\">Hello, World!<\/h2>\n<p class=\"import-paft\">It is a tradition in computer science to introduce a new language by outputting \u201cHello, World!\u201d Let\u2019s do it.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Open Excel and save the file as <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span>, making sure to save it as a macro-enabled workbook with the .xlsm extension by clicking <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Save as type<\/em><\/span> in the save window and choosing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">.<\/em><em class=\"import-i\">xlsm<\/em><\/span>. Click the <span class=\"import-ccust1\">Visual Basic<\/span> button in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Ribbon<\/em><\/span>. If the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab is not visible, press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">f<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">t<\/em><\/span> and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Customize Ribbon<\/em><\/span>, then check <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> in the list. You can also access Visual Basic by pressing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> (you may need to use the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> [<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">fn<\/em><\/span>] key) or right-clicking the sheet tab and selecting <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">View<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Code<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You have a new window on your screen with a lot of things you have never seen before. Welcome to Excel\u2019s kitchen! You are in the Visual Basic Editor (VBE).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Across the top there is a familiar menu of items. The top-left panel should be the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> (press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Ctrl<\/em><em class=\"import-i\">-r<\/em><\/span> if you do not see it). You may also see other panels.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>In the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel, scroll, if needed, to find your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span> workbook (it will be in parentheses after VBAProject), and select it (highlighted in blue). Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Insert<\/em><\/span> in the top menu and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Module<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You now see a blank window. This is where you will write your code. Notice also that your workbook in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel now has a new component, the module you just inserted.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>The cursor should be blinking in the blank window, but if not, click in the window. Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">sub<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myfirstmacro<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The text is transformed. The <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">S<\/em><\/span> is capitalized and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> is in blue, parentheses have been appended, and a new line <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> has been added. Apparently, VBE is a high-level editor with a great deal of support.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> stands for subroutine, a set of instructions or lines of code. The statements between the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> lines are the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">body<\/em><\/span> of the macro. You could pass <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">arguments<\/em><\/span> to your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> by entering them in the () on the first line.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>In the middle line (where the cursor is blinking), enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">msgbox<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cHello<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">World!\u201d<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">As you entered the text, you undoubtedly noticed the yellow pop-up showing the various options for the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">MsgBox<\/em><\/span> object. This shows again the strong support offered in the editor in the VBA environment. Also, this example reveals that VBA is an object-oriented programming language. We write code to apply different methods and options to the objects.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We have finished our first macro and are ready to run it.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Run<\/em><\/span> in the top menu and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Run<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub\/User<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Firm<\/em><\/span> (or press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F5<\/em><\/span>).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You did it! You are returned to the Excel spreadsheet, and it displays a message box with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cHello<\/em><\/span>, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">World!\u201d<\/em><\/span> on it.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This is exciting, but how can we run the macro from the spreadsheet?<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span> to close the message box and click the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab. Click the <span class=\"import-ccust1\">Insert<\/span> button and select the top-left <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Button<\/em><\/span> icon. Click and drag in the spreadsheet to create a button. In the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">AssignMacro<\/em><\/span> dialog box, select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myfirstmacro<\/em><\/span> and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span>. Click on an empty cell in the spreadsheet.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You have added a button to the spreadsheet and attached a macro to it. When you click the button, the macro will run. Try it.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the button to see the message box pop up.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Now the user does not have to know how to run macros in VBA. By attaching the macro to the button, you have made it easy for the user to run your code.<\/p>\n<div class=\"textbox\">\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP\u00a0<\/em><\/strong><\/span>Regular (left) clicks enable you to use objects in Excel. Right-clicks select objects so that you can modify them.<\/p>\n<\/div>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Right-click your button and replace the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Button 1<\/em><\/span> text with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Click Me<\/em><\/span>. Click the button to see that it works.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This shows that the caption of the button can be different from the name of the macro.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We used the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">S<\/em><em class=\"import-i\">croll <\/em><em class=\"import-i\">B<\/em><em class=\"import-i\">ar<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">C<\/em><em class=\"import-i\">ombo <\/em><em class=\"import-i\">B<\/em><em class=\"import-i\">ox<\/em><\/span> controls earlier, but they were not macro-enabled. By assigning macros to controls, we greatly expand the power of Excel.<\/p>\n<h2 class=\"import-bh\">Recording Macros<\/h2>\n<p class=\"import-paft\">VBA is hard at first because beginning users do not know any of the objects or commands. It is like learning a new spoken language: You know the words exist for what you want to say, but you do not know what they are.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">One way to start growing your VBA vocabulary is by recording macros. You turn on the recorder and do things in Excel, then examine the recorded code in VBA.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Developer<\/em><\/span> tab and click the <span class=\"import-ccust1\">Record Macro<\/span> button. Click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span> in the pop-up dialog box. Select cell A1 and enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">100<\/em><\/span>. Make the formatting $. Click the <span class=\"import-ccust1\">Stop Recording<\/span> button. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> to go to VBA.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is a new module sheet in your VBA project.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Double-click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Module2<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Project<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Explorer<\/em><\/span> panel.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You are looking at the code needed to do the steps you did in Excel. You now know that <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Selection.Style<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">\u201cCurrency\u201d<\/em><\/span> applies currency formatting to the selected cell.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">100<\/em><\/span> in the recorded macro to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0.1<\/em><\/span> and change <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Currency<\/em><\/span> to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Percent<\/em><\/span>. Return to Excel by pressing <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> (this toggles you between Excel and VBA). Right-click your button and select <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">AssignMacro<\/em><\/span>. Choose <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Macro1<\/em><\/span> (that you just recorded and edited) and click <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">OK<\/em><\/span>. Click an empty cell in the spreadsheet and click your button.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Your macro changed A1 to 0.1 and formatted it as percent! This demonstrates that you can definitely control Excel from VBA.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">This example shows how you can record a macro to reveal the code needed to perform a task in Excel. The usual procedure is to record a series of steps and then edit the code, removing superfluous lines and changing values or other attributes.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Use the macro recorder to write a macro that takes a random number from a cell and pastes its value into the cell below it. Assign your macro to your button and click the button to run it. If you need help, see the appendix.<\/p>\n<div class=\"textbox textbox--key-takeaways\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Takeaways<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"import-paft\">In the late 20th century, Excel was in a race with other spreadsheets that you have never heard of because Excel completely overwhelmed them. In 1993, Excel 5.0 debuted with VBA, and Microsoft crushed the competition.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">VBA is an implementation of Visual Basic that runs within Excel. Macros are written in VBA in modules and then assigned to controls (such as buttons) on the spreadsheet.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">If you want to continue learning about VBA, Reed Jacobson\u2019s <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Microsoft Office Excel <\/em><em class=\"import-i\">2007 Visual Basic for Applications Step by Step<\/em><\/span> is a great place to start. The files needed were originally on a CD (an older technology that could store \u201chuge\u201d amounts of data), but they are available for download at <a href=\"http:\/\/dub.sh\/hbvba\">dub.sh\/hbvba<\/a>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You can also look at open-source VBA code. For example, the Comparative Statics Wizard and Monte Carlo simulation add-ins are freely accessible. So are the macro-enabled workbooks we have used. You can open their modules and inspect the code to learn VBA.<\/p>\n<\/div>\n<\/div>\n<div class=\"textbox textbox--examples\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">References<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"hanging-indent\">The epigraph is from the first chapter of Reed Jacobson\u2019s <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Microsoft Office Excel 2007 Visual Basic for Applications Step by Step<\/em><\/span> (Pearson Education), Kindle Edition. This book is out of print, but there are many copies available, and it remains a great way to learn how to write macros in Excel.<\/p>\n<\/div>\n<\/div>\n<div class=\"textbox textbox--exercises\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Appendix<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"import-paft\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Click the <span class=\"import-ccust1\">Record Macro<\/span> button and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span> in a cell. Copy the cell, select another cell, and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Paste Special<\/em><\/span> as <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Values<\/em><\/span>. Stop recording and go to VBA. The body of your recorded macro (the code between the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Sub<\/em><\/span> lines) should look something like this:<\/p>\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">&#8216;<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">&#8216; Macro3 Macro<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">&#8216;<\/p>\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">&#8216;<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Select<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">ActiveCell.FormulaR1C1 = \u201c=RAND()\u201d<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Select<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Selection.Copy<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA4\u201d).Select<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">SkipBlanks :=False, Transpose:=False<\/p>\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA5\u201d).Select\\\\<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">These lines are the actual commands you gave Excel. Notice that an apostrophe at the start of a line comments out that line (it is not executed) and is displayed in green-colored text (e.g., the line Macro3 Macro). The underscore character, _, continues the current statement on the next line.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You could clean up this code so it looks like this:<\/p>\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">Range(\u201cA3\u201d).FormulaR1C1 = \u201c=RAND()\u201d<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA3\u201d).Copy<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA4\u201d).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">SkipBlanks:=False, Transpose:=False<\/p>\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">Range(\u201cA5\u201d).Select<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">You could comment out the first line, and it would still work since the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">RAND()<\/em><\/span> function is already in cell A3. Running this macro replaces the contents of cell A3 without any warning. You do not notice this because the code writes the same formula that was there.<\/p>\n<\/div>\n<\/div>\n<h1 class=\"import-ah\">9.2 Functions in VBA<\/h1>\n<p class=\"import-paft\">Attaching a macro to a button or other object allows you and your users to run VBA code, but there is another way to access code from an Excel workbook: a <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">user-defined<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> (UDF).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Unlike a native function, like =RAND() or =SUM(cell range), which is part of the Excel application itself, UDFs are functions that you write in VBA and are stored in a module in a macro-enabled workbook (file name extension .xlsm).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">To the user, UDFs work the same way as native functions, so they require no special knowledge. The only extra step is that the user has to enable the content in the workbook (or disable all security, which is not recommended).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">To demonstrate how UDFs work, we will create two user-defined functions, the first without any arguments and the second allowing the user to pass information to the function, making it more flexible and useful.<\/p>\n<h2 class=\"import-bh\">UDF with No Arguments<\/h2>\n<p class=\"import-paft\">Recall from our work on Monte Carlo simulation that we can create a 90% free throw shooter in Excel with the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=IF(RAND()&lt;0.9,1,0)<\/em><\/span>. Alternatively, we could create a function in VBA\u2014say, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span>\u2014that the user could enter in any cell. Ninety percent of the time it would produce a 1, and the remaining 10% would be 0.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Open your <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">IntroVBA.xlsm<\/em><\/span> macro-enabled workbook and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> to go to Visual Basic. Insert a new module sheet in this workbook. Type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span> (uppercase FT) and hit enter.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Excel\u2019s Visual Basic Editor capitalizes the F in <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">function<\/em><\/span>, adds an <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> for you, and colors the text blue for the beginning and ending lines of the code.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Instead of the Excel function <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span>, VBA has its own random number generator <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span>. It produces random numbers in the interval from 0 to 1. It is supereasy to make our UDF output a random number.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>. Return to Excel (<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Alt<\/em><em class=\"import-i\">-F11<\/em><\/span> is a toggle) and click on cell H1. Enter the function <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FT()<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Congratulations! You just wrote your first function in Excel and accessed it from Excel.<\/p>\n<div class=\"textbox\">\n<p class=\"import-bxt\" style=\"padding-left: 40px;\"><span style=\"color: #339966;\"><strong><em>EXCEL TIP <\/em><\/strong><\/span>For UDFs, Excel remembers how you first entered the function. If you enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=ft()<\/em><\/span> (lowercase), it will keep using lowercase (even if you enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> in a different cell). This is not true for native functions: Enter <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=rand()<\/em><\/span> and Excel converts it to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=RAND()<\/em><\/span>.<\/p>\n<\/div>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">There is, however, a problem with our UDF.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times. Nothing happens to cell H1. It is not bouncing like <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">RAND()<\/em><\/span>, generating a new random number each time we recalculate the sheet.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The problem is that UDFs, by default, are nonvolatile functions. This means they do not get recalculated unless they depend on other cells that have changed. We must add code to make our function recalculate when <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> is pressed.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Return to your VBA code for the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> function. Click on the top line, after the close parenthesis, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span> so that you are on a new, blank line and enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Application.Volatile<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">True<\/em><\/span>. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>. Return to Excel and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times. Success!<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">But we do not want to simply output a random number; we want to see if we made (1) or missed (0) a free throw attempt. We need to add some code to do this. Like the Excel formula we used to model a free throw result, we need an <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement to separate made from missed free throws.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Go to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> code. Click after <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">True<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span> to create a new line after the volatile statement. Enter the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">if <\/em><em class=\"import-i\">rnd<\/em><em class=\"import-i\"> &lt; 0.9 then<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Enter<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">As usual, the editor capitalizes and colors the text for you. The next line of code defines what happens if the random number is less than 0.9. It is followed by lines of code for missed free throws.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Press the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Tab<\/em><\/span> key to indent, type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span>, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Type the word <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Else<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Tab<\/em><\/span>, type <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Type <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">end if<\/em><\/span> and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">enter<\/em><\/span>. Put a straight single quote (&#8216;) in front of the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT=<\/em><em class=\"import-i\">Rnd<\/em><\/span> line to comment it out (so it does not get executed).<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Your masterpiece of code should look like this (with color added to keywords and lines):<\/p>\n<p class=\"import-clf\" style=\"margin-left: 36pt; margin-right: 36pt; text-autospace: none;\">Function FT()<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Application.Volatile True<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">If Rnd &lt; 0.9 Then<\/p>\n<p class=\"import-cl1\" style=\"margin-left: 54pt; margin-right: 36pt;\">FT = 1<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">Else<\/p>\n<p class=\"import-cl1\" style=\"margin-left: 54pt; margin-right: 36pt;\">FT = 0<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">End If<\/p>\n<p class=\"import-cl\" style=\"margin-left: 36pt; margin-right: 36pt;\">&#8216;FT = Rnd<\/p>\n<p class=\"import-cll\" style=\"margin-left: 36pt; margin-right: 36pt;\">End Function<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The macro draws a uniformly distributed random number on the interval from 0 to 1 (<span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span>), and if it is less than 0.9, it goes to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> line. Since the function is called <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span>, it outputs the number 1 if <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Rnd<\/em><\/span> &lt; 0.9. If the random number drawn is not less than 0.9, it goes to the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> line and outputs a 0.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">The code is easy to read, but does it actually work? Let\u2019s find out.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Return to Excel, fill cell H1 down to cell H10, and press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">That is pretty cool, but what if we wanted a more generalized version, where the user tells us the chances of success?<\/p>\n<h2 class=\"import-bh\">UDF with an Argument<\/h2>\n<p class=\"import-paft\">In native Excel functions\u2014like <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">SUM<\/em><\/span>, for example\u2014the arguments are passed in the parentheses: <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">SUM(A1:A3)<\/em><\/span>. UDFs work the same way. We will add an argument to our code in the parentheses and modify the code to enable it to incorporate the information provided by the user.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Copy the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> code and paste it below the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End Function<\/em><\/span> line. Since you cannot have two functions with the same name, change the name of the newly pasted function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> (for argument). In the parentheses, type the text <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot as Double<\/em><\/span>. Replace the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0.9<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement line with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot<\/em><\/span>. Return to Excel and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FTARG(0.5)<\/em><\/span> in cell I1.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Excel displays an error message in cell I1. Can you figure out what is wrong with the UDF and fix it? If you cannot, take a look at the appendix.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">We can make the function even more flexible by having it accept a value in another cell.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\"><span style=\"color: #ff0000;\"><em class=\"import-hemb-i\">STEP<\/em> <\/span>Copy the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> code and paste it below the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">End<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> line. Change the name of the newly pasted function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> (for argument from another cell). In the parentheses, change the text to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myShootCell<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">as<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Range<\/em><\/span>. Replace <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Shoot<\/em><\/span> in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">If<\/em><\/span> statement line with <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">myShootCell.Value<\/em><\/span>. Change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> lines to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARGCELL<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>. To see how this works, return to Excel and enter the formula <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=FTARGCELL($K$1)<\/em><\/span> in cell J1. Fill it down to cell J10. In cell K1, enter a number from 0 to 1, such as 0.25. Press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span> a few times.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">With a 25% chance of success, your 10 numbers in column J are bouncing around every time you press <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">F9<\/em><\/span>, and you usually get 2 or 3 ones.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Is it better to allow the user to input a number as the argument or a cell address? That depends on the context of the problem, including the user\u2019s familiarity with Excel. In fact, if you do not need to change the success rate, our original UDF, <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT()<\/em><\/span>, might be the best choice.<\/p>\n<div class=\"textbox textbox--key-takeaways\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Takeaways<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"import-paft\">VBA code can be accessed by users from Excel\u2019s front end by attaching macros to buttons and other controls.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Another option is a UDF. The user enters a formula that runs the UDF code to do computations or other manipulations that are not available in native Excel functions.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Like native Excel functions, UDFs usually require arguments. These variables are declared in the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">Function<\/em><\/span> statement, inside the parentheses.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Writing code is science and art. Deciding whether arguments are needed and, if so, how to pass them (numbers or cell addresses, for example) requires knowledge of what needs to be done and who is going to do it. You need to know your audience.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">Most people think of Excel as some kind of sophisticated adding machine or calculator. Excel can certainly do arithmetic and other mathematical operations, but its back end or kitchen opens up a whole new world of opportunities and possibilities.<\/p>\n<\/div>\n<\/div>\n<div class=\"textbox textbox--exercises\">\n<header class=\"textbox__header\">\n<p class=\"textbox__title\">Appendix<\/p>\n<\/header>\n<div class=\"textbox__content\">\n<p class=\"import-paft\">The <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> function fails when you change the name of the function to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> but do not likewise update the name of the function in the code. You must change the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> line to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">1<\/em><\/span> and the <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FT<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span> line to <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">FTARG<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">=<\/em><\/span> <span style=\"border: none windowtext 0pt; padding: 0;\"><em class=\"import-i\">0<\/em><\/span>.<\/p>\n<p class=\"import-p\" style=\"text-indent: 36pt;\">When you write a UDF, you always use the function name to output a result. If you need more than one cell to output results, you can use an array function.<\/p>\n<\/div>\n<\/div>\n","protected":false},"author":13,"menu_order":9,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapters\/44"}],"collection":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/wp\/v2\/users\/13"}],"version-history":[{"count":10,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapters\/44\/revisions"}],"predecessor-version":[{"id":370,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapters\/44\/revisions\/370"}],"part":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapters\/44\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/pressbooks\/v2\/chapter-type?post=44"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/wp\/v2\/contributor?post=44"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.palni.org\/gatewaytobusinessanalytics\/wp-json\/wp\/v2\/license?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}