Custom events

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is in Sheet1:

Code:
Private WithEvents rng As clsRange

Private Sub rng_CellSelect(cell As Range)

rng.Color = 4

If rng.Color < 1 Or rng.Color > 56 Then
MsgBox "Error! Enter a value for ColorIndex between 1 and 56"
Exit Sub
End If

rng.Name = "FirstCell"

rng.methodColor

Dim i As Integer
i = rng.Color

rng.selectedRange.Select
Selection.Offset(0, 1).Value = "Cell Name: " & rng.Name
Selection.Offset(0, 2).Value = "Cell Address: " & Selection.Address
Selection.Offset(0, 3).Value = "Cell Interior ColorIndex: " & i
Selection.Offset(0, 4).Value = "Cell Content: " & Selection.Value

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = Range("A1").Address Then Call Module1.gr(Target)

On Error GoTo ErrorHandler  'Enable error-handling routine for any run-time error

Set rng = New clsRange

If Target.Address = Range("A1").Address Then
Set rng.selectedRange = Target
Else
Exit Sub
End If

ErrorHandler:
  Application.EnableEvents = True  'EnableEvents is changed back to True on any error

End Sub


This is in ClsRange:

Code:
Private varRng As Range
Private intColor As Integer
Private strName As String

Public Event CellSelect(cell As Range)

Public Property Set selectedRange(objRng As Range)

Set varRng = objRng

RaiseEvent CellSelect(varRng)
   
End Property

Public Property Get selectedRange() As Range

Set selectedRange = varRng

End Property

Property Let Name(nm As String)

strName = nm

End Property

Property Get Name() As String

Name = strName

End Property

Property Let Color(clr As Integer)

intColor = clr

End Property

Property Get Color() As Integer

Color = intColor

End Property

Sub methodColor()

selectedRange.Interior.ColorIndex = Color

End Sub

When the contents of cell A1 is changed, the event kicks off.

What I don't understand is why go through all this when you could easily just put the code in Sheet1, for example:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = Range("A1").Address Then

    ' do something

End If

End Sub

and not need RaiseEvent, Event and WithEvents?

Thanks
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Your class can work with any range at all. If you put the code in a worksheet, it's specific to that sheet. It's all about encapsulation. I suspect that this is also more of an example than something that is necessarily that practical.
 
Upvote 0
Your class can work with any range at all. If you put the code in a worksheet, it's specific to that sheet. It's all about encapsulation. I suspect that this is also more of an example than something that is necessarily that practical.
"Your class can work with any range at all. If you put the code in a worksheet, it's specific to that sheet. It's all about encapsulation. "

A bit like application level events, when you want all workbooks to trigger printing?
 
Upvote 0
Vaguely. It allows you to effectively monitor ranges without adding code to the target workbook.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top