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.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Knowing if a workbook is already open can be a prerequisite to your macro working correctly. Here's how to check it out.
Discover MoreNamed ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...
Discover MoreWhen you need to stop a macro while it is running, you normally press Ctrl+Break. What are you to do if the keypress ...
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