Solved

Run an Excel macro by clicking a cell

Posted on 2014-01-07
2
4,234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

717 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