刷新数据透视表的运行时错误1004

时间:2015-12-29 10:42:27

标签: excel vba excel-vba refresh pivot-table

我创建了一个宏,它会刷新工作表中的所有数据透视表。相同的代码在一张纸上工作,但在另一张纸上不起作用。我尝试在调试模式下运行它,它适用于前几个表并且在中间的一个表面上有错误:

  

运行时错误'1004':PivotTable类的RefreshTable方法   失败。

此表中有20个数据透视表,但在它工作的那个数据表是26.你的帮助将非常感谢。

Sub Refresh_Pivots2()

Dim PL As PivotTable

Worksheets("Incidents Pivots").Activate

    For Each PL In ActiveSheet.PivotTables

    PL.RefreshTable

    Next PL

End Sub

我刚尝试了另一个代码

Sub Refresh_Incidents()

Worksheets("Incidents Pivots").Activate

Dim A

A = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20")

For i = 0 To 19

ActiveSheet.PivotTables(A(i)).RefreshTable

Next i

End Sub

如果我从i = 0开始它会出错,但是当它切换到i = 1时它会起作用。任何想法这个表有什么问题。我可以手动刷新它。

4 个答案:

答案 0 :(得分:2)

正如Rory所说,一个表的源数据有问题,它使用A1而不是R1C1表示法。我在Excel Option-> Formulas-> R1C1参考样式中更改了它,现在宏工作了。

答案 1 :(得分:0)

您是否检查了数据透视表数据源。也有一段时间由于数据库缓存而发生这种问题。在刷新枢轴之前调用以下代码。

Sub PivotCacheClear()
    Dim pc As PivotCache
    Dim ws As Worksheet
    With ActiveWorkbook
        For Each pc In .PivotCaches
            pc.MissingItemsLimit = xlMissingItemsNone
        Next pc
    End With
End Sub 

答案 2 :(得分:0)

我有一个代码可以自动调整数据透视表中的数据,然后刷新数据透视表,可能它可以阻止你:

Sub AdjustPivotDataRange(ByVal SheetName As String)

Dim Data_sht As Worksheet
Dim Pivot_sht As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String

  Sheets("All 2014 to Current Date").Visible = True
'Set Variables Equal to Data Sheet and Pivot Sheet
  Set Data_sht = Worksheets("All 2014 to Current Date")
  Set Pivot_sht = Worksheets(SheetName)

'Enter in Pivot Table Name
  PivotName = "PivotTable1"

'Dynamically Retrieve Range Address of Data
  Set StartPoint = Data_sht.Range("A6")
  LastRow = Data_sht.Cells(Rows.Count, 1).End(xlUp).Row
  LastColumn = Data_sht.Cells(LastRow, Columns.Count).End(xlToLeft).Column
  Data_sht.Activate
  Set DataRange = Data_sht.Range(StartPoint, Cells(LastRow, LastColumn))

  NewRange = Data_sht.Name & "!" & _
    DataRange.Address(ReferenceStyle:=xlR1C1)

'Make sure every column in data set has a heading and is not blank (error prevention)
  If WorksheetFunction.CountBlank(DataRange.Rows(1)) > 0 Then
    MsgBox "One of your data columns has a blank heading." & vbNewLine _
      & "Please fix and re-run!.", vbCritical, "Column Heading Missing!"
    Exit Sub
  End If

'Change Pivot Table Data Source Range Address
  Pivot_sht.PivotTables(PivotName).ChangePivotCache _
    ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange)

'Ensure Pivot Table is Refreshed
  Pivot_sht.PivotTables(PivotName).RefreshTable
  Sheets("Rolling 12-mo.").Select
  Sheets("All 2014 to Current Date").Visible = False
'Complete Message
  'MsgBox PivotName & "'s data source range has been successfully updated!"

End Sub

答案 3 :(得分:0)

'连接属性' =>'用法' =>'启用后台刷新',请确保它尚未被选中。