Raise Event

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The following code is taken from here:

Code:
http://www.cpearson.com/Excel/Events.aspx

This is in CExcelEvents:

Code:
Option Explicit

Public Event EventName(IDNumber As Long, ByRef Cancel As Boolean)

    Sub AAA()
        Dim B As Boolean
        Dim IDNumber As Long
        IDNumber = 1234
        Cancel = False
        RaiseEvent EventName(IDNumber, Cancel)

        If Cancel = False Then
            ' Appropriate Non-Cancel Action
        Else
            ' Appropriate Cancel Action
        End If
    End Sub

The article goes on as follows:

Code:
Once you have created your class with events, you need to write the code that will receive the event triggers. Note that only object modules (class modules, a user form code module, a Sheet module, or the ThisWorkbook module -- standard code modules cannot receive events) can receive event messages. In a suitable object module, declare the event class using WithEvents:

    Dim WithEvents XLEvents As CExcelEvents
    ' More code

At some point in your code, you will need to set the XLEvents variable to an instance of the CExcelEvents class, with the code:

    Set XLEvents = New CExcelEvents

Exactly when and where you put the object initialization code depends on what sort of module contains the event declaration. While it is technically possible to put the initialization of the variable in another procedure, this is generally a bad programming practice: it makes the code more difficult to debug and maintain. As a general rule, the code that initializes the events variable should be in the same class as the events variable. Of course, the actual event code must reside in the same object module as the events variable declaration. In a class module, the initialization would normally be in the Class_Initialize event. For a user form, the code would go in the UserForm_Initialize event.

    Private Sub XLEvents_EventName(IDNumber Long, Cancel As Boolean)
        Cancel = True ' or False -- your choice
    End Sub

Where should these lines go?

Code:
Dim WithEvents XLEvents As CExcelEvents

Set XLEvents = New CExcelEvents

Private Sub XLEvents_EventName(IDNumber Long, Cancel As Boolean)
        Cancel = True ' or False -- your choice
    End Sub

I tried putting them in a worksheet module, such as:

Code:
Option Explicit

Dim WithEvents XLEvents As CExcelEvents

Private Sub Worksheet_Change(ByVal Target As Range)
Set XLEvents = New CExcelEvents
End Sub

but it didn't even compile, instead displaying this error message:

Code:
Compile error variable not defined

Cancel = False


Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
In your first code snippet, Cancel has not been declared. You need to add
VBA Code:
Dim Cancel as Boolean
just below the line Sub AAA()
 
Upvote 0
In your first code snippet, Cancel has not been declared. You need to add
VBA Code:
Dim Cancel as Boolean
just below the line Sub AAA()

Thanks

Where should these lines go?

Code:
Dim WithEvents XLEvents As CExcelEvents

Set XLEvents = New CExcelEvents

Also, what exactly is the code supposed to do?
 
Upvote 0
Does the article not help?

I've read it but not understood 100% but I'm sure you can make more sense of it.
 
Upvote 0
I can certainly make sense of the article, I've written custom events myself in the past. I hardly ever have a need for them though.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,442
Members
449,083
Latest member
Ava19

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