I want the code to first determine whether a single cell is selected on the sheet I'm in, and then it should determine whether another sheet in the Workbook contains a value that matches the value in the selected cell.
I'm trying to make a hyperlink in the selected cell that refers to the sheet with the same name/value if these criteria are satisfied.
For instance, I want the code to generate a hyperlink in cell A1 to the sheet named "1000" if I select a cell (A1) with the value "1000" and there is a sheet named "1000".
selectedCell.Hyperlinks.Add Anchor:=selectedCell, Address:="", SubAddress:=selectedCell.Value & "!", TextToDisplay:=selectedCell.Value
Sub CreateHyperlinkToSheet()
Dim selectedCell As Range
Set selectedCell = Selection
If selectedCell.Count <> 1 Then
MsgBox "Please select a single cell.", vbExclamation
Exit Sub
End If
If Not SheetExists(selectedCell.Value) Then
MsgBox "No sheet exists with the name '" & selectedCell.Value & "'.", vbExclamation
Exit Sub
End If
selectedCell.Hyperlinks.Add Anchor:=selectedCell, Address:="", SubAddress:=selectedCell.Value & "!", TextToDisplay:=selectedCell.Value
End Sub
Function SheetExists(sheetName As String) As Boolean
On Error Resume Next
SheetExists = (Len(Sheets(sheetName).Name) > 0)
On Error GoTo 0
End Function