Linking a range in a class to a cell, with change notification
Luca wrote:
I cannot figure out how to raise a change
event to trigger the needed actions.
Here's a suggestion:
' ---<Code in class module named Class1---
Option Explicit
Private WithEvents m_Worksheet As Excel.Worksheet
Private m_Range As Excel.Range
Public Property Set Range(ByVal RHS As Excel.Range)
' validation e.g.
If RHS Is Nothing Then
Err.Raise _
vbObjectError + 1, TypeName(Me), _
"Invalid Range object."
End If
If RHS.Rows.Count 1 Or RHS.Columns.Count 1 Then
Err.Raise _
vbObjectError + 2, TypeName(Me), _
"Must be single cell Range object."
End If
Set m_Range = RHS
Set m_Worksheet = m_Range.Worksheet
End Property
Public Property Get Range() As Excel.Range
Set Range = m_Range
End Property
Private Sub m_Worksheet_Change( _
ByVal Target As Range _
)
If m_Range Is Nothing Then
Exit Sub
End If
Dim oIntersection As Excel.Range
Set oIntersection = _
Excel.Application.Intersect(Target, m_Range)
If oIntersection Is Nothing Then
Exit Sub
End If
MsgBox "TODO: take action here"
End Sub
' ---<Code in class module named Class1---
' ---<Code in ThisWorkbookcode module---
Option Explicit
Private m_Class1 As Class1
Private Sub Workbook_Open()
Set m_Class1 = New Class1
Set m_Class1.Range = Sheet1.Range("B2")
' Trigger event in class
Sheet1.Range("B2").Value = 0
End Sub
' ---</Code in ThisWorkbookcode module---
Jamie.
--
|