Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
There are no files which go with this video.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | warmday |
When: | 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
From: | Andrew G |
When: | 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
From: | warmday |
When: | 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
From: | Andrew G |
When: | 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!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.