I have the following code as part of a bigger routine. The whole routine goes through a list of files and opens each one to copy out some data into a consolidated file. The data to be copied is the result of a Pivot Table so the pivot table needs to be refreshed.
The particular field in the Source Data has 4 options (EPP, ESP, PSS and (blank)) and I want to filter the PT to show only ESP. However, the source data may not have all options from the outset; ESP is almost definite and blank is more than likely but the other two are less likely now but are likely to be added at some point in the future. The field is a spend type and as each project gets into later stages the spend types will occur. When creating the pivot, if the other options are not available I cannot create the filter but on each refresh I need to check that the filter is still correct. The code below is taken from the recorded script of applying the filter to a project that I knew had all 4 spend types and then amended it.
I imagine I need the On Error line to allow the script to continue without falling over when the PT Field doesn't include the variable that I am trying to make Visible/Hidden. From previous work I understand the "On Error Resume Next" will enable the script to continue for all errors so I only need it once; or do I need to split the "With...End With" into 4 blocks to check each field option and put the On Error before each block of code. In addition, how do I reset the "Error flag" so that any errors after these lines are captured and potentially stop the code running?
As always, assistance is much appreciated.
On Error Resume Next
.PivotItems("ESP").Visible = True
.PivotItems("EPP").Visible = False
.PivotItems("PSS").Visible = False
.PivotItems("(blank)").Visible = False