Macro to dynamically hyperlink to different cell in another worksheet

meowws

New Member
Joined
Jan 17, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi All,
Looking to create a macro which will automatically reference a specific cell (different worksheet), instead of manually changing the cell reference and linking one by one.
For example, user will click on "TB" (1st image), this will take them to the Fines cell in TB worksheet (2nd image).
V7nH4.png
1674013101162.png


Have attempted to write a simple macro, however is not working and looking for some advice.
Also looking to see how to go about specifying fines to be linked to fines with an AND function?

Sub Hyperlink()

For Each reference In Range("B3:B7")
If reference = "TB" Then
Worksheets("TB").Activate
Cells(2, "A").Activate.Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:="'Queries' !B3", TextToDisplay:="TB"

Exit For
End If
Next

End Sub
 

Attachments

  • 1673934046089.png
    1673934046089.png
    7.2 KB · Views: 10

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Made some changes - however saying invalid call or argument
I can see RDRef is empty and no value is assigned to it, not sure why.

Sub Hyperlink()

Dim reference As Range
Dim TBRef As Variant
Dim RDDef As Variant

x = 2
y = 2

Worksheets("Queries").Select

For Each reference In Range("B3:B7").Cells
If reference = "TB" Then
TBRef = Worksheets("TB").Cells(x, "A").Value
ActiveCell.Hyperlinks.Add Anchor:=reference, Address:="", SubAddress:=TBRef, TextToDisplay:="TB"
Debug.Print TBRef
Else
RDRef = Worksheets("R&D Schedule").Cells(y, "A").Value
ActiveCell.Hyperlinks.Add Anchor:=reference, Address:="", SubAddress:=RDRef, TextToDisplay:="R&D"
Debug.Print RDRef
End If
x = x + 1
y = y + 1
Next reference
End Sub
 
Upvote 0
Would this be an option for you ?

• In Queries have the Hyperlinks just point to A1 in TB or R&D Schedule as required.
• Right Click on the Queries Sheet Tab Name and select View Code
• In the code screen that appears copy in the code below

Clicking on a Hyperlink in Queries will trigger the code.
The Hyperlink address will tell the code which sheet you want and the code will then search for the description from Column A in the target sheet.

VBA Code:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim strSubAddShtName As Variant
    Dim destSht As Worksheet
    Dim cellFindDesc As Range
    
    strSubAddShtName = Replace(Split(Target.SubAddress, "!")(0), "'", "")
    Set destSht = Worksheets(strSubAddShtName)
    
    Set cellFindDesc = destSht.Columns(1).Find(what:=Target.Parent.Offset(, -1), _
                                LookIn:=xlFormulas, SearchDirection:=xlNext, MatchCase:=False)
    If cellFindDesc Is Nothing Then
        Exit Sub
    Else
        cellFindDesc.Select
    End If
                           
End Sub
 
Upvote 0
Thank you for that!

VBA Code:
Sub addHyperlink()

Dim mysht As Worksheet
Dim sht As Worksheet

Set mysht = ActiveSheet
Set sht = ThisWorkbook.Worksheets("TB")
Set sht2 = Thisworkbook.Worksheets("R&D")

Dim reference As Range
Dim TBRef As String
Dim RDDef As String

For Each reference In mysht.Range("B3:B7").Cells
If reference = "TB" Then
        TBRef = getAddressOfCell(reference.Offset(, -1), sht.Range("A1:B5").CurrentRegion)
        mysht.Hyperlinks.Add Anchor:=reference, Address:="", subAddress:=TBRef, TextToDisplay:="TB"
        Else
        RDRef = getAddressOfCell(reference.Offset(, -1), ThisWorkbook.Worksheets("R&D Schedule").Cells(1, 1).CurrentRegion)
        mysht.Hyperlinks.Add Anchor:=reference, Address:="", subAddress:=RDRef, TextToDisplay:="R&D"
    End If
End If

Private Function getAddressOfCell(strFind As String, rgSearchIn As Range) As String
Dim rgFound As Range
With rgSearchIn
    Set rgFound = .Find(what:=strFind)
    If Not rgFound Is Nothing Then
        getAddressOfCell = rgFound.Address(True, True, , True)
    End If
End With
End Function
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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