562 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls 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
Event-handling macros in Excel Visual Basic
Part three of a five-part series of blogs
Want to do something when a user opens a workbook, or stop them clicking on a particular cell? You need to learn how to attach code to Excel workbook or worksheet events!
You can attach code to events for any worksheet in the same way as for a workbook.
Here's a quick summary of how to attach an event to a worksheet:
The steps for how to attach code to a particular worksheet are shown below.
The steps to follow are:
There are fewer events in a worksheet's life than a workbook's - does this mean they're less interesting?
The most useful events available for a worksheet are as follows:
|Change||This event fires whenever you change any cell's value|
|SelectionChange||Runs whenever you select a different cell or cells|
|BeforeDoubleClick||Whenever you double-click in the centre of a cell (rather than on the edge of it)|
|BeforeRightClick||Whenever you right-click in the centre of a cell (rather than on the edge of it)|
The rest of this page contains some examples of macros that you might write - as for the workbook examples, they are not meant to be taken seriously (although they do perfectly illustrate what is possible).
Suppose that you want to react to a user clicking on a particular cell. You can do this using the SelectionChange event:
We want to stop anyone clicking on the cell shown - and punish anyone who does!
The code to prevent anyone selecting the cell shown - or any range containing it - could look like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'if the range just select doesn't intersect
'with the broken one, that's fine
If Intersect(Target, Range("C2")) Is Nothing Then Exit Sub
'otherwise, vindictively delete all worksheet
MsgBox "It did warn you ..."
In this case, if you select any range containing C2 the nasty little macro will erase all of the cells in the current worksheet.
The really scary thing about macros like this one is that in Excel you can not undo the results of running a macro (although strangely you can in Word).
A common requirement is to do something when a user changes the value of a cell. In the example below, we prevent the input of odd numbers in a cell:
If a user types in an odd number, we want to react to it.
Some code to react to a user typing in an odd number could be:
Private Sub Worksheet_Change(ByVal Target As Range)
'if this is a single cell, and it's C2 ...
If Target.Cells.Count = 1 Then
If Target.Row = 2 And Target.Column = 3 Then
'don't allow odd numbers
If Target.Value Mod 2 = 1 Then
MsgBox "No odd numbers allowed"
Range("C2").Value = Target.Previous.Value
Here we check if the user has changed the value of a single cell in row 2, column 3 (we could also have checked if the cell's Address property had equalled $C$2, noting that this is case-sensitive).
You may now have reached this point in this online tutorial and be smugly thinking that your user can not now do anything untoward. However, users can bypass macros - read on!
|Parts of this blog|
25 Aytoun Street