Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Mouse Click Event in VBA.

Mouse Click Event in VBA

Written by Allen Wyatt (last updated April 13, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Supriyo asked if there is a mouse event handler in VBA. He wants a value inserted in a cell when that cell is clicked on.

The standard way to do this is with the SelectionChange event. Every time the selection changes in the worksheet, the event is triggered. The event doesn't just trigger when a cell is clicked on, but also if someone presses a cursor control key that results in a different cell being selected.

As an example, let's say that you wanted cell B5 to contain the value 10 whenever that cell is selected. To implement that, you could use the following:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("B5")) Is Nothing Then _
      Range("B5").Value = 10
End Sub

This code is added to one of the sheet objects in the Project Explorer area of the VB Editor. Double-click the worksheet you want the event handler to apply to, and then add the macro to the resulting code window.

When the SelectionChange event is triggered, the target (the cell range being selected) is passed to the handler. The macro then checks to see if the target range contains cell B5, and if it does, stuffs the value 10 into cell B5. If you want to make sure that the macro only stuffs information into B5 if only B5 (the single cell) is selected, you can use this version of the macro:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = Range("B5").Address Then _
      Range("B5").Value = 10
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12514) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Mouse Click Event in VBA.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Formatting Endnote Reference Marks

The reference marks used for endnotes are, by default, formatted "good enough" for most people. If you are one of those ...

Discover More

Creating New Windows

If you need to look at different parts of the same worksheet at the same time, the answer is to create windows for your ...

Discover More

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come ...

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can ...

Discover More

Pulling Cell Names into VBA

Excel allows you to define names that can refer either to ranges of cells or to constant information, such as formulas. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

2024-04-14 15:40:06

J. Woolley

"Supriyo asked if there is a mouse event handler in VBA. He wants a value inserted in a cell when that cell is clicked on." But he didn't specify the type of mouse click. There are two worksheet mouse click event handlers: Double-click and Right-click.

When a cell is double-clicked using the left mouse button it normally initiates Enter/Edit mode for typing into the cell. An alternate method is to select the cell (or left-click it) and press F2 (or simply start typing). If Supriyo wants cell B5 to contain the value 10 whenever that cell is double-clicked, he can add the following code to the applicable Sheet module:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
    Cancel As Boolean)
    If Intersect(Target, Range("B5")) Is Nothing Then Exit Sub
    Range("B5").Value = 10
    Cancel = True 'do not initiate Enter/Edit mode
End Sub

Right-clicking a cell normally opens its context menu. An alternate method is to select the cell (or left-click it) and press the Menu key (usually right of the Space bar). If Supriyo wants cell B5 to contain the value 10 whenever that cell is right-clicked, he can add the following code to the applicable Sheet module:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
    Cancel As Boolean)
    If Intersect(Target, Range("B5")) Is Nothing Then Exit Sub
    Range("B5").Value = 10
    Cancel = True 'do not open the context menu
End Sub

It is not necessary to disable events when using these event handlers.


2024-04-13 09:40:08

Alex Blakenburg

Once you add Event Macros to your workbook, you will need to consider disabling and enabling Events when running macros eg without it in the case below the updating of B5 could very well trigger a Worksheet_Change event macro to run.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Range("B5").Address Then
Application.EnableEvents = False
Range("B5").Value = 10
Application.EnableEvents = True
End If
End Sub


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.