所以我从同一个源创建了两个数据透视表。我想要做的是创建一个SlicerCache
来连接这两个表。代码在 Excel 2010 和 Excel 2013 中成功运行,但在 Excel 2016 中失败。
以下是我创建第一个数据透视表的代码:
Sub OneYearTrend(wb As Workbook, wk As Worksheet)
With wb
Dim pvc As PivotCache
'Creat pivotcache
Set pvc = .PivotCaches.Create(xlDatabase, wk.Range("A1").CurrentRegion, xlPivotTableVersion14)
Dim pvt As PivotTable
'Create pivot table for new hire and separation
Set pvt = pvc.CreatePivotTable(TableDestination:=.Worksheets("Dashboard").Range("M6"), _
TableName:="OneYear_pvTbl_InOut")
With pvt
With .PivotFields("Type")
.Orientation = xlColumnField
.Position = 1
.PivotItems("Total Workforce").Visible = False
.PivotItems("NEW HIRES").Caption = "In"
.PivotItems("SEPARATIONS").Caption = "Out"
End With
With .PivotFields("JobGroup")
.Orientation = xlPageField
.Position = 1
.EnableMultiplePageItems = True
End With
创建第二个数据透视表的代码:
Set pvt = pvc.CreatePivotTable(TableDestination:=.Worksheets("Dashboard").Range("AA1"), _
TableName:="OneYear_pvTbl_Sparkline")
With pvt
With .PivotFields("ExtractDate")
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields("White"), " White", xlSum
.AddDataField .PivotFields("Black"), " Black", xlSum
.AddDataField .PivotFields("Hispanic"), " Hispanic", xlSum
.AddDataField .PivotFields("Asian"), " Asian", xlSum
.AddDataField .PivotFields("AmericanIndian"), " AmericanIndian", xlSum
.AddDataField .PivotFields("Male"), " Male", xlSum
.AddDataField .PivotFields("Female"), " Female", xlSum
.AddDataField .PivotFields("Total"), " Total", xlSum
End With
这是我遇到问题的代码: '创建隐藏的切片器以连接所有数据透视表
Dim SC As SlicerCache
**Set SC = .SlicerCaches.Add(pvt, "JobGroup")**
SC.Slicers.Add wk, , "JobGroup", "JobGroup", 50, 1000, 150, 200
SC.PivotTables.AddPivotTable .Worksheets("Dashboard").PivotTables("OneYear_pvTbl_InOut")
我收到错误消息:
运行时错误' -2147418113'(8000ffff)': 方法'添加'对象' SlicerCaches'失败
任何人都可以提供帮助吗?
答案 0 :(得分:0)
SlicerCaches.Add
。
尝试
Set SC = .SlicerCaches.Add2(pvt, "JobGroup")