【问题标题】:Excel VBA pivot table cache method errorExcel VBA 数据透视表缓存方法错误
【发布时间】:2017-07-25 03:42:00
【问题描述】:

我正在尝试执行一个宏,它基本上从动态范围创建一个数据透视表,然后利用这些数据。

数据透视表保存在工作表中,每次执行宏都会删除该工作表中的数据透视表,然后更新数据范围并使用新范围再次生成数据透视表。

但是,尝试生成缓存的部分代码会导致错误:

“无效的过程调用或参数”。

起初它失败了,所以我添加了一个刷新现有(我认为是)缓存的片段。但我仍然收到提到的错误。

代码如下。

Sub Weeks_Coverage_Calc()
'Creates a pivot table to calculate stock weeks coverage for every item

Dim sht As Worksheet
Dim Activesht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim lastRow As Integer
Dim Rng As Range
Dim Row As Range

'Clear the worksheet for pivot table
    Set sht = ActiveWorkbook.Sheets("Weeks Coverage")
    For Each pvt In sht.PivotTables
    pvt.PivotSelect "", xlDataAndLabel, True
    Selection.Delete Shift:=xlToLeft
    Next pvt
'Set Transfer sheet as active sheet
 Set Activesht = ActiveWorkbook.Sheets("Transfer")

'Determine the dynamic range
 lastRow = Activesht.Range("B1000000").End(xlUp).Row
 Set Rng = Activesht.Range("B4:AF" & lastRow)


'Determine the data range you want to pivot
  SrcData = ActiveSheet.Name & "!" & Rng.Address(ReferenceStyle:=xlR1C1)

'Initialize the cell to start the table
  StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)

'Create Pivot Cache from Source Data
  Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)


    'Refresh pivot cache
        For i = 1 To Worksheets("Weeks Coverage").PivotTables.Count
        Worksheets("Weeks Coverage").PivotTables(i).PivotCache.Refresh
        Next i


'Create Pivot table from Pivot Cache
    Set pvt = pvtCache.CreatePivotTable( _
    TableDestination:=StartPvt, _
    TableName:="PivotTable1")

'Add fields to Pivot Table

    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Item ID")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Stok"), "Sum of Stok", xlSum

    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Sales"), "Sum of Sales", xlSum

【问题讨论】:

  • 应该是SrcData = ActiveSht.Name 而不是SrcData = ActiveSheet.Name

标签: excel caching pivot-table vba


【解决方案1】:

尝试使用以下代码设置PivotTablePivotCache

'Determine the data range you want to pivot
SrcData = Rng.Address(False, False, xlA1, xlExternal)

'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
            SourceData:=SrcData)

'Create Pivot table from Pivot Cache
Set pvt = sht.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=sht.Range("A3"), _
            TableName:="PivotTable1")

【讨论】:

  • 但是,出现了一个新问题。向表中添加数据透视字段时,它会显示“无法获取数据透视类的工作表功能”。我试图将名字更改为工作表名称,但无法修复它。我尝试将 put 代码放在评论中,但它可以在问题本身中找到。
  • '向数据透视表添加字段'Worksheets("Weeks Coverage").Activate With Sheets("Weeks Coverage").PivotTables("PivotTable1").PivotFields("Item ID") .Orientation = xlRowField .Position = 1 以 ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables(_"PivotTable1").PivotFields("Stok"), "Sum of Stok", xlSum ActiveSheet.PivotTables("PivotTable1") 结尾。 AddDataField ActiveSheet.PivotTables(_"PivotTable1").PivotFields("Sales"), "Sum of Sales", xlSum
  • @osygl 尝试使用您刚刚创建的pvt 对象和Set,使用With pvt,然后将所有PivotField 对象嵌套在里面
  • 谢谢,但在此之前我有一个问题。我刚刚重新打开文件,它以某种方式工作?它添加了第一个字段,但是当涉及到第二个字段时,它会重新生成相同的错误。为什么会如此不稳定?是因为记忆还是什么,还是和VBA有关?
  • @osygl 因为我需要了解您的全部代码范围,并查看您的数据透视表的示例数据(但这应该是一个新帖子,一旦您发送给我,我会很乐意提供帮助链接)
猜你喜欢
  • 1970-01-01
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多