【问题标题】:PivotFields property of the PivotTable Class problemsPivotFields 属性的数据透视表类问题
【发布时间】:2016-12-21 09:38:56
【问题描述】:

我知道我会因此被扣分,但是就这样吧。

关于这个错误,我已经浏览了几个论坛和信息丰富的网站,但我无法找出问题所在。

错误是:

运行时错误“1004”:无法获取数据透视表类的 PivotFields 属性

这发生在以下行: With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Precinct")

我看到参考资料表明该错误可能是因为该字段未称为“Precinct”。但是,我直接复制并粘贴了它,并确保代码“写入”了该特定标题。我就是想不通。这可能与刷新数据或数据透视表有关吗?有没有办法用单元格引用替换问题行中的“Precinct”?

代码是:

    Sub OccupancyPivot() 
Dim SrcData As Variant 
Dim LRow As Long, LCol As Long 
Dim wsSheet As Worksheet 
Dim PTCache As PivotCache 
Dim PT As PivotTable 

 'Determine the data range you want to pivot
LRow = Cells(Rows.Count, 1).End(xlUp).Row 
LCol = Cells(1, Columns.Count).End(xlToLeft).Column 
Set SrcData = Worksheets("Raw Data").Range("A1:" & Cells(LRow, LCol).Address(False, False)) 

Sheets.Add.Name = "PivotTable1" 

Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData) 

Set PT = PTCache.CreatePivotTable(Sheets("PivotTable1").Range("A1"), "Occupancy") 

 'Create the headings and row and column orientation
With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Precinct") 
    .Orientation = xlRowField 
    .Position = 1 
End With 
With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Registration") 
    .Orientation = xlDataField 
    .Function = xlCount 
End With 

With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Captured Date") 
    .Orientation = xlColumnField 
    .Position = 1 
End With 

With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Captured Session") 
    .Orientation = xlColumnField 
    .Position = 2 
End With 

With Sheets("PivotTable1").PivotTables("Occupancy").PivotFields("Location") 
    .Orientation = xlRowField 
    .Position = 2 
End With 

 'ActiveWorkbook.Sheets("PivotTable").Visible = xlSheetVeryHidden
End Sub 

谁能告诉我上面有什么问题?

编辑:我发现其他一些关于这种情况的提及。由于某种原因,当数据透视子过程是其他子过程的一部分时,数据透视字段无法识别数据中的标题。我还没有找到明确的原因,但相信这与刷新数据透视和数据有关。

【问题讨论】:

  • 如果将msgbox PT.PivotFields("Precinct").Caption 添加到导致错误的行之前,会返回什么?
  • @Rory 当我将它放在 With 语句之前时,它只会出现上述错误。
  • 那么源数据的字段名不正确。
  • @Rory 我也这么认为。即使我从源数据中复制并粘贴字段名称,错误仍然存​​在。显然,pivot 函数无法识别名称,但我不知道为什么。
  • 启动此代码时是否激活了原始数据表?

标签: excel pivot-table vba


【解决方案1】:

试试下面的修改代码,我还添加了您已经创建“PivotTable1”工作表和“Occupancy”数据透视表(在此代码的先前运行中),然后您只想用“原始数据”表中的修改数据刷新数据透视表的数据。

Sub OccupancyPivot()

Dim SrcData             As Variant
Dim LRow                As Long, LCol       As Long
Dim wsSheet             As Worksheet
Dim PTCache             As PivotCache
Dim PT                  As PivotTable
Dim PivotShtExists      As Boolean

 ' Determine the data range you want to pivot
LRow = Worksheets("Raw Data").Cells(Worksheets("Raw Data").Rows.Count, 1).End(xlUp).Row
LCol = Worksheets("Raw Data").Cells(1, Worksheets("Raw Data").Columns.Count).End(xlToLeft).Column
Set SrcData = Worksheets("Raw Data").Range("A1:" & Cells(LRow, LCol).Address(False, False))

' check is "PivotTable1" sheet already exists (from previous Macro runs)
For Each wsSheet In ThisWorkbook.Sheets
    If wsSheet.Name = "PivotTable1" Then
        PivotShtExists = True
    End If
Next wsSheet

If Not PivotShtExists Then Sheets.Add.Name = "PivotTable1"
Set wsSheet = ThisWorkbook.Sheets("PivotTable1")

Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)

' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PT = wsSheet.PivotTables("Occupancy") ' check if "Occupancy" Pivot Table already created (in past runs of this Macro)

On Error GoTo 0
If PT Is Nothing Then

    ' create a new Pivot Table in "PivotTable1" sheet, start from Cell A1
    Set PT = wsSheet.PivotTables.Add(PivotCache:=PTCache, TableDestination:=wsSheet.Range("A1"), TableName:="Occupancy")


     'Create the headings and row and column orientation
    With PT.PivotFields("Precinct")
        .Orientation = xlRowField
        .Position = 1
    End With
    With PT.PivotFields("Registration")
        .Orientation = xlDataField
        .Function = xlCount
    End With

    With PT.PivotFields("Captured Date")
        .Orientation = xlColumnField
        .Position = 1
    End With

    With PT.PivotFields("Captured Session")
        .Orientation = xlColumnField
        .Position = 2
    End With

    With PT.PivotFields("Location")
        .Orientation = xlRowField
        .Position = 2
    End With

Else
    ' just refresh the Pivot cache with the updated Range (data in Sheet1)
    PT.ChangePivotCache PTCache
    PT.RefreshTable
End If

 'ActiveWorkbook.Sheets("PivotTable").Visible = xlSheetVeryHidden
End Sub

运行此代码后,多次扫描,这是我得到的结果(模拟数据):

【讨论】:

  • 初步测试似乎相当不错。我假设它与刷新数据透视缓存和数据透视表有关?
  • @user6661501 是的,这是其中的一部分。如果回答了你的帖子,标记为回答
  • 不幸的是,它仍然在同一点出现相同的错误。
  • @user6661501 我很困惑,你刚刚写的初始测试看起来还不错,所以发生了什么?第一次运行正常吗?
  • 我发现代码第一次运行正常,甚至第二次运行正常(这也完全关闭了excel)。之后虽然有时不注册。
【解决方案2】:

根据您目前所说的,我认为您的 LRow 和/或 Lcol 变量是从错误的工作表中设置的,因此您的源数据不是应该的。试试这个:

Sub OccupancyPivot()
    Dim SrcData               As String
    Dim wsSheet               As Worksheet
    Dim PTCache               As PivotCache
    Dim PT                    As PivotTable

    'Determine the data range you want to pivot
    SrcData = "'Raw Data'!" & Worksheets("Raw Data").Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1)

    Sheets.Add.Name = "PivotTable1"

    Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)

    Set PT = PTCache.CreatePivotTable(Sheets("PivotTable1").Range("A1"), "Occupancy")

    'Create the headings and row and column orientation
    With PT
        With .PivotFields("Precinct")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("Registration")
            .Orientation = xlDataField
            .Function = xlCount
        End With

        With .PivotFields("Captured Date")
            .Orientation = xlColumnField
            .Position = 1
        End With

        With .PivotFields("Captured Session")
            .Orientation = xlColumnField
            .Position = 2
        End With

        With .PivotFields("Location")
            .Orientation = xlRowField
            .Position = 2
        End With
    End With
    'ActiveWorkbook.Sheets("PivotTable").Visible = xlSheetVeryHidden
End Sub

【讨论】:

  • 我们当时遇到了一个新错误:对象'PivotCache'的方法'CreatePivotTable'失败。 'Set PT = PTCache.CreatePivotTable(Sheets("PivotTable1").Range("A1"), "Occupancy") ' 行出错
  • 从 A1 开始的 Raw Data 上是否有实心块中的数据?
  • 是的。数据从 A1 开始,一直到 F1898。
  • 我刚刚测试了代码,它对我来说很好用。你能提供一个不起作用的工作簿吗?
猜你喜欢
  • 2013-06-30
  • 2020-03-10
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
相关资源
最近更新 更多