user2142350
user2142350

Reputation: 21

Excel-Hyperlink with a search function

Excel - I would like to be able to click on a cell on sheet 1 and have it take me to a cell in sheet 2. Now I do not want a simple hyperlink, I would need the cell in sheet 2 to be found. For example, I click on cell A1 that contains the word "start" and upon clicking on it I am taken to the cell in sheet 2 with the same phrase "start". Now on sheet 2 this could be cell A1 or A4 or A57. I would like it found in case it ever changes position within that column. Essentially I need a hyperlink with a find function.

Upvotes: 2

Views: 3510

Answers (1)

chris neilsen
chris neilsen

Reputation: 53137

You can use the Worksheet_FollowHyperlink VBA event to move the selection after the link is clicked.

Add a normal hyperlink to any cell on the desired destination sheet

Add this code to the source worksheet module

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    Dim rngDest As Range
    Dim shDest As Worksheet
    Dim cl As Range

    Set shDest = ActiveSheet
    Set rngDest = shDest.Range("A:A") ' <--- change this to your target search range
    With rngDest
        Set cl = .Find(Target.Range.Cells(1, 1).Value, .Cells(.Rows.Count, .Columns.Count), xlValues, xlWhole, xlByRows, xlNext)
        If Not cl Is Nothing Then
            cl.Select
        Else
            ' value not found, return to original sheet
            Target.Range.Worksheet.Activate
            MsgBox Target.Range.Cells(1, 1).Value & " not found", vbOKOnly, "Not Found"
        End If
    End With

End Sub

Upvotes: 4

Related Questions