我有扫描文档的代码,并用超链接打印数据透视表的所有名称。但不确定如何将其链接到数据透视表的位置。 这是代码:
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
答案 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
语句,将您带到链接列表的开头。