Solved

Updating Pivot Table within VBA

Posted on 2016-10-17
5
35 Views
Last Modified: 2016-10-18
All,

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.

Many thanks
Rob H

On Error Resume Next
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
With ActiveSheet.PivotTables("PivotTable1").PivotFields("EPP/ESP")
         .PivotItems("ESP").Visible = True
         .PivotItems("EPP").Visible = False
         .PivotItems("PSS").Visible = False
         .PivotItems("(blank)").Visible = False
End With

Open in new window

0
Comment
Question by:Rob Henson
  • 2
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41847727
HI,

1 You only need it once

and capture errors again use
On Error Goto 0 ' normal error handling
'or
On Error Goto ErrorHandler 'userdefined

Open in new window

Regards
1
 
LVL 17

Expert Comment

by:xtermie
ID: 41847848
On Error Resume Next would also work ok but there will be no error capturing
0
 
LVL 32

Author Comment

by:Rob Henson
ID: 41848026
@xtermie - "On Error Resume Next" is what I already have so that it ignores the potential errors caused when filtering the pivot table for values that don't exist.

@Rgonzo - I want to reset the "Error Capture" after the pivot refresh so that any further errors are captured. Will using "On Error Goto 0" work, without causing an error of its own as I don't have a line 0 for it to goto.

Thanks
Rob
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41848051
it does not create an error
On Error Resume Next
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
With ActiveSheet.PivotTables("PivotTable1").PivotFields("EPP/ESP")
         .PivotItems("ESP").Visible = True
         .PivotItems("EPP").Visible = False
         .PivotItems("PSS").Visible = False
         .PivotItems("(blank)").Visible = False
End With
On Error Goto 0

Open in new window

for reference
https://msdn.microsoft.com/en-us/library/5hsw66as.aspx
0
 
LVL 32

Author Closing Comment

by:Rob Henson
ID: 41848113
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now