Link runs macro...second click runs another macro?

jmpatrick

Active Member
Joined
Aug 17, 2016
Messages
477
Office Version
  1. 365
Platform
  1. Windows
Searched for this and didn't see a solution that used a link instead of a command button.

I have two very basic macros: one that shows gridlines, headings and the formula bar, and a second one that hides them.

I want use a sinle link in cell H1 that alternates between the two macros. The sheet would open with everything hidden (macro called HideWork) and the text link in the cell reading Show Work. If I click the link (running the macro called ShowWork) the macro runs and the text link in H1 changes to Hide Work.

Here's the macros I have so far:

VBA Code:
Sub ShowWork()
  ActiveWindow.DisplayGridlines = True
  ActiveWindow.DisplayHeadings = True
  Application.DisplayFormulaBar = True
End Sub

VBA Code:
Sub HideWork()
  ActiveWindow.DisplayGridlines = False
  ActiveWindow.DisplayHeadings = False
  Application.DisplayFormulaBar = False
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Here's an example:
Create a hyperlink, assign it to cell I2.
Replace MsgBox with a call to your sub.
VBA Code:
Dim flag As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Address(0, 0) = "I2" Then
        If flag Then
            MsgBox "ZZ"
        Else
            MsgBox "qqqqqqqqq"
        End If
        flag = Not flag
    End If

End Sub

another method:
double-clicking cell H1 instead of using hyperlink
 
Upvote 0
Create a hyperlink in cell H1 that references itself and place the code below in the VBE of the Sheet where the hyperlink exists.

1629816631394.png


NOTE: UPDATED Code to exit sub as it was continuing to call the second macro.

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Show Work" Then
        Call HideWork
        Selection.Hyperlinks(1).TextToDisplay = "Hide Work"
        Exit Sub
    End If
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Hide Work" Then
        Call ShowWork
        Selection.Hyperlinks(1).TextToDisplay = "Show Work"
        Exit Sub
    End If
End Sub
 
Last edited:
Upvote 0
Solution
Create a hyperlink in cell H1 that references itself and place the code below in the VBE of the Sheet where the hyperlink exists.

View attachment 45533

NOTE: UPDATED Code to exit sub as it was continuing to call the second macro.

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Show Work" Then
        Call HideWork
        Selection.Hyperlinks(1).TextToDisplay = "Hide Work"
        Exit Sub
    End If
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Hide Work" Then
        Call ShowWork
        Selection.Hyperlinks(1).TextToDisplay = "Show Work"
        Exit Sub
    End If
End Sub

Perfect! Thank you.
 
Upvote 0
Additionally you will want to put this code into the Thisworkbook section of the VBE so that when the workbook opens, it will "Hide Work" and show the hyperlink as "Show Work"

VBA Code:
Private Sub Workbook_Open()
    If Range("H1").Hyperlinks(1).TextToDisplay = "Hide Work" Then
        Call HideWork
        Range("H1").Hyperlinks(1).TextToDisplay = "Show Work"
    End If
End Sub
 
Upvote 0
I just realized, I reversed the macro calls above. the code is updated below.

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Show Work" Then
        'Call HideWork
         Call ShowWork
        Selection.Hyperlinks(1).TextToDisplay = "Hide Work"
        Exit Sub
    End If
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Hide Work" Then
        'Call ShowWork
        Call HideWork 
        Selection.Hyperlinks(1).TextToDisplay = "Show Work"
        Exit Sub
    End If
End Sub
 
Upvote 0
I just realized, I reversed the macro calls above. the code is updated below.

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Show Work" Then
        'Call HideWork
         Call ShowWork
        Selection.Hyperlinks(1).TextToDisplay = "Hide Work"
        Exit Sub
    End If
    If Target.Range.Address = "$H$1" And Selection.Hyperlinks(1).TextToDisplay = "Hide Work" Then
        'Call ShowWork
        Call HideWork
        Selection.Hyperlinks(1).TextToDisplay = "Show Work"
        Exit Sub
    End If
End Sub

I figured that out!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,857
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top