XP Microsoft Excel 2002 Tutorial 8 – Developing an Excel Application New Perspectives on Microsoft Excel 2002 Tutorial 8 1 XP Create validation rules for data entry • You will want to prevent errors in your workbooks as much as possible. – You can specify the type of data that is allowed and/or a range of acceptable values – If a value is entered that does not meet the requirements, an error message is displayed – Setting a rule like this is a preventative measure that allows you to validate data upon entry • There are several different options in the Data Validation dialog box allowing you to provide various rules related to data entry. • You can also provide an input message that will aid the user in entering the data. New Perspectives on Microsoft Excel 2002 Tutorial 8 2 XP The Data Validation dialog box This figure shows the Data Validation dialog box. Note the three tabs on the dialog box. Use the Settings tab to specify the validation criteria. Use the Input Message tab to specify a prompt that will aid the user in entering data. Use the Error Alert tab to set what style of error message you want to appear when the validation criteria is not met. New Perspectives on Microsoft Excel 2002 Tutorial 8 3 XP The Allow list box options The table below shows the options available in the Allow list box field of the Data Validation dialog box, and a description of their purpose. New Perspectives on Microsoft Excel 2002 Tutorial 8 4 The Input Message tab of the DataXP Validation dialog box On the Input Message tab of this dialog box, you can create an input message to display when a user clicks on a worksheet cell. New Perspectives on Microsoft Excel 2002 Tutorial 8 5 A worksheet with an input message displayed XP This figure shows a portion of a worksheet with an Input Message displayed for a date field. By prompting the user for the correct information, you reduce the risk of error. New Perspectives on Microsoft Excel 2002 Tutorial 8 6 The Error Alert tab of the Data XP Validation dialog box On the Error Alert tab of this dialog box, you can create a Stop alert that will display if the user attempts to enter something that violates the validation rules defined for the field. New Perspectives on Microsoft Excel 2002 Tutorial 8 7 XP Protect the contents of worksheets and workbooks • Once you have a worksheet that you know is correct, you may want to protect the worksheet so that users cannot make changes. – Setting the locked property will disallow any changes to a particular cell – The worksheet will have to be protected in order for the locked property to have any affect • You can also specify a password that must be entered in order to remove worksheet protection. • Finally, you can protect an entire workbook, which would disallow changes to the workbook, such as adding or deleting worksheets. New Perspectives on Microsoft Excel 2002 Tutorial 8 8 XP Unlocking Selected Cells To lock or unlock specific cells, click the cells to select them, click the Format menu, click Cells, and then click the Protection tab, as shown in this figure. Click the Locked option box to insert or remove the check mark. New Perspectives on Microsoft Excel 2002 Tutorial 8 9 XP The Protect Sheet dialog box This figure shows the Protect Sheet dialog box. Notice that there are many options on this dialog box that you can set to allow or disallow. These rules will apply only to the selected worksheet. New Perspectives on Microsoft Excel 2002 Tutorial 8 10 XP The Protect Workbook dialog box To protect an entire workbook, click the Tools menu, point to Protection, and then click Protect Workbook. The dialog box shown here will open. Select the option(s) you want, and you can also assign an optional password to prevent anyone else from changing the settings you specify. New Perspectives on Microsoft Excel 2002 Tutorial 8 11 XP Create and use range names • It may be useful assign a name to a cell or cell range. • This is called a range name and allows you to refer to the cell or range of cells by their name instead of their cell references. – For example, you might assign a range name, Expenses, to a group of cells that represent your expenses – When you want to calculate with those cells, you enter their Range Name in the formulas instead of the cell reference • You can later change the definition of the range names. New Perspectives on Microsoft Excel 2002 Tutorial 8 12 The Allow Users to Edit Ranges XP dialog box If you want to allow users to edit portions of a worksheet, you can set up ranges in which the user can make edits. This is done on the Allow Users to Edit Ranges dialog box as shown in this figure. New Perspectives on Microsoft Excel 2002 Tutorial 8 13 XP The Define Name dialog box The figure below is an example of the Define Name dialog box. It is in this dialog box that you can establish range names and set their definition. The easiest approach is to select the cells you want to have a name first and then open this dialog box by clicking Name from the Insert menu and then clicking Define. New Perspectives on Microsoft Excel 2002 Tutorial 8 14 XP View multiple range names This figure shows the Define Name dialog box with multiple ranges defined. If you click on a range name, the Refers to: box at the bottom tells you what cells are defined for that range. New Perspectives on Microsoft Excel 2002 Tutorial 8 15 XP Using Range Names in a chart This figure shows a chart that has been defined using range names to specify the data. Note that the workbook name must precede the range name. New Perspectives on Microsoft Excel 2002 Tutorial 8 16 Macro viruses and Excel's securityXP features • Because a macro is a program, a virus can be attached to the macro. – A macro is attached to a workbook and just opening the workbook can cause the macro to be run – If a macro has an attached virus, running the macro will likely infect the computer being used to view the workbook • The first line of defense is to be sure you know where the workbook came from and whether the source is trustworthy. • Excel allows you to specify security for any workbook opened within Excel. • There are three levels of security: high, medium, and low. New Perspectives on Microsoft Excel 2002 Tutorial 8 17 XP The Security dialog box This figure shows the Security dialog box. Before making a selection, you should read the explanatory information associated with each choice. Notice that Low security is not recommended and should rarely be selected. New Perspectives on Microsoft Excel 2002 Tutorial 8 18 XP Create macros using the macro recorder • One way to create a macro is to use the Excel macro recorder. – When you start the macro recorder, all of your keystrokes are recorded and saved – Once you have completed the keystrokes you want recorded, you can close the macro – Once the macro has been created, you can replay the macro at anytime • The macro can be stored in the workbook, making it available whenever the workbook is opened. • You can also store the macro in a new workbook or in the Personal Macro workbook, which makes it available anytime Excel is running. New Perspectives on Microsoft Excel 2002 Tutorial 8 19 XP The Record Macro dialog box When you create a macro, you need to give it a name. The figure below shows the Record Macro dialog box. In this figure, the macro is named Report30. Notice also the assignment of the “a” key as the shortcut key and that the macro will be stored in the active workbook. New Perspectives on Microsoft Excel 2002 Tutorial 8 20 XP The Macro dialog box The following figure is another example of the Macro dialog box. In this dialog box, three macros have been created. Notice the Run button. To run a macro, you select the macro you want from the list and press the Run button. New Perspectives on Microsoft Excel 2002 Tutorial 8 21 Edit and print a macro using theXP Visual Basic Editor • All macros are small programs written in Visual Basic for Applications (VBA), which is the programming language for Office XP applications. • You can create a new macro or edit an existing macro in the Visual Basic Editor. In the editor: – You can see the VBA statements that make the macro work – You can view and edit the commands • The body of a macro is enclosed between the “Sub” and “End Sub” statements. • The body consists of the statements that will be performed when the macro is run. New Perspectives on Microsoft Excel 2002 Tutorial 8 22 XP The VBA editor • Learning to program in VBA is an extensive process. • However, you can read through an existing macro and get an idea of how the macro works. • You can print the macros so that you can review the printout. New Perspectives on Microsoft Excel 2002 Tutorial 8 23 XP The Visual Basic editor This figure shows the Visual Basic Editor with the NewData procedure displayed in the editor window. The macro begins with the “Sub NewData()” statement and end with the “End Sub” statement. New Perspectives on Microsoft Excel 2002 Tutorial 8 24 XP Update a macro in the VBA editor This figure shows a macro that has had a statement inserted to turn off screen updating. Once you learn VBA syntax, you can write entire macros using the editor instead of using the macro recording mechanism. New Perspectives on Microsoft Excel 2002 Tutorial 8 25 XP Assign a macro to a keyboard shortcut • You already learned how to invoke a macro through the macro menu. • Another option is to assign a shortcut key to the macro. • When the shortcut key is pressed, the macro is run. New Perspectives on Microsoft Excel 2002 Tutorial 8 26 XP Assign a macro to a button • You can create a button on the workbook that will invoke a macro. • To create a macro button, you must first display the Forms toolbar. • Then, select the button tool on the toolbar. • The button is assigned to a particular macro and, when it is pressed, the assigned macro runs. New Perspectives on Microsoft Excel 2002 Tutorial 8 27 XP The Forms toolbar In this figure, you see the Forms toolbar. Notice the button tool on the right side of the toolbar. This button tool will create a command button on your worksheet. New Perspectives on Microsoft Excel 2002 Tutorial 8 28 XP A macro button on a worksheet Once you have placed the command button on the worksheet, as shown in the figure below, you can assign the button to a macro. This makes it possible for the user to invoke the macro simply by pressing the button. In this figure, the text on the button is Button 8, however, this text can be altered to reflect how the button is to be used. New Perspectives on Microsoft Excel 2002 Tutorial 8 29 XP Add multiple macro buttons This figure shows a worksheet that has had macro buttons added to run a 30-day report, a 60-day report, or a 90-day report. New Perspectives on Microsoft Excel 2002 Tutorial 8 30