How to call a sub defined in macro when double click a cell?

G

Guest

Dear all,

I would like to know how to write the codes if I want to call a sub defined
in macro when double clicking a cell on the worksheet. I know that I can add
buttons on the worksheet, but I wonder if I can do the same thing by just
double clicking a cell.

I tried to record the macro, I got the following codes:

Range("A1").Select
ActiveCell.FormulaR1C1 = ""

Can anyone advise? Thanks a lot!

Ivan
 
R

Rowan Drummond

Say you want to run a macro called theMacro every time you double click
in cell A1. To do this you would use the sheet event BeforeDoubleClick.
Right click the sheet tab, select view code and paste the following
event code:

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

Hope this helps
Rowan
 
G

Guest

Dear Rowan,

Thanks a lot! It helps!

Ivan

Rowan Drummond said:
Say you want to run a macro called theMacro every time you double click
in cell A1. To do this you would use the sheet event BeforeDoubleClick.
Right click the sheet tab, select view code and paste the following
event code:

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

Hope this helps
Rowan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top