まず第一に、このようなあなたのコードをリファクタリングすることができます。そうすることで、デバッグが容易になります。
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を非表示にする方法は問題に遭遇します。ピボットフィールドをフィルタリングするときに、最後のピボットアイテムを非表示にするとエラーが発生します。たとえば、5つのアイテムが表示され、合計10のアイテムがある場合、ループ中に最初の5つがループされ、すべてが非表示に設定されている場合、6番目から10番目のアイテムに関係なく、5番目のアイテムでエラーが発生します。見えるようにする。
私が知る限り、すべてのピボットテーブルを「キャンペーンコード」内の同じアイテムにフィルタリングしています。なぜこの作業にスライサーを使用しないのですか?この種のプロセスを処理するための装備がはるかに優れています。
このフィルタを使用して少なくとも1つのMCが表示されていることを確認してください。何が起こっているかは、コードがリスト内の最後の項目を隠そうとしたときにエラーが発生する場合です。自分でコードを実行することなく、これは私の最高の推測です。 –
フィードバックBrandonに感謝します。私はそれが働くように管理したので、私は洞察に感謝する。現時点で問題となっているのは、1つのピボットテーブルで2つのフィールドを更新しようとしていることです。 2つのフィールドは範囲(動作する)ですが、2番目のフィールドは別の計算セルに参照する日付です –
ここでは、 "ReportDate"と呼ばれる "Segment"にPivotFieldsがあり、計算された日付を参照しようとしていますこのシート上のセルA1が、コードを動作させることはできません。 2番目のPivotFieldをコードにどのように統合すればよいですか? –