Solved

Run an Excel macro by clicking a cell

Posted on 2014-01-07
2
3,643 Views
Last Modified: 2014-01-09
Hello,

In Excel (2013), is there a way to trigger a macro by clicking a cell rather than by using a key combination or clicking an object or image?

As far as I know, Excel macros can be initiated only by a key combination (hotkey) or by defining an object (or image) as the trigger.

What I'm wondering is if, instead of the above, a cell itself can be defined as the trigger for a macro so the macro can be launched by clicking the cell — in the same way an hyperlink can be activated by clicking the cell.

Thanks
0
Comment
Question by:Steve_Brady
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39764240
The Worksheet_SelectionChange event macro runs whenever the user selects a cell on the worksheet. You can use this event sub to trap the selection of a specific cell.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cel As Range, targ As Range
If Target.Cells.Count > 1 Then Exit Sub

Set targ = Range("A1:A5")   'Watch these cells for user selections
Set targ = Intersect(targ, Target)
If targ Is Nothing Then Exit Sub

Select Case Target.Address
Case "$A$1", "$A$3"
    Macro1
Case "$A$2"
    Macro2
Case Else
    Macro3
End Select

End Sub

Open in new window

UsingCellSelectionToTriggerAMacr.xlsm
0
 

Author Closing Comment

by:Steve_Brady
ID: 39769731
That's perfect Brad — and just what I was after. Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now