Forum

VBA Filter If Crite...
 
Notifications
Clear all

VBA Filter If Criteria is Matched Filter If not Select Blank

12 Posts
2 Users
0 Reactions
279 Views
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

I would like a VBA macro so that if I wanted to see only a specific number (e.g. 100) filtered then have the filter in Pivot Table data must filtered to "100", otherwise filtered to “Blank”. If the 100 is not found in the Data then select “Blank”.

Example

Data

100

101

102

103

104

Blank

 
Posted : 18/09/2020 6:58 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Richard,

There's not enough info here for me to understand what you are trying to do.

If you want to filter a pivot table to only show 100 then you can use the PT filters?  Why use VBA?

Please supply a workbook with data that can be used to illustrate your situation and desired results.

Regards

Phil

 
Posted : 18/09/2020 7:43 pm
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

Hello Philip - Please see attached sample. I put some comments into the file.

 
Posted : 18/09/2020 8:39 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Richard,

no file attached.

A video showing how to attach a file can be found here https://www.myonlinetraininghub.com/excel-forum/forum-rules-and-guides

Regards

Phil

 
Posted : 18/09/2020 8:44 pm
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

Hello Philip hopefully its attached now.

 
Posted : 18/09/2020 11:37 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Thanks Richard.  So what you are saying is that if you have a company '200' that isn't in the source data, you want the pivot table to set the filter to 'blank' because '200' isn't in your source?

Where are you getting the value '200' from?  You haven't explained the full picture and I can't write any code without knowing details.  Where would the code get the values from to use as PT filters?

Regards

Phil

 
Posted : 19/09/2020 12:26 am
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

Hello Philip

Yes I want to set the filter in the Pivot Table to 'blank' because '200' isn't in your source data. 

The 200 is just an example. Basically wanted to show a data table and have an individual pivot for each company.

Data Tab has the two data fields: Column A = Company Number, Column B = Sales Data.

I then want a few Pivot Tables lets say for Company 100 through 200 in separate pivots. If it finds the match to the company codes that is set/hard coded then use the company code number as the filter. If there is no match to the assigned company code for each pivot then select the blank (e.g. 106  to 200 would be blank for the pivot filter since they are not in the Data set).

Sorry if I'm not explaining everything clearly.

Richard

 
Posted : 19/09/2020 12:36 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Richard,

I'm still unsure as to where the values for the filter are coming from, specifically for companies that aren't in your source data. 

I can write some code that will look through your source data and change the PT filter for every company it finds, but I don't see how or why it would set the filter to a company that isn't in the source data.  Why do you want this to happen?  Where would the VBA code get the company values from? If it's from the source data then why would the code try to filter for a company that doesn't exist?

Perhaps all you need is 'Show Report Filter Pages...' ?

Click into one of your pivot tables, then from the Ribbon on the PivotTable Analyze tab (I'm using Office 365) click the drop down arrow beside 'Options' in the PivotTable group (the left-most area of the Ribbon) - see attached image.

Then click on 'Show Report Filter Pages...' and from the pop-up window click OK. 

This creates a sheet with a PT for every company in the source data.

Regards

Phil

srptf.png

 
Posted : 19/09/2020 2:13 am
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

Hi Matt,

Appreciate all the help and time you have given.

The "Data" can change month to month but I want a specific Pivot to populate only if a certain company code is found, in this case 200.

This is the VBA code that I was using to set the Pivot data to filter on company code 200:

ActiveSheet.PivotTables("TBPivotTable").PivotFields("Company").ClearAllFilters
ActiveSheet.PivotTables("TBPivotTable").PivotFields("Company").CurrentPage = _
"200"

Since the Data set didn't contain any company code 200 data the filter defaulted to "ALL". Is there VBA logic to say is company code 200 is not found the pivot would select the Blank instead?

Does this make more sense what I'm asking?

 
Posted : 19/09/2020 7:32 am
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Richard,

You can trap the error that is generated when you try to set the filter to a value that isn't in the source data.  When this occurs you then set the filter to (blank)

ActiveSheet.PivotTables("TBPivotTable").PivotFields("Company").ClearAllFilters
On Error Resume Next
ActiveSheet.PivotTables("TBPivotTable").PivotFields("Company").CurrentPage = "200"
If Err.Number > 0 Then
ActiveSheet.PivotTables("TBPivotTable").PivotFields("Company").CurrentPage = "(blank)"
End If
On Error GoTo 0

Regards

Phil

P.S. Matt?

 
Posted : 19/09/2020 8:57 am
(@notdodo18)
Posts: 6
Active Member
Topic starter
 

THANK YOU SO MUCH THIS IS PERFECT!

Really appreciate all the time and help Philip.

 
Posted : 19/09/2020 3:11 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

no worries

 
Posted : 19/09/2020 5:42 pm
Share: