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

Monitoring a specific range for changes<br />

The Change event occurs when any cell on the worksheet changes. In most cases, you’ll only be concerned<br />

with changes that are made to a specific cell or range. When the Worksheet_Change event-handler procedure<br />

is called, it receives a Range object as its argument. This Range object corresponds to the cell or<br />

cells that changed.<br />

Assume that your worksheet has a range named InputRange, and you want to monitor changes to this<br />

range only. No Change event exists for a Range object, but you can perform a quick check within the<br />

Worksheet_Change procedure. The following procedure demonstrates this:<br />

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

Dim VRange As Range<br />

Set VRange = Range(“InputRange”)<br />

If Union(Target, VRange).Address = VRange.Address Then<br />

Msgbox “The changed cell is in the input range.”<br />

End if<br />

End Sub<br />

This example creates a Range object variable named VRange, which represents the worksheet range that<br />

you want to monitor for changes. The procedure uses VBA’s Union function to determine whether VRange<br />

contains the Target range (passed to the procedure in its argument). The Union function returns an<br />

object that consists of all the cells in both of its arguments. If the range address is the same as the VRange<br />

address, Vrange contains Target, and a message box appears. Otherwise, the procedure ends, and nothing<br />

happens.<br />

The preceding procedure has a flaw. Target may consist of a single cell or a range. For example, if the user<br />

changes more than one cell at a time, Target becomes a multicell range. Therefore, the procedure requires<br />

modification to loop through all the cells in Target. The following procedure checks each changed cell<br />

and displays a message box if the cell is within the desired range:<br />

ON the CD-ROM<br />

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

Set VRange = Range(“InputRange”)<br />

For Each cell In Target<br />

If Union(cell, VRange).Address = VRange.Address Then<br />

Msgbox “The changed cell is in the input range.”<br />

End if<br />

Next cell<br />

End Sub<br />

A workbook with this example is available on the CD-ROM the file is named monitor a<br />

range.xlsm.<br />

Using the SelectionChange event<br />

The following procedure demonstrates the SelectionChange event. It executes whenever the user makes<br />

a new selection on the worksheet.<br />

Private Sub Worksheet_SelectionChange(ByVal Target _<br />

As Excel.Range)<br />

Cells.Interior.ColorIndex = xlNone<br />

With ActiveCell<br />

.EntireRow.Interior.ColorIndex = 35<br />

752

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

Saved successfully!

Ooh no, something went wrong!