Cheekysoft
Cheekysoft

Reputation: 35580

Bind macro to excel cell onclick?

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

Upvotes: 4

Views: 16120

Answers (2)

Arjen
Arjen

Reputation: 66

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

Upvotes: 5

JDunkerley
JDunkerley

Reputation: 12495

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

Upvotes: 4

Related Questions