Excel 2019 Power Programming with VBA. Michael Alexander

Читать онлайн.
Название Excel 2019 Power Programming with VBA
Автор произведения Michael Alexander
Жанр Программы
Серия
Издательство Программы
Год выпуска 0
isbn 9781119514916



Скачать книгу

a directory that is deemed a safe zone where only trusted workbooks are placed. A trusted location allows you and your clients to run a macro-enabled workbook with no security restrictions as long as the workbook is in that location.

      1 Select the Macro Security button on the Developer tab. This activates the Trust Center dialog box.

      2 Click the Trusted Locations button. This opens the Trusted Locations menu (see Figure 2.6), which shows you all the directories that are considered trusted.FIGURE 2.6 The Trusted Locations tab allows you to add directories that are considered trusted.

      3 Click the Add New Location button.

      4 Click Browse to find and specify the directory that will be considered a trusted location.

      After you specify a trusted location, any Excel file that is opened from this location will have macros automatically enabled.

      Storing macros in your Personal Macro Workbook

      Most user-created macros are designed for use in a specific workbook, but you may want to use some macros in all of your work. You can store these general-purpose macros in the Personal Macro Workbook so that they're always available to you. The Personal Macro Workbook is loaded whenever you start Excel. This file, named Personal.xlsb, doesn't exist until you record a macro using Personal Macro Workbook as the destination.

      If you store macros in the Personal Macro Workbook, you don't have to remember to open the Personal Macro Workbook when you load a workbook that uses macros. When you want to exit, Excel asks whether you want to save changes to the Personal Macro Workbook.

      NOTE

      The Personal Macro Workbook normally is in a hidden window to keep it out of the way.

      Assigning a macro to a button and other form controls

      When you create macros, you may want to have a clear and easy way to run each macro. A basic button can provide a simple but effective user interface.

      As luck would have it, Excel offers a set of form controls designed specifically for creating user interfaces directly on spreadsheets. There are several different types of form controls, from buttons (the most commonly used control) to scrollbars.

      The idea behind using a form control is simple. You place a form control on a spreadsheet and then assign a macro to it—that is, a macro you've already recorded. When a macro is assigned to the control, that macro is executed, or played, when the control is clicked.

      Take a moment to create a button for the AddTotalRelative macro you created earlier. Here's how:

      1 Click the Insert button on the Developer tab (see Figure 2.7).FIGURE 2.7 You can find the form controls on the Developer tab.

      2 Select the Button control from the drop-down list that appears.

      3 Click the location where you want to place your button.When you drop the button control onto your spreadsheet, the Assign Macro dialog box, as shown in Figure 2.8, activates and asks you to assign a macro to this button.FIGURE 2.8 Assign a macro to the newly added button.

      4 Select the macro that you want to assign to the button and then click OK.

      At this point, you have a button that runs your macro when you click it. Keep in mind that all the controls in the Form Controls group (shown in Figure 2.7) work in the same way as the command button in that you right-click and choose Assign Macro to specify a macro to trigger with the control.

      NOTE

      Notice the form controls and ActiveX controls in Figure 2.7. Although they look similar, they're quite different. Form controls are designed specifically for use on a spreadsheet, and ActiveX controls are typically used on Excel user forms. As a general rule, you should always use form controls when working on a spreadsheet. Why? Form controls need less overhead, so they perform better, and configuring form controls is far easier than configuring their ActiveX counterparts.

      Placing a macro on the Quick Access toolbar

      You can also assign a macro to a button in Excel's Quick Access toolbar. The Quick Access toolbar sits either above or below the Ribbon. You can add a custom button that will run your macro by following these steps:

      1 Right-click your Quick Access toolbar and select Customize Quick Access Toolbar. This will open the dialog box illustrated in Figure 2.9.FIGURE 2.9 Adding a macro to the Quick Access toolbar

      2 Select Macros from the Choose Commands From drop-down list on the left.

      3 Select the macro that you want to add and click the Add button.

      4 Click the Modify button to choose an icon for your macro and provide a friendly display name.

      5 Click the OK button.

      The Visual Basic Editor is a separate application that runs when you open Excel. To see this hidden VBE environment, you'll need to activate it. The quickest way to activate the VBE is to press Alt+F11 when Excel is active. To return to Excel, press Alt+F11 again.

      You can also activate the VBE by using the Visual Basic command on Excel's Developer tab.

      Understanding VBE components

Screenshot of the Visual Basic Editor with significant elements identified such as Menu Bar, Toolbar, Project Window, Properties Window, Code Window, and Immediate Window.

      Menu bar

      The VBE menu bar works just like every other menu bar you've encountered. It contains commands that you use to do things with the various components in the VBE. You will also find that many of the menu commands have shortcut keys associated with