VBA Pivot Field Filter Error

Hi there, I have written a VBA procedure (well multiple as its very large code) which basically re-creates a large number of Pivot tables from the source data sheet so as the source data changes, I delete & rebuild the Pivots.  All of this is working fine.  

However, in the source data, there is a column called "Region" and there are 5 main regions consisting of CEE, DACH, UK/IE, NL & CENTER.  I have noticed towards the very end of coding, that in some cases, this approach is generating an error and its where the source data for the Pivot, does not actually contain a specific region

For example, if I copy the VBA for the multiple Pivots I created for NL region, I have been setting the region filters in VBA as follows:-

'Add Region Filter
With .PivotFields("Region")
        .PivotItems("CEE").Visible = False
        .PivotItems("DACH").Visible = False
        .PivotItems("UK/IE").Visible = False
        .PivotItems("CENTER").Visible = False
        .PivotItems("(blank)").Visible = False
End With

The above works fine so long as in the source data for the Pivot I am creating, all of the values above are present, if for example, in the Pivot source data, there is no data for "UK/IE", the VBA throws an error, rather than simply skipping over the appropriate line of code.

Is there an easy / quick resolution sticking with the above structure, to get VBA to ignore a filter request if the data does not exist in the source ( I do not really want to re write all of the code - it really is quite massive) ?

Thanks in advance
Dave
David EdwardsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsDistinguished Expert - 2017Commented:
you can add as below;
On Error Resume Next
'Add Region Filter
With .PivotFields("Region")
        .PivotItems("CEE").Visible = False
        .PivotItems("DACH").Visible = False
        .PivotItems("UK/IE").Visible = False
        .PivotItems("CENTER").Visible = False
        .PivotItems("(blank)").Visible = False
End With
On Error Goto 0

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David EdwardsAuthor Commented:
thanks - that seems simple enough :)  - will the "Goto 0" part work ok if this filter logic appears multiple times within a proc?
0
ShumsDistinguished Expert - 2017Commented:
You need to start with Error Resume Next before every loop and end with On Error Goto 0
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rob HensonFinance AnalystCommented:
I had a similar issue with VBA refreshing Pivot Tables.

My Row Value label was Cost Type and was made up of 3 types of Cost (Support, Procurement and Consultant). I was refreshing the table for each cost type to then further generate details for accrual journals and came across this issue.

Lets imagine I ran the report for Support and the data concerned including some Support costs and the journal data was suitably created. However, when I then ran the report for Procurement there was no Procurement costs in the data set. When trying to set the pivot table to only show Procurement it fell over because it couldn't do it so I used the "On Error Resume Next" statement as suggested. This left the Pivot Table showing the Support data and then onward processing created erroneous Journal data because the pivot showed the incorrect data.

To get round this I implemented a check count on the sheet with the pivot. I set a cell to the cost type value being refreshed and in the next cell a count of that cost type in the particular pivot column. The VBA process then checked that the count was greater than zero before processing the data in the pivot table.

Hope that helps
Thanks
Rob
0
David EdwardsAuthor Commented:
thanks this worked perfectly and I will check the rest of my code and add in Error Resume Next before every loop and end with On Error Goto 0
0
Rob HensonFinance AnalystCommented:
Have you noted my comment.

When you use "On Error Resume Next" to skip the Pivot Filter, the Pivot Table will not change and will still show the data from the previous refresh; ie one of the Regions that does exist in that Pivot.
0
ShumsDistinguished Expert - 2017Commented:
No comment has been added to this question in more than 14 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:
Split: Shums (ID: 42052688) and Rob Henson (ID: 42052688)

If you feel this question should be closed differently, post an objection and a moderator will read all objections and then close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.