Excel-Hyperlink with a search function

后端 未结 1 498
遇见更好的自我
遇见更好的自我 2021-01-22 06:23

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

1条回答
  •  鱼传尺愫
    2021-01-22 07:08

    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
    

    0 讨论(0)
提交回复
热议问题