Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Excel_2010_Bible.pdf
Скачиваний:
26
Добавлен:
13.03.2015
Размер:
11.18 Mб
Скачать

CHAPTER

Introducing Visual Basic for Applications

This chapter is an introduction to the Visual Basic for Applications (VBA) macro language — a key component for users who want to customize and automate Excel. This chapter teaches you how to

record macros and create simple macro procedures. Subsequent chapters expand upon the topics in this chapter.

Introducing VBA Macros

In its broadest sense, a macro is a sequence of instructions that automates some aspect of Excel so that you can work more efficiently and with fewer errors. You may create a macro, for example, to format and print your month-end sales report. After the macro is developed, you can then execute the macro to perform many time-consuming procedures automatically.

You don’t have to be a power user to create and use simple VBA macros. Casual users can simply turn on Excel’s macro recorder: Excel records your actions and converts them into a VBA macro. When you execute this macro, Excel performs the actions again. More advanced users, though, can write code that tells Excel to perform tasks that can’t be recorded. For example, you can write procedures that display custom dialog boxes, add new commands to Excel’s menus, or process data in a series of workbooks.

IN THIS CHAPTER

Introducing VBA macros Creating VBA macros Recording VBA macros

More about recording VBA macros

Writing VBA code Learning more about VBA

795

Part VI: Programming Excel with VBA

What You Can Do with VBA

VBA is an extremely rich programming language with thousands of uses. The following list contains just a few things that you can do with VBA macros. (Not all of these tasks are covered in this book.)

Insert boilerplate text. If you need to enter standard text into a range of cells, you can create a macro to do the typing for you.

Automate a procedure that you perform frequently. For example, you may need to prepare a month-end summary. If the task is straightforward, you can develop a macro to do it for you.

Automate repetitive operations. If you need to perform the same action in 12 different workbooks, you can record a macro while you perform the task once — and then let the macro repeat your action in the other workbooks.

Create a custom command. For example, you can combine several Excel commands so that they’re executed from a single keystroke or from a single mouse click.

Create a simplified “front end” for users who don’t know much about Excel. For example, you can set up a foolproof data-entry template.

Develop a new worksheet function. Although Excel includes a wide assortment of built-in functions, you can create custom functions that greatly simplify your formulas.

Create complete macro-driven applications. Excel macros can display custom dialog boxes and respond to new commands added to the Ribbon.

Create custom add-ins for Excel. Most add-ins shipped with Excel were created with Excel macros. I used VBA exclusively to create my Power Utility Pak.

Displaying the Developer Tab

If you plan to work with VBA macros, you’ll want to make sure that the Developer tab is present on the Excel Ribbon. The Developer tab, which does not appear by default, contains useful commands for VBA users. To display this tab

1.Choose File Options.

2.In the Excel Options dialog box, select Customize Ribbon.

3.In the list box on the right, place a check mark next to Developer.

4.Click OK to return to Excel.

Figure 39.1 shows the Ribbon commands of the Developer tab.

796

Chapter 39: Introducing Visual Basic for Applications

FIGURE 39.1

The Developer tab.

About Macro Security

Macros have the potential to cause serious damage to your computer, such as erasing files or installing malware. Consequently, Microsoft has added macro-security features to help prevent macro-related problems.

Figure 39.2 shows the Macro Settings section of the Trust Center dialog box. To display this dialog box, choose Developer Code Macro Security.

FIGURE 39.2

The Macro Settings section of the Trust Center dialog box.

By default, Excel uses the Disable All Macros with Notification option. With this setting in effect, if you open a workbook that contains macros (and the file is not digitally “signed”), the macros will be disabled, and Excel displays a Security Warning above the Formula bar (see Figure 39.3). If you’re certain that the workbook comes from a trusted source, click the Enable Content button in the security warning area, and the macros will be enabled. Excel remembers your decision; if you enable the macros, you will not see the Security Warning the next time you open that file.

797

Part VI: Programming Excel with VBA

Note

If the Visual Basic (VB) Editor window is open when you open a workbook that contains macros, Excel does not display the Security Warning above the Formula bar. Rather, it displays a dialog box with two buttons: Enable Macros and Disable Macros. n

FIGURE 39.3

Excel displays a Security Warning if a workbook contains macros.

Rather than deal with individual workbooks, you may prefer to designate one or more folders as “trusted locations.” All the workbooks in a trusted location are opened without a macro warning. You designate trusted folders in the Trusted Locations section of the Trust Center dialog box.

Saving Workbooks That Contain Macros

If you store one or more VBA macros in a workbook, you must save the file with an XLSM extension.

The first time you save a workbook that contains macros, the file format defaults to XLSX — and this format can’t contain macros. Unless you change the file format to XLSM, Excel displays the warning shown in Figure 39.4. You need to click No, and then choose Excel Macro-Enabled Workbook (*.xlsm) from the Save As Type drop-down list in the Save As dialog box.

Note

Alternatively, you can save the workbook in the old Excel 97-2003 format (which uses an XLS extension). This file format can contain macros. n

FIGURE 39.4

Excel warns you if your workbook contains macros and you attempt to save it in a nonmacro file format.

798

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]