excel 创建指向其他工作表的宏超链接

7gs2gvoe  于 2023-01-21  发布在  其他
关注(0)|答案(2)|浏览(215)

希望创建一个自动引用特定单元格(不同的工作表)的宏,而不是手动更改单元格引用并逐个链接。例如,用户将单击“TB”(第一张图片),这将使他们进入TB工作表中的精细单元格(第二张图片)。

尝试了下面的,但是说无效的调用或参数,我可以看到RDRef是空的,没有值被分配给它,不知道为什么。

Sub Hyperlink()

Dim reference As Range
Dim TBRef As Variant
Dim RDDef As Variant

x = 2
y = 2

Worksheets("Queries").Select

    For Each reference In Range("B3:B7").Cells
        If reference = "TB" Then
            TBRef = Worksheets("TB").Cells(x, "A").Value
            ActiveCell.Hyperlinks.Add Anchor:=reference, Address:="", SubAddress:=TBRef, TextToDisplay:="TB"
        Else
            RDRef = Worksheets("R&D Schedule").Cells(y, "A").Value
            ActiveCell.Hyperlinks.Add Anchor:=reference, Address:="", SubAddress:=RDRef, TextToDisplay:="R&D"
    
        End If
x = x + 1
y = y + 1
    Next reference
End Sub

预期:用户点击TB for Fines,这将使他们进入工作表TB单元格Fines。宏将自动创建从查询中的每个单元格到不同工作表中特定单元格的链接

xzlaal3s

xzlaal3s1#

SubAddress必须以地址的形式传递,而不是单元格的值。而且你必须在目标工作表中查找单元格才能得到地址。
这就是函数getAddressOfCell返回的结果。

Sub addHyperlink()

Dim wsQueries As Worksheet
Set wsQueries = ThisWorkbook.Worksheets("Queries")

Dim rgType As Range
Set rgType = wsQueries.Range("B2:B6")

Dim subAddress As String
Dim reference As Range

For Each reference In rgType.Cells
    subAddress = getAddressOfCell(reference.Offset(, -1), _
        ThisWorkbook.Worksheets(reference.Value).Cells(1, 1).CurrentRegion)
    If subAddress <> vbNullString Then
        wsQueries.Hyperlinks.Add anchor:=reference, Address:="", subAddress:=subAddress ', TextToDisplay:=reference.Value
    End If
Next reference
End Sub

Private Function getAddressOfCell(strFind As String, rgSearchIn As Range) As String
Dim rgFound As Range
With rgSearchIn 
    Set rgFound = .Find(what:=strFind)
    If Not rgFound Is Nothing Then
        getAddressOfCell = rgFound.Address(True, True, , True)
    End If
End With
End Function
sh7euo9m

sh7euo9m2#

添加超链接

Option Explicit

Sub AddHyperlinksToQueries()

    Dim sNames(): sNames = VBA.Array("TB", "RD Schedule")
    Dim sAddresses(): sAddresses = VBA.Array("A2", "A2")
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    
    Dim dws As Worksheet: Set dws = wb.Sheets("Queries")
    Dim drg As Range
    Set drg = dws.Range("B3", dws.Cells(dws.Rows.Count, "B").End(xlUp))
    
    Dim dCell As Range, sIndex, sName As String, sAddress As String
    
    For Each dCell In drg.Cells
        sName = CStr(dCell.Value)
        sIndex = Application.Match(sName, sNames, 0)
        If IsNumeric(sIndex) Then ' 'sIndex' is one-based...
            sIndex = sIndex - 1 ' ... the arrays are zero-based
            sName = sNames(sIndex)
            sAddress = sAddresses(sIndex)
            dCell.Hyperlinks.Add Anchor:=dCell, Address:="", _
                SubAddress:="'" & sName & "'!" & sAddress, TextToDisplay:=sName
            sAddresses(sIndex) = dws.Range(sAddress).Offset(1).Address(0, 0)
        Else ' not in array
            dCell.Clear
        End If
    Next dCell
        
    MsgBox "Hyperlinks created.", vbInformation
        
End Sub

相关问题