Название: Excel VBA Programming For Dummies
Автор: Dick Kusleika
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119843092
isbn:
Activating the VBE
The quickest way to activate the VBE is to press Alt+F11 from an Excel window. To return to Excel, press Alt+F11 again. Or you can simply click the Close button on the VBE’s title bar. When the VBE window closes, Excel becomes the active window.
You can also activate the VBE by choosing Developer ⇒ Code ⇒ Visual Basic. If you don’t have a Developer tab at the top of your Excel window, refer to Chapter 2, which shows how to get that handy Developer tab to show up.
Exploring VBE components
The VBE contains several windows and is highly customizable. You can hide windows, rearrange windows, dock windows, and more. Figure 3-1 shows the VBE program, with some key parts identified. Your VBE program window probably won’t look exactly like what is shown in Figure 3-1, but from the figure, you can at least get a feel for the basic appearance of the VBE.
FIGURE 3-1: The VBE is your customizable friend.
Menu bar
The VBE menu bar contains commands you can use to do things with the various components in the VBE. Many of the menu commands have shortcut keys associated with them.
The VBE also features shortcut menus. You can right-click virtually anything in the VBE and get a shortcut menu of common commands.
Toolbar
The Standard toolbar, which is directly below the menu bar by default (refer to Figure 3-1), is one of the four VBE toolbars. You can customize the toolbars, move them around, display other toolbars, and more. To do so, choose View ⇒ Toolbars, and customize as you see fit. Most people just leave them as they are.
Project Explorer
The Project Explorer displays a tree diagram that shows every workbook currently open in Excel (including add-ins and hidden workbooks). Double-click items to expand or contract them within the outline. See the upcoming “Working with the Project Explorer” section for more detail.
If the Project Explorer is not visible, press Ctrl+R or choose View ⇒ Project Explorer. To hide the Project Explorer, click the Close button on its title bar. Or right-click anywhere in the Project Explorer and choose Hide from the shortcut menu.
Code pane
A Code pane is where you put your VBA code. Every object in a project has an associated Code pane. To view an object’s Code pane, double-click the object in the Project Explorer. For example, to view the Code pane for the Sheet1 object in Book1, double-click Sheet1 in the VBAProject for Book1. Unless you’ve added some VBA code, the Code pane is empty.
You find out more about Code panes later in this chapter’s “Working with a Code Pane” section.
Immediate window
The Immediate window may or may not be visible. If it isn’t visible, press Ctrl+G or choose View ⇒ Immediate Window. To close the Immediate window, click the Close button on its title bar (or right-click anywhere in the Immediate window and choose Hide from the shortcut menu).
GETTING HELP IN THE VBE
Beginning with Excel 2013, all VBA help information is on the internet and is displayed in your web browser. In other words, you must be connected to the internet to access the Help system. You can, however, download your very own copy of the Help system from Microsoft’s site. Do a web search for download excel VBA documentation, and you’ll find it. Pick the link at microsoft.com and don’t worry if the version they have is a few versions older than yours — they don’t update it all that often.
The Immediate window is most useful for executing VBA statements directly and for debugging your code. If you’re just starting out with VBA, this window won’t be all that useful, so feel free to hide it and free up some screen space for other things.
Chapter 13 covers the Immediate window in detail. It may just become your good friend!
Working with the Project Explorer
In the VBE, workbooks are called projects. Every workbook contains exactly one project, and every project is associated with exactly one workbook. A project holds all the modules and keeps them neatly in folders that tell you what kind of module it is. There are four kinds of modules, each kind in its own folder:
Microsoft Excel Objects: These are modules that are linked to user interface elements like workbooks and worksheets. See Chapter 11 for more about these types of modules.
Modules: Excel calls these simply modules, but everyone else refers to them as standard modules or VBA modules. These contain the code that doesn’t go in any of the other three module types.
Forms: These are modules that have a form user-interface. Chapter 16 provides an introduction to UserForms.
Class Modules: These are modules where you create your own objects. Class modules are beyond the scope of this book.
You can expand a project by clicking the plus sign (+) at the left of the project’s name in the Project Explorer. Collapse a project by clicking the minus sign (–) to the left of a project’s name. Or you can double-click a project’s name to expand and collapse it.
If a project is password-protected, the VBE prompts for its password when you double-click the project’s name. If you don’t know the password, you can’t expand the project — which means that you can’t view or modify any part of the project.Figure 3-2 shows a Project Explorer with four projects listed: an add-in named UIHelpers.xlam, a workbook named Book1, a workbook named NumbersToWords.xlsm, and the Personal Macro Workbook (which is always named PERSONAL.XLSB). Of the four, only the NumbersToWords.xlsm project is expanded to show all of its modules.