Excel VBA - Basics videos | Excel VBA Part 20 - Event Procedures

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.

There are no files which go with this video.

Making a  video bigger

You can increase the size of your video to make it fill the screen like this:

View full screen

Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown at its bottom right-hand corner.

 

When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.

Improving the quality of a video

To improve the quality of a video, first click on the Settings icon:

Settings icon

Make sure you're playing your video so that the icons shown appear, then click on this gear icon at the bottom right-hand corner.

 

Choose to change the video quality:

Video quality

Click on Quality as shown to bring up the submenu.

 

The higher the number you choose, the better will be your video quality (but the slower the connection speed):

Connection speed

Don't choose the HD option unless you have a fast enough connection speed 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.

This page has 2 threads Add post
14 Mar 21 at 12:44

Many thanks (sorry only about 2 years late)!

 

 

15 Mar 21 at 07:01

You're welcome, hope it was eventually useful!

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.

Andrew G  
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!

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

 

 

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!

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.

 

Andrew G  
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