简体   繁体   English

在单元格中插入超链接并打开隐藏的工作表[Excel VBA]

[英]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. 我是excel VBA的新手,我正在尝试使用VBA代码在单元格中插入超链接。 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") )? 能帮我找出一种方法,而不是“ https://www.google.com ”,而是调用一个功能模块( Call mainModule("BIRTHDAYS") )吗? Thank you. 谢谢。

To create the hyperlinks dynamically into Sheet1 you would need to use something like the following: 要将超链接动态创建到Sheet1中,您将需要使用以下内容:

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. 然后,您还需要更改Worksheet_FollowHyperlink,以查看是否已单击超链接,如果不单击,则取消隐藏工作表并显示它。 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: 因此,在我的测试中,当我运行下面的超链接时,除了第一个被称为“ Main”的表外,我都有5个表xlSheetVeryHidden。

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"): 上面的代码创建以下内容(忽略第一张工作表“ 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... 设置的问题在于,即使超链接可以由宏foo()动态创建,要更改可见性,您也必须手动添加每个Target.Range.Address才能取消隐藏相关工作表,因此在我的示例中我只是对它进行了编码,以便如果用户单击包含超链接的工作表1的A2,那么工作表Sheet2将变为可见,并且您可以导航到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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