简体   繁体   English

在VBA函数中引用动态单元格以创建具有相同名称的工作表索引

[英]Referencing a dynamic cell in VBA function for creating an Index of Worksheet with that same name

I have 3 worksheets: Table of Contents, Sheet Generator, and Sheet 1. 我有3个工作表:目录,工作表生成器和工作表1。

The "Sheet Generator" worksheet is where the unique sheet name is created, we will call the unique sheet "Sheet 1". “表生成器”工作表是创建唯一表名的地方,我们将唯一表称为“表1”。 At the point of generating "Sheet 1", the macro also inputs the unique name "Sheet 1" into a new line in in "Table of Contents" worksheet. 在生成“表1”时,宏还将在“目录”工作表中的新行中输入唯一名称“表1”。

This process happens many, many times and I can end up with 30 new uniquely named sheets. 这个过程发生了很多次,最终我可以得到30个新的唯一命名的工作表。

Currently I am manually adding a hyperlinks to these sheets so I can jump to each one a little quicker, than having to scroll horizontally for days. 目前,我正在手动为这些工作表添加超链接,因此与水平滚动数天相比,我可以更快地跳转到每个工作表。 I want to add into the macro that generates the unique name into the "Table Of Contents" a hyperlink to that sheet so I no longer have to do it manually. 我想在生成“目录”的超链接的宏中添加到该表的超链接,因此我不再需要手动执行此操作。

Doing it manually looks like this when the "Table of Contents" has "Sheet 1" written in cell A3. 当“目录”的单元格A3中写有“表1”时,手动执行此操作看起来像这样。

Sub TestINDEX2()

Range("A3").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'Sheet 1'!ac", TextToDisplay:="Sheet 1"

End Sub

The unique name of the sheet is generated in cell "NB2" in the "Sheet Generator" worksheet. 工作表的唯一名称在“工作表生成器”工作表的单元格“ NB2”中生成。 But the hyperlink needs to be in cell "A3" in the "Table of Contents" worksheet. 但是,超链接必须位于“目录”工作表中的单元格“ A3”中。

I want it to do this: 我希望它这样做:

Sub TestINDEX2()

Range("A3").Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'Sheet Generator'!NB2", TextToDisplay:="'Sheet Generator'!NB2"

End Sub

But, this obviously doesn't work.... 但是,这显然行不通....

Any suggestions on how to reference a dynamic cell into a macro that adds hyperlinks? 关于如何在添加超链接的宏中引用动态单元格的任何建议?

I think this is what you're looking for: 我认为这是您要寻找的:

Dim wb As Workbook
Dim wsToC As Worksheet
Dim wsGen As Worksheet
Dim rDest as Range

Set wb = ActiveWorkbook
Set wsToC = wb.Sheets("Table of Contents")
Set wsGen = wb.Sheets("Sheet Generator")
Set rDest = wsToC.Cells(wsToC.Rows.Count, "A").End(xlUp).Offset(1)
If rDest.Row < 3 Then Set rDest = wsToC.Range("A3")

wsToC.Hyperlinks.Add Anchor:=rDest, _
                     Address:="", _
                     SubAddress:="'" & wsGen.Range("NB2").Text & "'!A3", _
                     TextToDisplay:=wsGen.Range("NB2").Text

assuming sheets names list is in "Sheet Generator" sheet from cell A3 downwards, you may try this: 假设工作表名称列表位于单元格A3下方的“工作表生成器”工作表中,则可以尝试以下操作:

Option Explicit

Sub BuildHyperlinks()
    Dim cell As Range
    With Worksheets("Sheet Generator")
        For Each cell In .Range("A3", .Cells(.Rows.Count, 1).End(xlUp))
            ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
            "'" & cell.Value2 & "'!A1", TextToDisplay:=cell.value
        Next
    End With
End Sub

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

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