列表中的超链接透视表

时间:2013-08-14 13:44:23

标签: excel vba

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

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

1 个答案:

答案 0 :(得分: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语句,将您带到链接列表的开头。