简体   繁体   中英

Insert hyperlink in a cell and open a hidden sheet [Excel VBA]

I'm new to excel VBA and I am trying to accomplish inserting a hyperlink in a cell using a VBA code. Upon clicking that value, I want to be able to open a hidden sheet.

What I have accomplished so far is this: open a hidden sheet using buttons .

Sub mainModule(ByVal a_Sheets As String)

'Hide all tabs
For Each sht In ActiveWorkbook.Sheets
If sht.Name <> "MAIN" Then sht.Visible = xlSheetVeryHidden
Next

'View tabs when button is clicked
For i = 0 To UBound(Split(a_Sheets, ","))
Sheets(Split(a_Sheets, ",")(i)).Visible = True
Sheets(Split(a_Sheets, ",")(i)).Activate
Next

End Sub

Sub goHere_Click()

Call mainModule("BIRTHDAYS")

End Sub

What I want to do is when I click "GO HERE", it will open the hidden sheet.

在此处输入图片说明

Moreover, I found something that might help me solve this.

Sub sbCreatingHyperLink()
ActiveSheet.Hyperlinks.Add Range("A5"), "https://www.google.com"
End Sub

Could help me figure out a way wherein instead of " https://www.google.com ", it will call a function module instead ( Call mainModule("BIRTHDAYS") )? Thank you.

To create the hyperlinks dynamically into Sheet1 you would need to use something like the following:

Sub foo()
Dim ws As Worksheet
i = 1
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Sheet1" Then
        Sheet1.Cells(i, 1).Hyperlinks.Add Anchor:=Sheet1.Cells(i, 1), Address:="", SubAddress:=ws.Name & "!A1", TextToDisplay:="Go To:"
        i = i + 1
    End If
    Next
End Sub

Then you would need to also change the Worksheet_FollowHyperlink to see if the hyperlink has been clicked and if so unhide the Sheet and show it. Something like the following:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$A$2" Then
        Sheet2.Visible = xlSheetVisible
        Sheet2.Select
    End If
    'copy the if statement above for each hyperlink
End Sub

UPDATE:

So in my testing I have 5 sheets all of them xlSheetVeryHidden apart from the first one that is called "Main", when I run the macro below the following hyperlinks are created:

Sub foo()
Dim ws As Worksheet
i = 1
    For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "Main" Then
        Sheets("Main").Cells(i, 1).Hyperlinks.Add Anchor:=Sheet1.Cells(i, 1), Address:="", SubAddress:=ws.Name & "!A1", TextToDisplay:="Go To:"
        i = i + 1
    End If
    Next
End Sub

The code above creates the following (ignoring the first sheet "Main"):

在此处输入图片说明

Then behind the first Sheet's code I entered this code:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Range.Address = "$A$2" Then
        Sheet2.Visible = xlSheetVisible
        Sheet2.Select
    End If
    'copy the if statement above for each hyperlink
End Sub

This is where I placed the code:

在此处输入图片说明

The issue with this set up, is that even though the hyperlinks can be created dynamically by the macro foo(), to change the visibility you would have to manually add each Target.Range.Address to unhide the relevant sheet, so in my example I've only coded it so that if the user clicks on A2 of Sheet 1, which contains the hyperlink, then Sheet2 would become visible and you would navigate to it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM