Search code examples
excelvbahyperlinkrangeworksheet

Hyperlinking to a specific sheet in the workbook


I am trying to search for the sheet with the name of TextBox1.Value which is already created. Then find that same name on a sheet called "Employee List" and hyperlink that cell to the sheet with the same name.

It tries to link to "Name" inside the folder which I am working in. It doesn't even reach inside the workbook. Am I missing a parameter inside .Hyperlinks?

Dim findEmployee As Range
Dim foundEmployee As Range
Set findEmployee = Sheets("Employee List").Range("A:A")
Set foundEmployee = findEmployee.Find(TextBox1.Value)

With Worksheets("Employee List")
    .Hyperlinks.Add 
     Anchor:=.Range(foundEmployee.Address), _ Address:=Worksheets(TextBox1.Value).Range("A1"), _
     TextToDisplay:=TextBox1.Value
End With

Solution

  • Can you try this? When you record a macro, the subaddress is used rather than the address parameter, and it must be in the form of a string rather than range. if your sheet name has spaces you'll need to add single quotes to the string.

    Sub x()
    
    Dim findEmployee As Range
    Dim foundEmployee As Range
    
    Set findEmployee = Sheets("Employee List").Range("A:A")
    Set foundEmployee = findEmployee.Find(TextBox1.Value)
    
    If Not foundEmployee Is Nothing Then
        With Worksheets("Employee List")
            .Hyperlinks.Add Anchor:=.Range(foundEmployee.Address), _
                            Address:="", _
                            SubAddress:=Worksheets(TextBox1.Value).Name & "!A1", _
                            TextToDisplay:=TextBox1.Value
        End With
    End If
    
    End Sub