我想切片器连接到此处描述的使用方法多重透视表: http://dailydoseofexcel.com/archives/2014/08/05/slicers-and-slicercaches/
首先,我依次通过我的数据透视表,并为每一个切片:
wkbDash.SlicerCaches.Add(wksPivots.PivotTables(sPTName), sSlicerName). _
Slicers.Add wksSlicers, , sSlicerName, sSlicerName, 1, 1, 50, 100
然后,我遍历我的切片机,并尝试将其钩到每一个数据透视表
问题:我得到一个错误#424在这条线“对象需要”:
wkbDash.SlicerCaches(objSlicer).PivotTables.AddPivotTable
(wksPivots.PivotTables(varPTNames(i, 1)))
代码挂钩片来透视表:
' Declarations
Dim objSlicerCache As SlicerCache
Dim objSlicer As Slicer
Dim objPT As PivotTable
Dim varPTNames As Variant
Dim wksDefPivots As Worksheet
Dim wkbDash As Workbook
Dim i As Integer
' Initialize Variables
Set wkbDash = Workbooks(sDash)
Set wksDefPivots = Workbooks(sDash).Worksheets(sDefPivots)
varPTNames = wksDefPivots.Range("A2:A" & FindLastRow(wksDefPivots)).Value2
i = 0
' Procedure
For Each objSlicerCache In Workbooks(sDash).SlicerCaches
For Each objSlicer In objSlicerCache.Slicers
For i = LBound(varPTNames) To UBound(varPTNames)
wkbDash.SlicerCaches(objSlicer).PivotTables.AddPivotTable (wksPivots.PivotTables(varPTNames(i, 1)))
Next i
Next objSlicer
Next objSlicerCache
基于Bob的意见,我已经修改了循环尝试和挂钩的数据透视表的SlicerCache对象,而不是切片。 我仍然得到错误#424“对象需要”
修订循环:
For Each objSlicerCache In Workbooks(sDash).SlicerCaches
For j = LBound(varPTNames) To UBound(varPTNames)
objSlicerCache.PivotTables.AddPivotTable (wksPivots.PivotTables(varPTNames(j, 1)))
Next j
Next objSlicerCache
主要更新:我已经修改了代码,我非常接近。 新的代码序列:
1.创建1个PivotCache
2.使用PivotCache创建多个数据透视表
3.创建从第一数据透视表1 SlicerCache
4.添加额外的数据透视表SlicerCache
5.循环每一个数据透视表中SlicerCache并创建一个限幅器。
好消息:所有这些代码工作。 它创造了许多切片机,并且每个切片器将过滤所有的数据透视表
问题:所有的切片器仅过滤一个字段(见图像),而不是每个切片器providingng不同的字段来过滤。
码:
For Each objSlicerCache In wkbDash.SlicerCaches
objSlicerCache.Delete
Next objSlicerCache
sRF = Worksheets(sDefPivots).Range("B2").Value
sPT = Worksheets(sDefPivots).Range("A2").Value
Set objPT = Worksheets(sPivots).PivotTables(sPT)
Set objPF = Worksheets(sPivots).PivotTables(sPT).PivotFields(sRF)
Set objSlicerCache = wkbDash.SlicerCaches.Add(objPT, objPF.Name)
Set objPT = Nothing
Set objPF = Nothing
For Each objPT In Worksheets(sPivots).PivotTables
objSlicerCache.PivotTables.AddPivotTable objPT
Next objPT
For Each objSlicerCache In wkbDash.SlicerCaches
For Each objPT In objSlicerCache.PivotTables
objSlicerCache.Slicers.Add wksSlicers, , objPT.Name, objPT.Name, 1, 1, 50, 100
Next objPT
Next objSlicerCache
过滤器的屏幕截图