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

Find and Replace Multiple Values

This post will guide you how to find and replace multiple values at once with VBA macro or using formula  in Excel. How do I make multiple find and replace in Excel. Suppose that you have a few cells containing ...

VBA Macro For VLOOKUP From Another Sheet

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

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

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)

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 ...

Sidebar