Solved

Run an Excel macro by clicking a cell

Posted on 2014-01-07
2
4,034 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

685 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