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 run macro by clicking hyperlinks in Excel?

Macro code may be very helpful for us to solve some complicated jobs in Excel, normally, we can execute it in the Microsoft Visual Basic for applications window by clicking Run button or pressing F5 key. But, have you ever tried to run the macros by clicking the relative hyperlinks in worksheet?

Run or execute the macro by clicking hyperlink with VBA code

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


arrow blue right bubble Run or execute the macro by clicking hyperlink with VBA code


It may be interesting for us to trigger a macro by clicking a hyperlink in Excel, the following method can help you to achieve this task step by step:

1. Create hyperlinks that you want to use to execute the macro codes. Please click a cell and create a hyperlink to link to itself, see screenshot:

doc run macro from hyperlink 1

2. Repeat the above step to create other hyperlinks you want to use as following screenshot shown:

doc run macro from hyperlink 2

3. Then right click the sheet tab which contains the hyperlinks you want to use, and then choose View Code from the context menu, and in the opened Microsoft Visual Basic for applications window, copy and paste the following code into the blank Module:

VBA code: Run macro codes from hyperlinks:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$B$6" Then
        Call Macro1
        End If
         If Target.Range.Address = "$B$8" Then
        Call Macro2
         End If
End Sub

doc run macro from hyperlink 3

Note: In the above code, B6 and B8 are the cells contain hyperlinks you want to use, and Macro1 and Macro2 are the macro code names, please change them to your need.

4. Then save and close the code window, from now on, when you click the hyperlink, the specific macro code will be executed at once.


Related articles:

How to run macro when cell value changes in Excel?

How to run macro based on cell value in Excel?

How to run macro based on value selected from drop down list in Excel?

How to run macro automatically before printing in Excel?

How to run macro when sheet is selected from a workbook?


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
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.
    Lucas · 9 months ago
    Olá,

    Um dos poucos locais onde encontrei essa solução. Muito obrigado! Estou quase lá. Não estou usando uma célula para o hiperlink, mas uma imagem que inseri na planilha e indicando uma célula qualquer como referência. Fiz como indicado, mas não funciona (Só para verificação, fiz com uma célula e deu certo). Há alguma modificação a ser feita na macro?
  • To post as a guest, your comment is unpublished.
    lg · 1 years ago
    Hello
    This is very handy, thank you, however this only works if you are in the cell $B$8. I want to create the same hyperlink, which performs the same macro function, but in every row down the sheet.
    ie. I have two tabs, sheet 1 and sheet 2. The user is in Sheet 1, and a macro is run which enters data in sheet 1 and sheet 2 on a row-by-row basis. This works fine, but then i want a macro so that the user can click a link in the next cell and it takes them back to their original row in sheet 1, offset a few cells. I can do this using the above, BUT only if the user is in a set row in sheet 2 (eg $B$8). How can i make it so the macro works when they are in sheet 2 B9, B10, B11....?
    • To post as a guest, your comment is unpublished.
      answer · 1 years ago
      This will assign sheet1 column J, row 2 to 35 with a hyperlink to it's own cell with the text name

      Sub whateverName()
      For i = 2 To 35 Step 1
      ActiveCell.FormulaR1C1 = "name"
      Range("J" & i).Select
      ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
      "'sheet1'!J" & i, TextToDisplay:="name"
      Next

      End Sub