2017-03-04 59 views
0

在运行下面的代码“类型不匹配”错误出现当宏到达这条线:错误的枢轴缓存语法

​​

如何纠正代码中的错误?

Sub Pivot() 
    Dim PSheet As Worksheet 
    Dim DSheet As Worksheet 
    Dim PCache As PivotCache 
    Dim PTable As PivotTable 
    Dim PRange As Range 
    Dim LastRow As Long 
    Dim LastCol As Long 
    Application.DisplayAlerts = False 
    Worksheets("Summary").Delete 
    Sheets.Add Before:=ActiveSheet 
    ActiveSheet.Name = "Summary" 
    Application.DisplayAlerts = True 
    Set PSheet = Worksheets("Summary") 
    Set DSheet = Worksheets("Content") 
    LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row 
    LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column 
    Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol) 
    Sheets("Content").Select 
    Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange).CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), TableName:="ERA_Dashboard") 
    Set PTable = PCache.CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), TableName:="ERA_Dashboard") 
    With ActiveSheet.PivotTables("ERA_Dashboard").PivotFields("Issue Status") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    With ActiveSheet.PivotTables("ERA_Dashboard").PivotFields("Issue Status") 
     .Orientation = xlDataField 
     .Position = 1 
     .Function = xlCount 
     .NumberFormat = "#,##0" 
     .Name = "Issue Status" 
    End With 
End Sub 
+1

您试图分配数据透视表PCACHE被定义为一个PivotCache。 – TnTinMn

+1

这不是VB.NET代码,这是一个不好的标题。看到[问]并采取[Tour] – Plutonix

回答

0

没有必要删除和重新创建“摘要”表,只需使用下面的代码。下面的代码将与更新PCache“摘要”工作表中创建(或更新)PTable枢轴表。

代码

Option Explicit 

Sub Pivot() 

Dim PSheet As Worksheet 
Dim DSheet As Worksheet 
Dim PCache As PivotCache 
Dim PTable As PivotTable 
Dim PRange As Range 
Dim LastRow As Long 
Dim LastCol As Long 

Set PSheet = Worksheets("Summary") 
Set DSheet = Worksheets("Content") 

With DSheet 
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 
    Set PRange = .Range("A1").Resize(LastRow, LastCol) ' set data range for Pivot Table 
End With 

'Set the Pivot Cache 
Set PCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PRange) 

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

On Error GoTo 0 
If PTable Is Nothing Then 

    ' create a new Pivot Table in "Summary" sheet 
    Set PTable = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Range("A1"), TableName:="ERA_Dashboard") 

    With PTable.PivotFields("Issue Status") 
     .Orientation = xlRowField 
     .Position = 1 
    End With 
    With PTable.PivotFields("Issue Status") 
     .Orientation = xlDataField 
     .Position = 1 
     .Function = xlCount 
     .NumberFormat = "#,##0" 
     .Name = "Issue Status" 
    End With 
Else 
    ' just refresh the Pivot cache with the updated Range 
    PTable.ChangePivotCache PCache 
    PTable.RefreshTable 
End If 

End Sub 

编辑1

Option Explicit 

Sub Pivot() 

Dim PSheet   As Worksheet 
Dim DSheet   As Worksheet 
Dim PTable   As PivotTable 
Dim PCache   As PivotCache 
Dim PRange   As Range 
Dim LastRow   As Long 
Dim LastCol   As Long 

Set PSheet = Worksheets("Summary") 
Set DSheet = Worksheets("Content") 

With DSheet 
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 
    LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column 

    Set PRange = .Range("A1").Resize(LastRow, LastCol) ' set data range for Pivot Table 
End With 

' Set the Pivot Cache 
Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PRange, Version:=xlPivotTableVersion14) 

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

On Error GoTo 0 
If PTable Is Nothing Then 

    ' create a new Pivot Table in "Summary" sheet 
    Set PTable = PSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=PSheet.Range("A1"), TableName:="ERA_Dashboard") 

Else 
    ' just refresh the Pivot cache with the updated Range 
    PTable.ChangePivotCache PCache 
    PTable.RefreshTable 
End If 

End Sub 
+0

我很抱歉,当我尝试运行您的代码时出现同样的错误。有什么我需要做我的工作簿? –

+0

@YuvanViky 3个问题:1.代码在哪一行中断? 2.您的'PRange'中有多少行和多列数据? 3.运行此代码时,“摘要”工作表是否存在?是否存在“ERA_Dashboard”? –

+0

谢谢!代码休息@集PCACHE = ActiveWorkbook.PivotCaches.Add(xlDatabase,普兰奇)时出现的错误“,‘类型不匹配’是在网页‘摘要’在那里已经ERA_dasboard是我的透视表的名称,请帮助我在此先感谢@shairado –