How to Trigger A Macro by Clicking A Specific Cell in Excel

Sometimes we create a Macro but we don’t want trigger it by Run Macro via Developer->Run Macro or other keyboard shortcuts. We may set the trigger by clicking a specific cell like click A1 or E1 for example. Once we clicking on the specific cell A1 or E1, the Macro will run properly, it looks verify convenient. So, in this article we will help you to trigger a macro by clicking cell via VBA.

First, create a Macro “DeleteAllCharts” which is used for remove all charts on worksheet.

We create a table with two different type of charts. See screenshot below.

Trigger A Macro by Clicking A Specific Cell 1

Edit VBA script to remove all charts for current worksheet, see VBA script below. The Macro name is “DeleteAllCharts”. Let’s started to learn how to trigger a macro by a specific cell now.

Trigger A Macro by Clicking A Specific Cell 2

Trigger A Macro by Clicking A Specific Cell in Excel


Step 1: Right click on Sheet1 to load Sheet management menu. Select View Code, Microsoft Visual Basic for Applications window pops up.

Or you can enter Microsoft Visual Basic for Applications window via Developer->Visual Basic.

Trigger A Macro by Clicking A Specific Cell 3

Step 2: In Microsoft Visual Basic for Applications window, click on Sheet1, enter below code:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Selection.Count = 1 Then

        If Not Intersect(Target, Range("D2")) Is Nothing Then

            Call DeleteAllCharts

        End If

    End If

End Sub

 

 

Comment:

In above script, D2 is the cell for trigger the Macro, DeleteAllCharts is Macro name, you can replace above two parameters by your own parameters. In this case, if we click on D2, DeleteAllCharts will be executed, all charts will be removed.

Step 3: Save the codes, see screenshot below. And then quit Microsoft Visual Basic for Applications.

Trigger A Macro by Clicking A Specific Cell 4

Step 4: Click on D2. Verify that all charts are removed properly. That means macro is triggered by cell D2 properly. You can add some descriptions in D2 to do demonstration, you can also leave D2 as blank, it also works normally if you click on cell D2 without any value entered.

Trigger A Macro by Clicking A Specific Cell 5

Related Posts

VBA Macro For VLOOKUP From Another Sheet
vba macro for vlookup from another sheet1

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

How To Insert Comments in Protected Worksheet in Excel
insert commetns in protected workshee3

This post will show you how to allow comments in a protected worksheet in Excel. You can easily to insert comments into cells in a normal worksheet in Excel, but if want to insert a comment in a worksheet that ...

How To Convert Text to Upper Cases(Using VBA) in Excel
convert text to upper cases1

This post will show you how to switch from lower case to upper case in Excel. and I am going to show you two different ways of converting text to upper cases using formula or VBA macro in Excel 2013,Excel ...

How To Hide Every Other Row in Excel (Using VBA)
hide every other row1

This post will show you how to hide alternate rows or columns in Excel or how to hide every third, fourth, fifth row or column in Excel. If you want to hide every other row in your current worksheet, how ...

How to Disable the Save As Prompt in Excel
disable save as prompt1

This post will show you how to use a VBA Macro to save an Excel file and overwrite any existing file without a prompt so that you are going to get the little window that says file already exists do ...

How to Count Cells that Contain even or odd numbers in Excel
count cells that contain even numbers5

This post will guide you how to count the number of cells that contain odd or even numbers within a range of cells using a formula in Excel 2013/2016.How do I count cells that contain odd numbers through the use ...

How to Count Cells that Contain negative Numbers in Excel

This post will guide you how to count the number of cells that contain negative numbers within a range of cells using a formula in Excel 2013/2016.You can count the number of negative numbers in your data using easy functions ...

How to Count Cells Are Not Blank or Empty in Excel
count non blank nonempty cells5

This post will guide you how to count cells that are not blank or empty in a given range cells using a formula in Excel 2013/2016.How do I count the number of cells that are not blank in a particular ...

How to Count Cells Not equal to X or Y in Excel
count cells not equals to x or y2

This post will guide you how to count the number of cells not equal to criteria X or Y in a given range cells using a formula in Excel 2013/2016.You can easily to count cells equal to or not equal ...

How to Count Cells Less Than a Specific Value in Excel
count cells lessr than5

This post will guide you how to count the number of cells less than a particular numeric value in a given range cells using a formula in Excel 2013/2016. How do I count cells that are less than a specific ...

Sidebar