VBA code error while using Hyperlink for multiple sheets

I’m trying to run a code, in which when I click a hyperlink to the sheet2 it opens and when I clicked on the sheet1 the sheet2 with the link is not visible anymore, it will be only visible if clicked on link.

But it is only working for 1 sheet i want to do this for multiple sheets.

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
    ' Unhide the sheet named "sheet2"
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("sheet2")
    
    ' Unhide the sheet when the hyperlink is clicked
    ws.Visible = xlSheetVisible
    
    ' Activate the sheet
    ws.Activate
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    ' Hide the sheet again when you leave it
    If Sh.Name = "sheet2" Then
        Sh.Visible = xlSheetVeryHidden
    End If
End Sub

This VBA code controls the visibility of worksheets based on hyperlink navigation. When a hyperlink is clicked, the Workbook_SheetFollowHyperlink event makes the destination sheet visible and navigates to it, while the current sheet remains visible. The Workbook_SheetDeactivate event hides any sheet that is deactivated unless it is a sheet explicitly excluded (such as Sheet1). This ensures that specific sheets, like Sheet1, remain visible while others are hidden upon deactivation.

The expression ThisWorkbook.Worksheets(Array(Sheet1.Name)) is used to reference specific sheets by dynamically retrieving their names. In this case, it fetches the name of the worksheet object Sheet1 and uses it to create an array of sheet names. This allows you to easily refer to the worksheet even if its name changes in the future. By using the Sheet1 object (the code name of the worksheet), you ensure that the reference remains valid regardless of what the user renames the sheet to in Excel.

Using the sheet’s code name (Sheet1) is a safer and more reliable method than hardcoding its name (e.g., “Sheet1”). The code name of a sheet does not change when the user renames the sheet through the Excel interface, whereas the sheet name (the visible name) can be altered. This ensures that the code continues to function correctly even if the user modifies the sheet’s name.

Refactored Code

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
    Dim Destination As Range
    Rem Use error handling to prevent the code from stopping if there is an issue with the hyperlink
    On Error Resume Next
    
    Rem Set the destination range based on the hyperlink's target address (SubAddress)
    Set Destination = Range(Target.SubAddress)
    
    Rem Reset error handling back to default
    On Error GoTo 0
        
    Rem If the destination is valid (not Nothing), proceed with the following
    If Not Destination Is Nothing Then
        
        Rem Make the sheet where the hyperlink leads visible
        Destination.Parent.Visible = xlSheetVisible
        
        Rem Navigate to the destination range
        Application.Goto Destination
    End If
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Dim Worksheet As Worksheet
    
    Rem Loop through each worksheet in the workbook except for the active one (Sh)
    For Each Worksheet In ThisWorkbook.Worksheets(Array(Sheet1.Name))
        
        Rem If the current sheet is the active one (Sh), exit the sub to avoid hiding it
        If Worksheet Is Sh Then Exit Sub
    Next
    
    Rem If the sheet is not the active one, hide it when deactivating
    Sh.Visible = xlSheetVeryHidden
End Sub

Assuming you have cell reference hyperlinks on the sheet1

you need to use this code:

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
  Dim sa As String
  sa = Target.SubAddress
  sa = Left$(sa, InStr(sa, "!") - 1)
  If Left$(sa, 1) = "'" Then sa = Mid$(sa, 2, Len(sa) - 2)
  With ThisWorkbook.Sheets(sa)
    .Visible = xlSheetVisible
    .Activate
  End With
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    ' Hide the sheet again when you leave it
    If Sh.Name <> "sheet1" Then ' use the exact sheet name here
        Sh.Visible = xlSheetVeryHidden
    End If
End Sub

P. S. I depicted my test workbook where sheets are Sheet1, Sheet2, and Sheet3. But in code I used the sheet name (“sheet1”) provided by TS.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật