VBA - advanced videos | Excel VBA Part 20a - Application Events

Posted by Andrew Gould on 26 November 2015

In an earlier video in the series we've seen how to write event-handlers for workbooks and worksheets; this video teaches you how to access events of the Excel application itself! You'll learn the significance of the WithEvents keyword, a couple of the basic application events such as the new workbook event, and how to use a basic class module to keep your application event handlers neatly organised.

You can increase the size of the video:

Full screen mode for YouTube

You can view the video in full screen mode as shown on the left, using the icon at the bottom right of the frame.

You can also increase the quality of the video:

Changing resolution

You can improve the resolution of the video using another icon at the bottom right of the frame. This will slow down the connection speed, but increase the display and sound quality. This icon only becomes visible when you start playing the video.

Finally, if nothing happens when you play the video, check that you're not using IE in compatibility view.

This page has 2 threads Add post
22 Apr 20 at 11:15

I am trying to learn the mertis of creating your own events.

Can you please explain the use of the following code?

This is in Sheet1:

 

Option Explicit

    Dim WithEvents SomeClass As Class1
   
Private Sub SomeClass_ShowMessage(Message As String)
   
    MsgBox Message
   
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

    Set SomeClass = New Class1
   
    Call SomeClass.SomeMethod

End Sub

 

This is in Class1:

 

Option Explicit

    Public Event ShowMessage(Message As String)

Sub SomeMethod()

    RaiseEvent ShowMessage("Hi")

End Sub

 

My (limited!) understanding of this code is that when something changes on Sheet1, a messagebox displays the message "Hi".

 

But why go to all that length using a class module, as well as Raise Event, Event and WithEvents when you could simply write this in Sheet1:

 

Private Sub Worksheet_Change(ByVal Target As Range)

   MsgBox "Hi!"

End Sub

 

Thanks

 

 

22 Apr 20 at 16:48

The short answer is that you wouldn't!  The example you've given is one to demonstrate the basic principles of custom events.  In reality you'd be writing custom events which are meaningful in the context of the class in which they're defined.  A common, generic approach is to raise custom events both before and after something has happened in a class.  What that something is will be entirely dependent on what you're designing your class to do.

http://cpearson.com/excel/Events.aspx - scroll to the end for a short section on custom events.

https://rubberduckvba.wordpress.com/2019/03/27/everything-you-ever-wanted-to-know-about-events/ - this contains a link to a Battleships game which uses lots of advanced VBA techniques

 

08 Feb 20 at 21:16

The following is code form your video:

' ThisWorkbook

Option Explicit
   
    Private xlApp As EventApp
   
Private Sub Workbook_Open()

    Set xlApp = New EventApp
   
End Sub

 

' Class Module called EventApp

Option Explicit

    Private WithEvents xlApp As Application
   
Private Sub Class_Initialize()

    Set xlApp = Application
   
End Sub

Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)

    Wb.Worksheets("Sheet1").Range("A1").Value = "Created on " & Date
    Wb.Worksheets("Sheet1").Range("A2").Value = "Created by " & Environ("UserName")
   
End Sub

 

I have seen this from a book:

 

' Class Module called EventApp

    Public WithEvents xlApp As Application

Private Sub xlApp_NewWorkbook(ByVal Wb As Workbook)

    Wb.Worksheets("Sheet1").Range("A1").Value = "Created on " & Date
    Wb.Worksheets("Sheet1").Range("A2").Value = "Created by " & Environ("UserName")
   
End Sub

' Standard Module

    Public myAppEvent As New EventApp

Sub TrapAppEvent()

    Set myAppEvent.xlApp = Application

End Sub

 

Both versions work but there are some differences.

1. Your variables are declared Private whereas the other version is declared Public, eg Private xlApp As EventApp / Private WithEvents xlApp As Application

    The other version declares: Public WithEvents xlApp As Application / Public myAppEvent As New EventApp

 

2. The other version contains this line:

    Set myAppEvent.xlApp = Application

but yours doesn't.

Can you please explain the significance of these two points.

Thanks

 

25 Feb 20 at 08:42

Hi there,

1. Variables declared as Private are accessible only within the module in which they are declared.  Variables declared as Public can be referenced from any module in the same project. I declared xlApp as Private in both the class and normal modules so that the variable was specific to each module.

2. The same line appears in my code in the Initialize event of the class module - the Initialize event is triggered when a new instance of the class is created.

I hope that helps!