Excel Guides

Mouse Click Event in VBA in Excel

When you click on a cell in Excel, there are a number of events that are fired off. One of these events is the MouseClick event. You can use this event to run a macro when a user clicks on a cell. This can be useful if you want to perform some action when a user clicks on a cell, such as opening a file or running a macro.

To use the MouseClick event, you first need to create a macro. To do this, open the Visual Basic Editor (VBE) by pressing Alt+F11. Then, in the left pane, double-click on the sheet where you want to add the code. This will open the code window for that sheet. Paste the following code into the code window:

Private Sub Worksheet_MouseClick(ByVal Target As Range)

    'Your code here
    
End Sub

This code will run any time a user clicks on the worksheet. The Target variable contains information about which cell was clicked. You can use this variable to determine which cell was clicked and take action accordingly.

For example, let's say you want to open a file when a user clicks on cell A1. You would add the following code to the MouseClick event:

Private Sub Worksheet_MouseClick(ByVal Target As Range)

    If Target.Address = "$A$1" Then  'Check if cell A1 was clicked
        Application.FollowHyperlink "C:\myfile.xlsx"  'Open the file
    End If    
    
End Sub

Move beyond 

Excel

Get started with Causal today.
Build models effortlessly, connect them directly to your data, and share them with interactive dashboards and beautiful visuals.