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
551 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 ...
Written by Andy Brown
In this tutorial
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:
Double-click on the worksheet in question.
Click on the drop arrow next to General and choose Worksheet, the only object available in the list.
Click on the right-hand arrow and choose the event you want to attach code to.
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:
Event | Use |
---|---|
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
Cells.Clear
MsgBox "It did warn you ..."
End Sub
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
End If
End If
End If
End Sub
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).
It is difficult in VBA to reset a cell's value back to the previous value for a macro like this - some fairly advanced ideas for how to do this can be found here.
You can learn more about this topic on the following Wise Owl courses:
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.