1

我有代码来扫描文档并使用超链接打印数据透视表的所有名称。但不确定如何将其链接到数据透视表的位置。这是代码:

Sub Button20_Click()

Dim sh As Worksheet
Dim cell As Range
Dim pvt As PivotTable

Range("A1").Select

For Each sh In ActiveWorkbook.Worksheets
 For Each pvt In sh.PivotTables
   ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
    "'" & pvt.Name & "'", TextToDisplay:=pvt.Name
    ActiveCell.Offset(1, 0).Select
  Next pvt
Next sh

End Sub
4

1 回答 1

0

我选择了第一个单元格TableRange2作为SubAddress. 那应该是数据透视表的左上角单元格。该External:=True参数创建完整地址,包括数据透视表所在的工作表:

Sub PivotHyperLinks()
Dim ListStart As Range
Dim sh As Worksheet
Dim pvt As PivotTable
Dim i As Long

With ActiveSheet
    Set ListStart = .Range("A50")
    For Each sh In ActiveWorkbook.Worksheets
        For Each pvt In sh.PivotTables
            .Hyperlinks.Add Anchor:=ListStart.Offset(i, 0), Address:="", SubAddress:= _
                                       pvt.TableRange2.Cells(1).Address(external:=True), TextToDisplay:=pvt.Name
            i = i + 1
        Next pvt
    Next sh
End With
ListStart.Select
End Sub

我还修改了代码以不使用Select语句。通常应避免使用它们,因为它们效率低下(尽管它们在这里并没有造成太大的危害)。链接仍会添加到 ActiveSheet。

编辑:我添加了最后一条Select语句,将您带到链接列表的开头。

于 2013-08-14T14:22:57.313 回答