首先,让重构你的代码是这样的。这样做应该使调试更容易。
Sub Test()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Application.DisplayStatusBar = False
Application.DisplayAlerts = False
Dim pi as PivotItem
Dim pt as PivotTable
Dim ws as Worksheet
Set ws = ActiveSheet ' It is best not to use activesheet and to instead
' go through "ThisWorkbook.Sheets("") or something similar.
' This loop will allow us to prevent pivot tables from updating automatically
' Doing this should speed up your code dramatically.
For each pt in ws.PivotTables
pt.ManualUpdate = True
Next
With ws.PivotTables("CampaignAnalysis").PivotFields("Campaign Code")
.ClearAllFilters
' This loop right here is going to cause an issue. If there is ever an instance
' where all pivotitems would be hidden by this loop, then you will encounter an error.
' It is best to set at least one item visible, and then loop through the items to set
' the other items to hidden.
For Each pi In .PivotItems
.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("MailCount").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("AgeRange").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("DrivablePort").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pt.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("CruiseName").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("CampaignName").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next
End With
With ws.PivotTables("Itinerary").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next pi
End With
With ws.PivotTables("Segment").PivotFields("Campaign Code")
.ClearAllFilters
For Each pi In .PivotItems
pi.Visible = WorksheetFunction.CountIf(ws.Range("CA_Range1"), pi.Name) > 0
Next pi
End With
'**-- so I would like the ActiveSheet.PivotTables("Segment").PivotFields("ReportDate") to equal a referencing calculated date cell on the same sheet e.g. cell Z4 = 12/02/2017**
For each pt in ws.PivotTables
pt.ManualUpdate = False
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = False
Application.DisplayStatusBar = True
Application.DisplayAlerts = True
End Sub
在你的代码中的注释指出,该方法使用的是隐藏PivotItems会遇到的问题。在过滤pivotfields时,您将在尝试隐藏最后一个pivotitem时收到错误。例如,如果您有5个物品可见,并且共有10个物品,但是在循环过程中,前5个物品将循环并且它们都设置为无法识别,那么无论6-10应该在第5个物品上遇到错误可见。
从我所知道的情况来看,您正在将所有数据透视表过滤为“广告系列代码”中的相同项目。为什么不使用切片机来完成这项任务?它处理这种过程要好得多。
确保使用此过滤器至少可以看到一个MC。可能发生的情况是,当您的代码尝试隐藏列表中的最后一项时,将会遇到错误。没有自己运行代码,这是我最好的猜测。 –
感谢您的反馈布兰登。我设法让它工作,所以我很欣赏这种见解。目前我遇到的问题是试图更新一个数据透视表中的两个字段。这两个字段是一个范围(工作),但第二个字段是引用回到另一个计算的单元格的日期 –
因此,在这里,“Segment”中有另一个名为“ReportDate”的PivotFields,我试图引用计算日期细胞在这张纸例如A1,但无法使代码正常工作。我将如何将第二个PivotField集成到代码中? –