Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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.

Trigger or run a Macro by clicking a specific cell with VBA code


Combine multiple worksheets/workbooks into one worksheet / workbook:

Combine multiple worksheets or workbooks into one single worksheet or workbook may be a huge task in your daily work. But, if you have Kutools for Excel, its powerful utility – Combine can help you quickly combine multiple worksheets, workbooks into one worksheet or workbook.

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

Trigger or run a Macro by clicking a specific cell with VBA code


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

Notes:

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.


Related articles:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    M. Symonds · 4 months ago
    Thanks for this code. Is it possible to get this to work by clicking on a cell that is merged with others?
  • To post as a guest, your comment is unpublished.
    Shahrokh · 6 months ago
    Thanks, but how about several cells for clicking for running several macros.
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi,
      Try the below VBA code.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRgArr As Variant
      Dim xFunArr As Variant
      Dim xFNum As Integer
      Dim xStr As String
      Dim xRg As Range
      xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
      xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
      If Selection.Count = 1 Then
      For xFNum = 0 To UBound(xRgArr)
      Set xRg = ActiveSheet.Range(xRgArr(xFNum))
      If Not Intersect(Target, xRg) Is Nothing Then
      xStr = xFunArr(xFNum)
      Application.Run xStr
      End If
      Next
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    adam · 11 months ago
    Why not just use some newer version it will be more easy
    https://games.lol/racing/
  • To post as a guest, your comment is unpublished.
    Roger · 11 months ago
    Excel 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.
      crystal · 9 months ago
      Hi Roger,
      We haven't tested the code in Excel 2002(XP). Why not use the newer version of Microsoft Office? It will be more easy for your work.
  • To post as a guest, your comment is unpublished.
    James Clements · 1 years ago
    My 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
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      goalken · 4 months ago
      This topic is very interesting and I am interested but do not know where to find, thankfully you create this topic, hope everyone will help me http://run-3.online
  • To post as a guest, your comment is unpublished.
    James Clements · 1 years ago
    i'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...
    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi James,
      The code only works for Microsoft Office Excel. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Alber · 1 years ago
    Thanks but what about merged cells?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Alber,
      The code does not work for merged cells.
  • To post as a guest, your comment is unpublished.
    Phil H · 1 years ago
    Hi.

    I have this working well, but would like to add a condition to running the macro. I only want to run the macro if the cell alongside the cell I click into contains a certain value.
    e.g. When I click on cell F6, I want the macro to run if cell E6 contains "x", but if cell E6 is blank, the macro must not run.
    Hope that makes sense.
    Thanks

    here is my original code without the condition:


    Option Explicit

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Selection.Count = 1 Then
    If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
    Call datePick
    End If
    End If
    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi,
      The following VBA code can help you solve the problem. Please have a try and thank you for your comment.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      Dim xRg As Range
      If Not Intersect(Target, Range("F6:F18")) Is Nothing Then
      Set xRg = ActiveSheet.Cells(Target.Row, Target.Column - 1)
      If (xRg.Value = "") Or (xRg.Value <> "X") Then Exit Sub
      Call datepick
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Medical Coder · 1 years ago
    Great. Its working well... Thank you....
  • To post as a guest, your comment is unpublished.
    Wayne · 2 years ago
    This worked perfectly and will save me a load of time - thank you for sharing your knowledge - much appreciated!
  • To post as a guest, your comment is unpublished.
    Simon Jones · 2 years ago
    If you want to have multiple macros, cells running different macros on the same page - is that possible?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Simon,
      The below VBA script can help you to run different macros by clicking on cells on the same page.

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If Target.Count = 1 Then
      If Not Intersect(Target, Range("D4")) Is Nothing Then Call MyMacro1
      If Not Intersect(Target, Range("D8")) Is Nothing Then Call MyMacro2
      If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro3
      End If
      End Sub

      Please add line "If Not Intersect(Target, Range("D10")) Is Nothing Then Call MyMacro" to run more macro by clicking cell. And change the cell and macro names in the code based on your needs.
      • To post as a guest, your comment is unpublished.
        Camila · 10 months ago
        It isn't working on my Excel. The code is correct?
        • To post as a guest, your comment is unpublished.
          crystal · 4 months ago
          Hi Camila,
          Sorry for the inconvenience. Try the below VBA code.

          Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          Dim xRgArr As Variant
          Dim xFunArr As Variant
          Dim xFNum As Integer
          Dim xStr As String
          Dim xRg As Range
          xRgArr = Array("A1", "D1", "C1") 'Cells used to trigger macro
          xFunArr = Array("Code name1", "Code name2", "Code name3") 'The corresponding code names
          If Selection.Count = 1 Then
          For xFNum = 0 To UBound(xRgArr)
          Set xRg = ActiveSheet.Range(xRgArr(xFNum))
          If Not Intersect(Target, xRg) Is Nothing Then
          xStr = xFunArr(xFNum)
          Application.Run xStr
          End If
          Next
          End If
          End Sub
  • To post as a guest, your comment is unpublished.
    Matt · 2 years ago
    Trigger Or Run A Macro By Clicking A Specific Cell With VBA Code, couldn't get this to work. I tried it many different way, but it never acheived what was promised.
    • To post as a guest, your comment is unpublished.
      Peter · 2 years ago
      This code will only work if you place it in the "ThisWorkbook" module. It does not work on a general module.
      workbookname > Microsft Excel Objects > ThisWorkbook.
      • To post as a guest, your comment is unpublished.
        PritishS · 2 years ago
        This code will work on worksheet code module. You need to right click on sheet tab and click on 'View Code'. This will open code module for that worksheet only. Then paste the mentioned code.