Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind macro to excel cell onclick?

Tags:

excel

events

vba

In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?

like image 449
Cheekysoft Avatar asked Jan 24 '23 04:01

Cheekysoft


2 Answers

Found a solution:

Make a named range for the cells you want to capture clickevent

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.EnableEvents = False
    Application.ActiveSheet.Cells(1, 1).Value = Target.Address
    If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then 
        ' do your stuff
        Range("A1").Select
    Endif
    Application.EnableEvents = True
End Sub
like image 161
Arjen Avatar answered Jan 31 '23 15:01

Arjen


You can bind to a cell double click.

Open VBA, goto the worksheet you want to wire up the event Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right The check the Target.Address is equal to the address of the cell you care about and call the function you wish.

Something like this:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Address(True, True, xlA1) = "$A$1" Then
        Call MyDemo
    End If
End Sub

Private Sub MyDemo()
    MsgBox "Hello"
End Sub
like image 38
JDunkerley Avatar answered Jan 31 '23 15:01

JDunkerley