How to trigger or run a Macro by clicking a specific cell in Excel?
While working with Microsoft Excel, you may know how to run a certain Macro with a Command Button. But do you know how to run a Macro by just clicking on a specific cell in a worksheet? This article will show you method of triggering a Macro by clicking on a specific cell in details.
Excel Productivity Tools
Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial
Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial
The following VBA code can help you to run a Macro by clicking a specific cell in Excel. Please do as follows.
1. On the worksheet with the cell you need to click to run a Macro, right click the sheet tab, and then click View Code from the context menu.
2. In the Microsoft Visual Basic for Applications window, copy and paste the below VBA script into the Code window.
VBA code: Trigger or run a Macro by clicking a specific cell
Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Selection.Count = 1 Then If Not Intersect(Target, Range("D4")) Is Nothing Then Call MyMacro End If End If End Sub
1. In the code, D4 is the cell you will click to run Macro;
2. Please replace the code name MyMacro with the Macro you will run in the worksheet. See screenshot:
3. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.
From now on, when clicking on cell D4 in current worksheet, your specified Macro will be triggered immediately.
- How to pop up a calendar when clicking a specific cell in Excel?
- How to change cell color when cell is clicked or selected in Excel?
- How to pop up message box when clicking on a certain cell in Excel?
- How to disable button after clicking once in Excel?
- How to disable right click on sheet tab in Excel?
Excel Productivity Tools
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 5 days agoThanks, but how about several cells for clicking for running several macros.
To post as a guest, your comment is unpublished.· 4 months agoWhy not just use some newer version it will be more easy
To post as a guest, your comment is unpublished.· 4 months agoExcel 2002 (XP): If a workbook is opened by selecting "File" and pressing "Shift" it disables macros in that sheet, the problem is that if I select "Tools > Macro > Macros..." I can run the macro anyway, how to solve?
To post as a guest, your comment is unpublished.· 5 months agoMy full code in this macro is:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim val As String
REM val = Range("A2").Value
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D24")) Is Nothing Then
REM Call MyMacro
val = Range("D24").Value
Range("B27").Value = val
To post as a guest, your comment is unpublished.· 5 months agoi'm using OpenOffice and have right clicked on sheet-tab and selected events and then selected this macro from MyMacros.. However I get and error on the following line: If Selection.Count = 1 Then >>> "Basic runtime error, variable not defined...