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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,224,835
Messages
6,181,247
Members
453,026
Latest member
cknader

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