Run vba by clicking shapehyperlink

ybr_15

Board Regular
Joined
May 24, 2016
Messages
204
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi dear, I am still learning about hyperlinks (with shape). I have vba code in module to move from one sheet to another, like this
Code:
Sub HomeButton()
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim rngReference As Worksheet
    Dim rngDestination As Worksheet
    Dim shpHyperlink As Shape
    
    Set rngReference = wb.ActiveSheet
    Set rngDestination = wb.Sheets("Home")
    Set shpHyperlink = rngReference.Shapes("Rectangle 1")
    
    rngReference.Hyperlinks.Add Anchor:=shpHyperlink, Address:="", SubAddress:="'" & rngDestination.Name & "'!B7", ScreenTip:="Back to Home"
End Sub
The code above work properly. Now, I want to add a new code so that when I click the hyperlink above (before moving to another sheet) then the second to fifth rows will be hidden
Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim rngReference As Worksheet
    
    Set rngReference = wb.Sheets("Report")
    If rngReference.Rows("2:5").EntireRow.Hidden = False Then rngReference.Rows("2:5").EntireRow.Hidden = True
End Sub
But, I still didn't get what I wanted (second to fifth lines are not hidden in Report sheet before moving to Home sheet). I still confused here how to add or putt the code so I can get what I want. Thank you.....#Sorry for my bad english ;)
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi there. Try removing the IF clause, so the line:
Code:
    If rngReference.Rows("2:5").EntireRow.Hidden = False Then rngReference.Rows("2:5").EntireRow.Hidden = True

becomes:
Code:
rngReference.Rows("2:5").EntireRow.Hidden = True
 
Upvote 0
Hi jmacleary, thanks for your reply. I have tried your suggestion but still can't. I know if the hyperlink is based on cell reference my code will run as I want (your suggestion too). But now, what I want to try is to use shape hyperlink to run my vba code. Can anyone help me? Thanks
 
Upvote 0
The FollowHyperlink event doesn't run when the hyperlink is on a shape.

How about assigning a macro to the shape instead of an hyperlink ?
 
Upvote 0
Hi Jaafar Tribak, sorry I don't know about that. I think because I can use hyperlink based on a cell reference so that I can do it too on a shape. I will try another ways as you suggest. Thanks for your information :)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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