Excel VBA Programming For Dummies. Dick Kusleika

Читать онлайн книгу.

Excel VBA Programming For Dummies - Dick  Kusleika


Скачать книгу
13, you’ll appreciate this option.

       Auto Indent setting: The Auto Indent setting determines whether the VBE automatically indents each new line of code the same as the previous line. Use the Tab key to indent your code, not the spacebar. Also, you can press Shift+Tab to “unindent” a line of code. If you want to indent more than just one line, select all the lines you want to indent. Then press the Tab key. The VBE’s Edit toolbar (which is hidden by default) contains two useful buttons: Indent and Outdent. These buttons let you quickly indent or “unindent” a block of code. Select the code and click one of these buttons to change the block’s indenting.

       Tab Width: Determines how wide your tab characters are in the equivalent number of spaces.

       Drag-and-Drop Text Editing option: The Drag-and-Drop Text Editing option, when enabled, lets you move or copy and paste text by dragging and dropping with your mouse.

       Default to Full Module View option: The Default to Full Module View option sets the default state for new modules. (It doesn’t affect existing modules.) If this option is set, procedures in the code pane appear as a single scrollable list. If this option is turned off, you can see only one procedure at a time.

       Procedure Separator option: When the Procedure Separator option is turned on, separator bars appear between procedures in a code pane.

      Using the Editor Format tab

       Code Colors option: The Code Colors option lets you set the text color and background color displayed for various elements of VBA code. This is largely a matter of personal preference.

       Font option: The Font option lets you select the font that’s used in your VBA modules. For best results, stick with a fixed-width font such as Courier New. In a fixed-width font, all characters are exactly the same width. This makes your code more readable because the characters are nicely aligned vertically, and you can easily distinguish multiple spaces (which is sometimes useful).

       Size setting: The Size setting specifies the point size of the font in the VBA modules. This setting is a matter of personal preference determined by your video display resolution and how many carrots you’ve been eating.

       Margin Indicator Bar option: This option controls the display of the vertical margin indicator bar in your modules. You should keep this turned on; otherwise, you won’t be able to see the helpful graphical indicators when you’re debugging your code.

Snapshot of changing how the VBE looks with the Editor Format tab.

      Using the General tab

Snapshot of General tab of the Options dialog box.

      FIGURE 3-9: The General tab of the Options dialog box.

      The most important setting is Error Trapping. It’s considered a best practice not to use the Break on All Errors setting. If you use this setting, your error-handling code won’t work. You can read more about this in Chapter 12.

      If you’re really interested in these options, click the Help button for details.

      Using the Docking tab

      

Sometimes, the VBE seems to have a mind of its own when you’re trying to dock a window. If docking doesn’t seem to work correctly, try double-clicking the title bar of the window.

Snapshot of General tab of the Options dialog box.H31

      FIGURE 3-10: The Docking tab of the Options dialog box.

      Introducing the Excel Object Model

      IN THIS CHAPTER

      

Learning the Excel Object Model and its hierarchy

      

Understanding how to access properties and methods

      

Discovering resources to help you code

      The purpose of Excel VBA is to manipulate Excel. Some of the code you write will be VBA language constructs — aspects of the language that are common to all programming languages. The rest of the code you write will be specific to Excel. In this chapter, you learn about the Excel Object Model. The object model is the gateway into all the objects that make up Excel. Manipulating these objects is what Excel VBA programming is all about.

      The Excel Object Model is computer code that Microsoft wrote that defines how VBA interacts with Excel. In it, Microsoft has defined hundreds of objects whose names you would recognize as elements of the Excel user interface, like Workbook, Worksheet, or PivotTable. An object is nothing more than computer code that defines properties, methods, and events. It is the unique combination of properties, methods, and events that defines an object. Properties and methods are discussed later in this chapter and events are discussed in Chapter 11.

      Excel’s object model is arranged as a hierarchy. That is, every object is organizationally a parent or a child of another object, like a family tree. There’s no magic to the hierarchy; it’s just how Microsoft designed it. But you won’t be surprised to learn that it mostly mirrors what you see in the user interface. Worksheet objects are children of a Workbook object just like how workbook hold worksheets in Excel. A Worksheet object is the parent of a PivotTable object because PivotTables exist on sheets in Excel.


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