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 />

Using Non-Object Events<br />

So far, the events discussed in this chapter are associated with an object (Application, Workbook,<br />

Sheet, and so on). This section discusses two additional events: OnTime and OnKey. These events are not<br />

associated with an object. Rather, you access them by using methods of the Application object.<br />

NOTE<br />

Unlike the other events discussed in this chapter, you use a general VBA module to program<br />

the “On” events in this section.<br />

Using the OnTime event<br />

The OnTime event occurs at a specified time. The following example demonstrates how to program Excel<br />

to beep and then display a message at 3 p.m.:<br />

Sub SetAlarm()<br />

Application.OnTime 0.625, “DisplayAlarm”<br />

End Sub<br />

Sub DisplayAlarm()<br />

Beep<br />

MsgBox “Wake up. It’s time for your afternoon break!”<br />

End Sub<br />

In this example, the SetAlarm procedure uses the OnTime method of the Application object to set up<br />

the OnTime event. This method takes two arguments: the time (0.625, or 3 p.m., in the example) and the<br />

procedure to execute when the time occurs (DisplayAlarm in the example). In the example, after<br />

SetAlarm executes, the DisplayAlarm procedure is called at 3 p.m., bringing up the message.<br />

Most people find it difficult to think of time in terms of Excel’s time numbering system. Therefore, you may<br />

want to use VBA’s TimeValue function to represent the time. TimeValue converts a string that looks like<br />

a time into a value that Excel can handle. The following statement shows an easier way to program an event<br />

for 3 p.m.:<br />

Application.OnTime TimeValue(“3:00:00 pm”), “DisplayAlarm”<br />

If you want to schedule an event that’s relative to the current time — for example, 20 minutes from now —<br />

you can write an instruction like this:<br />

Application.OnTime Now + TimeValue(“00:20:00”), “DisplayAlarm”<br />

You also can use the OnTime method to schedule a procedure on a particular day. Of course, you must<br />

keep your computer turned on, and Excel must be running.<br />

Using the OnKey event<br />

While you work, Excel constantly monitors what you type. As a result, you can set up a keystroke or a key<br />

combination that — when pressed — executes a particular procedure.<br />

The following example uses the OnKey method to set up an OnKey event. This event essentially reassigns<br />

the PgDn and PgUp keys. After the Setup_OnKey procedure executes, pressing PgDn executes the<br />

PgDn_Sub procedure, and pressing PgUp executes the PgUp_Sub procedure. The next effect is that pressing<br />

PgDn moves down one row, and pressing PgUp moves up one row.<br />

754

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

Saved successfully!

Ooh no, something went wrong!