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.

Working with Excel Events 43<br />

TABLE 43.2<br />

Worksheet Events<br />

Event<br />

Activate<br />

BeforeDoubleClick<br />

BeforeRightClick<br />

Calculate<br />

Change<br />

Deactivate<br />

FollowHyperlink<br />

PivotTableUpdate<br />

SelectionChange<br />

Action That Triggers the Event<br />

The worksheet is activated.<br />

The worksheet is double-clicked.<br />

The worksheet is right-clicked.<br />

The worksheet is calculated (or recalculated).<br />

Cells on the worksheet are changed by the user.<br />

The worksheet is deactivated.<br />

A hyperlink on the worksheet is clicked.<br />

A PivotTable on the worksheet has been updated.<br />

The selection on the worksheet is changed.<br />

Using the Change event<br />

The Change event is triggered when any cell in the worksheet is changed by the user. The Change event is<br />

not triggered when a calculation generates a different value for a formula or when an object (such as a chart<br />

or SmartArt) is added to the sheet.<br />

When the Worksheet_Change procedure executes, it receives a Range object as its Target argument.<br />

This Range object corresponds to the changed cell or range that triggered the event. The following example<br />

displays a message box that shows the address of the Target range:<br />

Private Sub Worksheet_Change(ByVal Target As Excel.Range)<br />

MsgBox “Range “ & Target.Address & “ was changed.”<br />

End Sub<br />

To get a feel for the types of actions that generate the Change event for a worksheet, enter the preceding<br />

procedure into the code module for a Worksheet object. After entering this procedure, activate Excel and,<br />

using various techniques, make changes to the worksheet. Every time that the Change event occurs, a message<br />

box displays the address of the range that changed.<br />

Unfortunately, the Change event doesn’t always work as expected. For example:<br />

n<br />

n<br />

n<br />

n<br />

Changing the formatting of a cell does not trigger the Change event (as expected), but choosing<br />

Home ➪ Editing ➪ Clear ➪ Clear Formats does.<br />

Pressing Delete generates an event even if the cell is empty at the start.<br />

Cells changed via Excel commands may or may not trigger the Change event. For example, the<br />

following operations do not trigger the Change event: sorting and goal seeking. However, operations<br />

such as find and replace, using the AutoSum button, or adding a Totals row to a table do<br />

trigger the event.<br />

If your VBA procedure changes a cell, it does trigger the Change event.<br />

751

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

Saved successfully!

Ooh no, something went wrong!