23. VBA Tips - Double Click

Nick's picture


Here's a VBA tip on how to capture the Double Click event and do something useful with it.

  • A Double Click event is simply an event that fires when you double click on a cell
  • This example will demonstrate how to provide a simple interface to cycle through a series of values that enables a user of your Excel system to avoid entering things
  • The advantage is that is is very quick and simple for a user

We have this:

double-click

We double click once, and get this:

double-click

We double click again, and get this:

double-click

We double click a third time, and go back to where we started.

Here's the code we'll use:

double-click

 Explanation

  1. Run the StartDoubleClick procedure to start trapping double click events
  2. Run StopDoubleClick to stop
  3. MyDoubleClickMacro checks to see if you have double clicked on the cell we are interested in (C7).. if not, it does nothing
  4. If you have double clicked on C7, it looks at the value of C7
  5. If it's "Red", it changes it to green
  6. If "Green", it changes to blue
  7. In all other cases, it goes to red.

Note: Trapping double click has it's drawbacks as well in that if you double click on a cell, it no longer goes to Edit mode.

This code can be extended to check you're double clicking on the correct sheet as well, or modified to cope with double clicking on various cells, and handling the behaviour differently.

Download sheet to practise how to trap a Double Click event in Excel

Training Video on how to trap a Double Click event in Excel:

AttachmentSize
double-click.xls850 KB

Help - Need to Add range of cells

Hi Nick,

I would like to add a range to this instead of one cell.

I cannot figure it out. The range is I6 to X781. I need each cell to have access to double click into a color.

I appreciate any help or insight.

Cheers,

Stacey

Help - Need to Add range of cells

Hi Nick,

I would like to add a range to this instead of one cell.

I cannot figure it out. The range is I6 to X781. I need each cell to have access to double click into a color.

I appreciate any help or insight.

Cheers,

Stacey

navigate through worksheet

Hi Nick,
I am trying to use your code in my workbook to navigate through my tab. By double click in certain range in my main worksheet with value same with my sheet/tab name it will select the tab. So far I play around with the code but fail, I am new with this vba thing by the way. Maybe somehelp here from the master :)

Does not work too well

I have some problems in clicking the Start/Stop buttons. Sometimes clicking works, sometimes it needs 2 or more tries. Maybe your method is not too compatible with Excel 2007 which we have here in the office.

It won't if you have already

It won't if you have already double clicked on that cell. If you double click on one cell and fire off the event trap you need to then double click on another to reliably fire it again. If you always want to Doubel Click the same cell then as part of your VB code simply make it select another cell before returning.

Nick's picture

clicking buttons

hi

It works fine for me in Office 2007.

To replicate what I did:

  1. Download sheet
  2. Enable macros
  3. Click the "Start Double Click trapping" button
  4. Double click on the cell containing the colour

Nick