18.11.2014 Views

Microsoft Office

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Part VI<br />

Programming Excel with VBA<br />

The edited macro appears as follows:<br />

Sub MyName()<br />

‘<br />

‘ MyName Macro<br />

‘<br />

‘ Keyboard Shortcut: Ctrl+Shift+N<br />

‘<br />

ActiveCell.FormulaR1C1 = “John Walkenbach”<br />

ActiveCell.Font.Bold = True<br />

End Sub<br />

Test this new macro, and you see that it performs as it should.<br />

Another example<br />

This example demonstrates how to record a time-stamp macro that inserts the current date and time into<br />

the active cell. To create the macro, follow these steps:<br />

1. Activate an empty cell.<br />

2. Choose Developer ➪ Code ➪ Record Macro. Excel displays the Record Macro dialog box.<br />

3. Enter a new single-word name for the macro, to replace the default Macro1 name. A good<br />

name is TimeStamp.<br />

4. Assign this macro to the shortcut key Ctrl+Shift+T by entering uppercase T in the edit box<br />

labeled Shortcut Key.<br />

5. Click OK to close the Record Macro dialog box.<br />

6. Enter this formula into the selected cell:<br />

=NOW()<br />

7. Click the Copy button (or press Ctrl+C) to copy the cell to the Clipboard.<br />

8. Choose Home ➪ Clipboard ➪ Paste Values. This step replaces the formula with static text, so<br />

the data and time do not update when the worksheet is calculated.<br />

9. Press Escape to cancel Copy mode.<br />

10. The macro is finished, so choose Developer ➪ Code ➪ Stop Recording (or click the Stop<br />

Recording button in the status bar).<br />

Examining the macro<br />

Activate the VB Editor and take a look at the recorded code. Figure 39.9 shows the recorded macro, as displayed<br />

in the Code window.<br />

690

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

Saved successfully!

Ooh no, something went wrong!