Dynamic link to last selected cell in other sheet

jpetoday

New Member
Joined
Jul 11, 2021
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Hi all,
I'm having a hard time getting this code by John, posted below, to work.
At first, it seems to work fine: when I hover over the hyperlink in cell A1, it displays: 'sheet10!B12', which is where I previously clicked, so that's correct. However, when I click on it, I get 'reference is invalid' (not exactly sure what this error is called in English, this is my translation). The strange thing is, the hyperlink works only when I come from 1 specific sheet. For all others, I get the error I mentioned.

Any chance someone can help me out with this?
I can't think of a formula solution, so try this code which goes in the ThisWorkbook module. The code assumes the sheet 11 is named "Sheet11". It creates a hyperlink to the previously selected sheet and cell in cell A1 of "Sheet11".
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If Sh.Name <> "Sheet11" Then
        Worksheets("Sheet11").Hyperlinks.Add Anchor:=Worksheets("Sheet11").Range("A1"), Address:="", _
            SubAddress:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False), _
            TextToDisplay:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False)
    End If
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name <> "Sheet11" Then
        Worksheets("Sheet11").Hyperlinks.Add Anchor:=Worksheets("Sheet11").Range("A1"), Address:="", _
            SubAddress:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False), _
            TextToDisplay:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False)
    End If
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi @jpetoday, Welcome to the MrExcel message board!

Your code has a minor omission. The fact that worksheet names may contain spaces is not taken into account. The code below is likely to work as expected.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    If Sh.Name <> "Sheet11" Then
        Worksheets("Sheet11").Hyperlinks.Add Anchor:=Worksheets("Sheet11").Range("A1"), Address:="", _
            SubAddress:="'" & ActiveCell.Parent.Name & "'" & "!" & ActiveCell.Address(False, False), _
            TextToDisplay:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False)
    End If
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name <> "Sheet11" Then
        Worksheets("Sheet11").Hyperlinks.Add Anchor:=Worksheets("Sheet11").Range("A1"), Address:="", _
            SubAddress:="'" & ActiveCell.Parent.Name & "'" & "!" & ActiveCell.Address(False, False), _
            TextToDisplay:=ActiveCell.Parent.Name & "!" & ActiveCell.Address(False, False)
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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