我正在尝试打印所选预算持有人的报告(从预算持有人表中选择),使用预算持有人名称输入切片器,然后更新各种数据透视表。 问题是代码选择切片机中的所有预算持有者而不是从表中选择的单个预算持有者。
Sub PrintPDFsSO()
Dim Lobj As ListObject
Dim Budholder As String
Dim Path As String
Dim x As Long, y As Long, Number_of_rows As Long
Dim SourceBk As Workbook
Dim SlicItem As SlicerItem, SlicDummy As SlicerItem, SlicCache As SlicerCache
Dim pt As PivotTable, wb As Workbook, ws As Worksheet
Set SourceBk = ThisWorkbook
Set Lobj = SourceBk.Sheets("BudHolders").ListObjects("BudHolderList")
Set SlicCache = SourceBk.SlicerCaches("Slicer_Budget_Holder")
For x = 1 To Lobj.DataBodyRange.Rows.Count 'Budget Holders held in BudHolderList Table
Dim BudHolders()
ReDim BudHolders(1 To Lobj.DataBodyRange.Rows.Count) 'as Budholders will only ever hold one budget hodler name, can this be simpified?
Dim Counter As Long
Counter = 1
If Not Lobj.DataBodyRange.Rows(x).EntireRow.Hidden Then
Budholder = Lobj.DataBodyRange(x, 3) 'Name of budget holder held in 3rd column of Budget Holder Table
BudHolders(Counter) = Budholder 'Budholders holds the budget holder name
Counter = Counter + 1
ReDim Preserve BudHolders(1 To Counter - 1)
' Trying to stop slicers/pivot tables calculating so code setting new filter on budget name doesnt get stuck - but not working
Application.Calculation = xlCalculationManual
For Each ws In SourceBk.Sheets
For Each pt In ws.PivotTables
pt.ManualUpdate = True
Next pt
Next ws
'Code to change budget holder in slicer to next budget holder in selection from Table
For y = LBound(BudHolders) To UBound(BudHolders)
With SlicCache
.ClearManualFilter 'clears all filters and shows all items in budget holder slicer
For Each SlicItem In .SlicerItems
If BudHolders(y) <> SlicItem.Value Then 'Tests if the slicer item matches the current a value of budholder
SlicItem.Selected = False 'Grinding to a virtual halt on this line as it 'calculates and populates pivot table report'
End If
Next SlicItem
End With
Next y
Application.Calculation = xlCalculationAutomatic
For Each ws In SourceBk.Sheets
For Each pt In ws.PivotTables
pt.ManualUpdate = False
Next pt
Next ws
'Use budholder name which will populate some graphs etc in workbook with new figures
SourceBk.Sheets("Graphs - Summary").Range("BudHolder_SG").Value = Budholder
'Do Printing, saving etc
End If
Next
End Sub
答案 0 :(得分:0)
你可以扭转逻辑并隐藏那些不想要的东西吗?以下代码基于从表中选取过滤器并应用于数据透视表。
注意:它会将所有表过滤器存储在一个数组中,然后循环此数组以将过滤器一次一个地应用于与数据透视关联的切片器。
您肯定希望使代码更加模块化,并分离成单独的函数/子函数,存储过滤器,循环数组和任何单独的操作,例如:循环数组时生成报告 在移动设备上,缩进可能有点偏差。
Option Explicit
Sub PrintPDFs()
Dim Lobj As ListObject
Dim BudHolder As String
Dim SlicItem As SlicerItem, SlicCache As SlicerCache
Dim SourceBk As Workbook
Dim x As Long
Set SourceBk = ThisWorkbook
'Picks up Table with budget holder details
Set Lobj = SourceBk.Sheets("BudHolders").ListObjects("BudHolderList")
'Picks up slicer which drives pivot tables in workbook
Set SlicCache = SourceBk.SlicerCaches("Slicer_Budget_Holder")
Dim BudHolders()
ReDim BudHolders(1 To Lobj.DataBodyRange.Rows.Count)
Dim counter As Long
counter = 1
For x = 1 To Lobj.DataBodyRange.Rows.Count
If Not Lobj.DataBodyRange.Rows(x).EntireRow.Hidden Then ''Applies to items selected (ie visible) in the Budget Holder Table
BudHolder = Lobj.DataBodyRange(x, 3)
BudHolders(counter) = BudHolder
counter = counter + 1
End If
Next x
ReDim Preserve BudHolders(1 To counter - 1)
For x = LBound(BudHolders) To UBound(BudHolders)
With SlicCache
.ClearManualFilter
For Each SlicItem In .SlicerItems
If BudHolders(x) <> SlicItem.Value Then
SlicItem.Selected = False
End If
Next SlicItem
End With
‘Rest of code to do print PDF reports etc
End Sub
这是表名为BudHolderList的地方,pivottable是pivottable 1,切片器名为Slicer_Budget_Holder。
表:
支点:
答案 1 :(得分:0)
通过使用其中一个数据透视表而不是使用切片器,我找到了一种解决方法。因为这些表都是连接的(即所有表都有预算持有者作为过滤器字段并通过切片器连接),所以当我在数据透视表中的数据透视表中更新预算持有者时,它将更新所有的数据透视表相同的PivotField值。
因此,在原始问题中替换切片器代码的代码就是:
With sheets ("BudgetHolder").PivotTables("PivotTable1").PivotFields("BudgetHolder")
.ClearAllFilters
.CurrentPage=Budholder
End With