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.

Introducing Visual Basic for Applications 39<br />

What’s New in the Visual Basic Editor?<br />

E<br />

xcel 2007 sports a brand new interface, but what about the Visual Basic Editor? If you used the Visual Basic<br />

Editor in a previous version of Excel, you’ll be in familiar territory. In <strong>Office</strong> 2007, <strong>Microsoft</strong> left the VB<br />

Editor essentially untouched. The VBA language has been updated to accommodate the new Excel features,<br />

but the VB Editor has no new features, and the toolbars and menus work exactly like they always have.<br />

ON the CD-ROM<br />

A workbook that contains this macro is available on the companion CD-ROM. The file is<br />

named current date.xlsm.<br />

Sub procedures always start with the keyword Sub, the macro’s name (every macro must have a unique<br />

name), and then a pair of parentheses. (The parentheses are required; they’re empty unless the procedure<br />

uses one or more arguments.) The End Sub statement signals the end of the procedure. The lines in<br />

between comprise the procedure’s code.<br />

The macro shown in Figure 39.5 also includes a comment. Comments are simply notes to yourself, and<br />

they’re ignored by VBA. A comment line begins with an apostrophe. You can also put a comment after a<br />

statement. In other words, when VBA encounters an apostrophe, it ignores the rest of the text in the line.<br />

You execute a VBA Sub procedure in any of the following ways:<br />

n<br />

n<br />

Choose Developer ➪ Code ➪ Macros to display the Macro dialog box. Then select the procedure’s<br />

name from the list and click Run.<br />

You can also access the Macro dialog box by pressing Alt+F8 or by clicking the Play Macro button<br />

in the left part of Excel’s status bar.<br />

Press the procedure’s shortcut key combination (if it has one).<br />

n If the Visual Basic Editor is active, move the cursor anywhere within the code and press F5.<br />

n Refer to the procedure in another VBA procedure.<br />

VBA functions<br />

The second type of VBA procedure is a function. A function always returns a single value (just as a worksheet<br />

function always returns a single value). A VBA function can be executed by other VBA procedures or<br />

used in worksheet formulas, just as you would use Excel’s built-in worksheet functions.<br />

Figure 39.6 shows the listing of a custom worksheet function and shows the function in use in a worksheet.<br />

This function is named CubeRoot, and it requires a single argument. CubeRoot calculates the cube root<br />

of its argument. A Function procedure looks much like a Sub procedure. Notice, however, that function<br />

procedures begin with the keyword Function and end with an End Function statement.<br />

ON the CD-ROM<br />

A workbook that contains this function is available on the companion CD-ROM. The file is<br />

named cube root .xlsm.<br />

685

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

Saved successfully!

Ooh no, something went wrong!