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
538 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 28 February 2014
Writing Event Procedures allows you to create subroutines which run automatically in response to events in a workbook. This video teaches you how to access the events of an object and start writing event procedures for both workbooks and worksheets. You'll also see how to cancel certain events and how to disable events, as well as how to use the parameters of an event procedure.
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: | duggie |
When: | 14 Mar 21 at 12:44 |
Many thanks (sorry only about 2 years late)!
From: | Andrew G |
When: | 15 Mar 21 at 07:01 |
You're welcome, hope it was eventually useful!
From: | duggie |
When: | 14 Apr 21 at 12:33 |
Yes, it solves the headaches posed by my clients but unfortunately also raises others!
In the following example, there is only the Worksheet_Change Event, so I thought things ought to be simpler but alas, it seems to have confused me totally.
My worksheet contains a table (the Ctrl+T type) and I want to prevent users from changing / deleting the headings.
This is the code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Rows(1)) Is Nothing Then
Application.EnableEvents = False
On Error GoTo Errhandler:
Application.Undo
Application.OnUndo "", ""
End If
Errhandler:
Application.EnableEvents = True
End Sub
It works as expected but if a break point was put on this line:
If Not Intersect(Target, Me.Rows(1)) Is Nothing Then
then if you triggered the event by changing something in row 1, the code runs all the way to End Sub but then jumps back to the start and runs for a second time before exiting.
There is only one event (Worksheet_Change), as opposed to the problem in my previous post where it contained both the Selection_Change and Worksheet_change, so what is the reason for this odd behaviour?
I even tried your suggestion of adding a new boolean variable, like this:
Option Explicit
Dim DataChanged As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
DataChanged = True
If DataChanged Then
If Not Intersect(Target, Range("A1:C1")) Is Nothing Then
Application.EnableEvents = False
On Error GoTo Skip:
Application.Undo
Application.OnUndo "", ""
End If
DataChanged = False
End If
Skip:
Application.EnableEvents = True
End Sub
but it still ran twice.
From: | Andrew G |
When: | 19 Apr 21 at 14:37 |
Hi duggie!
I've tried but failed to replicate the behaviour you're describing - no matter which cells I change or how many it still triggers the even only once. I'm at a loss to explain why you're experiencing something different here, sorry!
From: | duggie |
When: | 04 May 19 at 19:31 |
Andrew,
Is something perculiare with the following? I tested it on Excel 2010.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.EnableEvents = False
Select Case Target.Column
Case 1
Me.Cells(Target.Row, 2).Select
Case 2
Me.Cells(Target.Row, 3).Select
Case 3
Me.Cells(Target.Row, 4).Select
Case 4
Me.Cells(Target.Row, 1).Select
End Select
.EnableEvents = True
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox "SelectionChange!"
End Sub
The first time you select a cell on the worksheet, it triggers the Worksheet_SelectionChange event and the message box appears. I expect that.
After clicking OK, if I change a value on the worksheet, the Worksheets_Change event kicks off (as expected) and runs to the End Sub.
However, what I did not expect is after the End Sub in Worksheet_Change has been run, it immediately jumps to Sub Worksheet_SelctionChange.
I thought that by adding Applkication.EnableEvents = False, it suppresses the event.
Do you see a similar result (preferably testing in Excel 2010)?
Thanks
From: | Andrew G |
When: | 06 May 19 at 11:16 |
Hi duggie,
Are you typing the value into a cell and then pressing Enter? If so, you're triggering two events: 1) the Change event triggered by changing the value; and 2) the SelectionChange event triggered by pressing Enter to move to the next cell.
Try pressing CTRL + Enter to enter the value into the cell without moving to another cell. You'll see that your code works as intended.
I hope that helps!
From: | duggie |
When: | 07 May 19 at 18:45 |
Andrew,
Thanks for the suggestion wich worked but alas I doubt very much my users will be entering data via the Ctrl key! (I'll get round it).
What got me puzzled was I thought adding Application.EnableEvents stopped events kicking off. As can be seen in my code, that is set to False when a cell is selected, so the event wasn't triggered immediately but as soon as it was switched back on, the SelectionChange event was started.
From: | Andrew G |
When: | 08 May 19 at 07:43 |
Hi duggie,
Yes it's not immediately obvious what's going on but here's my basic breakdown:
1. You type in some data and press Enter which triggers the Change event.
2. Within the Change event procedure you disable events and select another cell while events are disabled meaning that no new events are triggered.
3. At the end of the Change event procedure you enable events.
4. After your Change event procedure has finished, the SelectionChange event associated with pressing Enter (or Tab or a cursor key) originally is still triggered.
Here's a very simple way to prevent the message box appearing after you've entered some data and pressed Enter (or Tab or a cursor key):
Option Explicit
Dim DataChanged As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
DataChanged = True
With Application
.EnableEvents = False
Select Case Target.Column
Case 1
Me.Cells(Target.Row, 2).Select
Case 2
Me.Cells(Target.Row, 3).Select
Case 3
Me.Cells(Target.Row, 4).Select
Case 4
Me.Cells(Target.Row, 1).Select
End Select
.EnableEvents = True
End With
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not DataChanged Then
MsgBox "SelectionChange!"
End If
DataChanged = False
End Sub
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.