Trouble with raising custom events...

cosmarchy

New Member
Joined
Nov 11, 2009
Messages
13
I'm having issues raising a custom event.

I have the following code in Class1 module:
VBA Code:
Option Explicit

Private WithEvents app As Application
Private rng As Range
Public Event DAD(OldRange As Range, NewRange As Range, Worksheet As Worksheet)

Private Sub app_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
    Dim r As Range
    
    If (rng Is Nothing) Then Exit Sub
    
    If (rng.Address <> Target.Address) Then
        If (rng.Count = Target.Count) Then
               
            RaiseEvent DAD(Target, rng, Sh)
          
        End If
    End If
    
End Sub

Private Sub app_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    
    Set rng = Target
    
End Sub

Private Sub Class_Initialize()

    Debug.Print "Class init"
    
    Set app = Application
    
End Sub

and the following in ThiwWorkbook module:

VBA Code:
Option Explicit

Private WithEvents DnD As Class1
Dim cls As New Class1

Private Sub DnD_DAD(OldRange As Range, NewRange As Range, Worksheet As Worksheet)
    
    Debug.Print "hello, summit should have occurred"
    
End Sub

Private Sub Workbook_Open()

    Set cls = New Class1
    
End Sub

The problem is that Private Sub DnD_DAD never fires. When opening the workbook, the class is initialised as 'Class Init' is displayed in the immediate window, so I know at least this part is working. Does anyone know why Private Sub DnD_DAD is not triggered when cells are moved?
I've set a breakpoint in Sub app_SheetChange and have stepped through to the RaiseEvent, so I also know that this actually being called too.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
This:

Code:
Set cls = New Class1

should be:

Code:
Set DnD = New Class1
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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