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 1 thread Add post
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!