首先,讓重構你的代碼是這樣的。這樣做應該使調試更容易。
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集成到代碼中? –