Assign Macro to a Cell

  • Follow


Is there a way to assign a macro to a cell such that clicking (or double 
clicking) the cell executes the macro? 
Thanks,
    RUM

0
Reply Bob 12/14/2009 5:47:01 PM

You can pick up either a selction change

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.Row=1 and Target.Column=1 then
    'do Stuff
  End If
End Sub

a double click
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, _
        ByVal Target As Range, ByVal Cancel As Boolean)
  If Target.Row=1 and Target.Column=1 then
    'do Stuff
  End If
End Sub

or a right click
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, _
        ByVal Target As Range, ByVal Cancel As Boolean)
  If Target.Row=1 and Target.Column=1 then
    'do Stuff
  End If
End Sub

The code would go in VBA on the sheet where your target cell resides and not 
a seperate module.  Change the Target.Row=1 and Target.Column=1 to whatever 
cell you desire.
-- 
If this helps, please remember to click yes.


"Bob Myers" wrote:

> Is there a way to assign a macro to a cell such that clicking (or double 
> clicking) the cell executes the macro? 
> Thanks,
>     RUM
> 
> .
> 
0
Reply Utf 12/14/2009 6:44:01 PM


You would probably learn a heck of a lot if you read this:
http://www.cpearson.com/excel/Events.aspx


-- 
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Paul C" wrote:

> You can pick up either a selction change
> 
> Private Sub Worksheet_SelectionChange(ByVal Target As Range)
>   If Target.Row=1 and Target.Column=1 then
>     'do Stuff
>   End If
> End Sub
> 
> a double click
> Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, _
>         ByVal Target As Range, ByVal Cancel As Boolean)
>   If Target.Row=1 and Target.Column=1 then
>     'do Stuff
>   End If
> End Sub
> 
> or a right click
> Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, _
>         ByVal Target As Range, ByVal Cancel As Boolean)
>   If Target.Row=1 and Target.Column=1 then
>     'do Stuff
>   End If
> End Sub
> 
> The code would go in VBA on the sheet where your target cell resides and not 
> a seperate module.  Change the Target.Row=1 and Target.Column=1 to whatever 
> cell you desire.
> -- 
> If this helps, please remember to click yes.
> 
> 
> "Bob Myers" wrote:
> 
> > Is there a way to assign a macro to a cell such that clicking (or double 
> > clicking) the cell executes the macro? 
> > Thanks,
> >     RUM
> > 
> > .
> > 
0
Reply Utf 12/14/2009 7:35:01 PM

2 Replies
1629 Views

(page loaded in 0.051 seconds)

Similiar Articles:
















7/24/2012 9:09:26 AM


Reply: