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
Solved

Run an Excel macro by clicking a cell

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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