Watch Object Example

The following routine prompts the user for a range, then loops through each cell in the range and adds it to the Watch window. It then displays the Watch window:

Sub AddWatches()

Dim oWatch As Watch

Dim rng As Range

Dim rngWatches As Range

'Prompt the user for a range

'Suppress the error if they cancel

On Error Resume Next

Set rngWatches = Application.InputBox( _

"Please select a cell or cell range to watch",

"Add Watch", , , , , , 8)

On Error GoTo 0

'If they selected a range

If Not rngWatches Is Nothing Then

'Loop through each cell and

' add it to the watch list

For Each rng In rngWatches

Application.Watches.Add rng

Next rng

End If

'View the watch window based on their answer

Application.CommandBars("Watch Window").Visible =

= (Not rngWatches Is Nothing)

End Sub

Was this article helpful?

0 0

Post a comment