【问题标题】:Pivot table Run Time error 1004: Application-defined or object defined error数据透视表运行时错误 1004:应用程序定义或对象定义错误
【发布时间】:2020-09-01 05:57:45
【问题描述】:

我有以下代码,它基本上获取数据透视表行字段并尝试更新与这些行条目关联的链接。

代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'this exist
Set PvtTbl = PivotTables("pt1")

'this exist    
Set pvtFields = PvtTbl.RowFields(1)
   

For Each cel In pvtFields.DataRange
        ActiveSheet.Hyperlinks.Add Range(Cells(cel.Row, cel.Column), Cells(cel.Row, cel.Column)), Address:="https://www.google.com/"
    Next cel
        
End Sub

问题

每当我运行此代码时,它都会出错并显示“运行时错误 1004:应用程序定义或对象定义错误”

注意

  1. Excel 具有不受限制的访问权限
  2. 调试Cells(cel.Row, cel.Column) 总是产生价值
  3. 调试Range(Cells(cel.Row, cel.Column), Cells(cel.Row, cel.Column)) 总是产生价值
  4. 当我将 cel.Row 和 cel.column 替换为有效值(例如 7,3)时,我得到了同样的错误。但是,它可以在同一张纸上工作(例如 row,col: 1,1 没有数据透视表。)
  5. 我尝试了 Worksheet_SelectionChange 和 Worksheet_PivotTableUpdate 事件,但没有任何帮助。

不确定,数据透视表中真正缺少什么。如果你看到了什么,请告诉我。

谢谢

【问题讨论】:

  • “pt1”是否存在?

标签: excel vba


【解决方案1】:

您不能在数据透视表中插入超链接。

If you try to do it manually, you will see that when the cell inside the Pivot is selected, the Insert link menu also gets disabled.

当您在枢轴外部单击时,它会被启用。

这是实现您想要的另一种方法

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        Dim PvtTbl As PivotTable
        Set PvtTbl = PivotTables(1)
        
        Dim pvtFields As PivotField
        Set pvtFields = PvtTbl.RowFields(1)

        If Not Intersect(Target, pvtFields.DataRange) Is Nothing Then
            ThisWorkbook.FollowHyperlink Address:="https://www.google.com/"
        End If
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-28
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多