Written by Allen Wyatt (last updated January 13, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12514) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Mouse Click Event in VBA.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
A common part of working with text strings in a worksheet is normalizing those strings so that they follow whatever rules ...
Discover MoreAs you format your worksheet, Excel allows you to add page breaks where you'd like. If you want to put in a series of ...
Discover MoreYou can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2018-01-27 11:30:19
JonM
One way I've used to accomplish this is to use the Worksheet_BeforeRightClick event.
Quick-and-dirty 'air-code' below demonstrates.
This needs to be in a worksheet module.
In this case, checks that right-clicked cell is in Column 6 (or "F").
If it is, then enters "C" into cell and moves selection down one row.
Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count > 1 Or Target.HasFormula Then Exit Sub
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Select Case Target.Column '(or address)
Case 6 '(or whatever column, row or cell you need)
If Target = "" Then
Target = "C"
Cells(Target.Row + 1, Target.Column).Select
Cancel = True
Else
Target.ClearContents
Cancel = True
End If
Case Else
Cancel = False
End Select
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments