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!

  1. Introduction to Handling Events in Excel VBA Code
  2. Workbook Events
  3. Events for a Particular Worksheet (this blog)
  4. Bypassing Macros
  5. Considerations for Other MS Office Application Events

This series of blogs is part of our Excel VBA online tutorial.  If you want to learn more, have a look at our classroom-based courses in VBA macros and/or Excel.

Posted by Andy Brown on 22 November 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Events for a Particular Worksheet

You can attach code to events for any worksheet in the same way as for a workbook.

Attaching Events to a Worksheet

Here's a quick summary of how to attach an event to a worksheet:

Attaching code to worksheet

The steps for how to attach code to a particular worksheet are shown below.

The steps to follow are:

  1. Double-click on the worksheet in question.
  2. Click on the drop arrow next to General and choose Worksheet, the only object available in the list.
  3. 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 Main Worksheet Events Available

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).

Preventing a User Selecting a Cell

Suppose that you want to react to a user clicking on a particular cell.  You can do this using the SelectionChange event:

Diagram showing broken cell

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).

Reacting to a Change of Value for a Cell

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:

Input cell for even number

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 and here.

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!

 

This blog has 0 threads Add post