Excel VBA Programming For Dummies. Dick Kusleika

Чтение книги онлайн.

Читать онлайн книгу Excel VBA Programming For Dummies - Dick Kusleika страница 16

Excel VBA Programming For Dummies - Dick  Kusleika

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

.Size = 18

      When you use the macro recorder, you don’t have to know a lot of VBA language to be able to find the line you want to change and change it. However, if it’s not as straightforward as the font size example and you’re not sure what to change, you can record a new macro to see what the recorder will generate when you do what you want. Then compare the new macro to the old one, manually edit the old one to look like the new one, and delete what you just recorded.

      

Working in a VBA code module is much like working in a word-processing document (except there’s no word wrap, and you can’t format the text). On second thought, it’s more like working in Windows Notepad. You can press Enter to start a new line, and the familiar editing keys work as expected.

      If you store one or more macros in a workbook, the file must be saved as a macro-enabled file type. In other words, the file must be saved with an XLSM extension rather than the normal XLSX extension.

Snapshot of attempt to save it in a macro-free file format, Excel warns you.

      FIGURE 2-5: If your workbook contains macros, and you attempt to save it in a macro-free file format, Excel warns you.

      Macro security is a key feature in Excel. The reason is that VBA is a powerful language — so powerful that it’s possible to create a macro that can do serious damage to your computer. A macro can delete files, send information to other computers, and even destroy Windows so that you can’t start your system.

Snapshot of Macro Settings section of the Trust Center dialog box.

      FIGURE 2-6: The Macro Settings section of the Trust Center dialog box.

Snapshot of Excel’s warning that the file to be opened contains macros.

      FIGURE 2-7: Excel’s warning that the file to be opened contains macros.

You see the pop-up box in Figure 2-7 only when the VBE is open. Otherwise, Excel displays an eye-catching Security Warning above the Formula bar, as shown in Figure 2-8. If you know the workbook is safe, click the Enable Content button to enable the macros. To use the workbook without macros, click the X to dismiss the warning.

      Excel remembers when you designate a workbook to be safe. So the next time you open the workbook, you won’t see the Security Warning.

Snapshot of Excel's warning that the workbook just opened contains macros.

      FIGURE 2-8: Excel's warning that the workbook just opened contains macros. You see this warning when the VBE isn’t open.

      If you want to find out what the other macro security settings mean, press F1 while the Macro Settings section of the Trust Center dialog box is in view and review the Help screen that appears.

      Employing VBA with Excel

      See how to access the important parts of the Visual Basic Editor.

      Discover VBA code modules (where you store your VBA code).

      Obtain an overview of the Excel object model.

      Get a crash course in using the Excel macro recorder.

      Working in the Visual Basic Editor

      IN THIS CHAPTER

      

Accessing the Visual Basic Editor

      

Discovering the Visual Basic Editor parts

      

Knowing what goes into a VBA module

      

Getting VBA code into a module

      

Customizing the VBA environment

      The Visual Basic Editor (VBE) is the main tool for programming in VBA. It’s where you view and edit recorded code and write code from scratch. In this chapter, you find out how to work with the VBE, and you get down to the nitty-gritty of writing some VBA code.

      The Visual Basic Editor (often referred to as the VBE) is a separate application where you write and edit your VBA macros. Beginning with Excel 2013, when you open multiple workbooks, each opens in a separate window. However, the VBE only ever has one window, and from this window

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