18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part VI<br />

Programming Excel with VBA<br />

6. You’ll probably want to change the text that appears on the button to make it descriptive, so<br />

right-click the button, choose Edit Text from the shortcut menu, and make your changes.<br />

After performing these steps, clicking the button executes the assigned macro.<br />

FIGURE 39.11<br />

Adding a button to a worksheet so that it can be used to execute a macro.<br />

TIP<br />

You can also assign a macro to a button in your Quick Access Toolbar (QAT). Right-click your<br />

QAT and select Customize Quick Access Toolbar. In the Customize tab of the Excel Options<br />

dialog box, select Macros from the drop-down list on the left. Then select your macro and click the Add<br />

button. To change the icon, click the modify button.<br />

Writing VBA code<br />

As demonstrated in the preceding sections, the easiest way to create a simple macro is to record your<br />

actions. To develop more complex macros, however, you have to enter the VBA code manually — in other<br />

words, write a program. To save time, you can often combine recording with manual code entry.<br />

Before you can begin writing VBA code, you must have a good understanding of such topics as objects,<br />

properties, and methods. And it doesn’t hurt to be familiar with common programming constructs, such as<br />

looping and If-Then statements.<br />

This section is an introduction to VBA programming, which is essential if you want to write (rather than<br />

record) VBA macros. It isn’t intended to be a complete instructional guide. My book titled Excel 2007 Power<br />

Programming with VBA (Wiley Publishing, Inc.) covers all aspects of VBA and advanced spreadsheet application<br />

development.<br />

The basics: Entering and editing code<br />

Before you can enter code, you must insert a VBA module into the workbook. If the workbook already has a<br />

VBA module, you can use the existing module sheet for your new code.<br />

Use the following steps to insert a new VBA module:<br />

1. Press Alt+F11 to activate the VB Editor window. The VB Editor window is a separate application,<br />

although it works very closely with Excel.<br />

694

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!