Solved

Run an Excel macro by clicking a cell

Posted on 2014-01-07
2
3,479 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 80

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

708 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

13 Experts available now in Live!

Get 1:1 Help Now