How can I filter a slicer for one item through VBA? - excel

My slicer contains over 100 cities and I'd like to filter the slicer through VBA on one city at a time (in order to print the region of the chart into a Powerpoint). The trouble is that I think I would have to list every other city and show them as "Selected = False". I need a code block where I only specify the city/cities I and the rest of the slicer options are "Selected = False" by default.
I've recorded the macro code below and I've searched the net for a solution to this problem but have come up empty-handed.
With ActiveWorkbook.SlicerCaches("Slicer_City")
.SlicerItems("New York").Selected = True
.SlicerItems("Chicago").Selected = False
.SlicerItems("Trenton").Selected = False
.SlicerItems("Atlanta").Selected = False
.SlicerItems("Houston").Selected = False
.SlicerItems("Los Angeles").Selected = False
' there are 100 more cities to list if I just wanted "New York" to be selected
'in the slicer
End With
I need code where I should only specify the selection (New York) and not declare every other city as "Selected = False".

The quickest way is to make the pivotfield of interest a pagefield, and then iterate through the items and set the .CurrentPage property of the PivotField to each in turn, as this automatically turns the other items to FALSE. If you don't want the PivotField in the Page Fields area, then you can still do this quickly by setting up a 'Master' PivotTable somewhere out of sight, putting the field of interest in the master as a PageField, connecting the Master PivotTable to PivotTable1 ('Slave') with a Slicer, and then changing the .CurrentPage property of the Master, which will then instantly filter the .Slave via the Slicer.
See my answer at VBA code to filter a pivot table based on the value in a Cell that has some efficiency tips for this kind of thing, as well as links to other answers containing code.

Related

VBA problem with object in get date from pivot

I wanted to get data from a pivot table using the getdate pivot function. However, I get "Application - definied or object - definied error" . I want to take "Suma końcowa" and paste it in the E20 field.
Dim ptAs PivotTable
Set pt = wbMe.Sheets("RES_MAT").PivotTables("Tabela przestawna")
wbMe.Sheets("RES_MAT").Range("E20") = pt.GetPivotData("Suma z WYCENA_PLN", "Suma końcowa").Value
If you just specify the name of the field, that should return the total,,,
Range("E20")=pt.GetPivotData("Etykiety wierszy")

controlling report filters of powerpivot using excel VBA

I had 10 power pivot tables which report filters needs to be controlled by a cell reference using excel VBA in Excel-2010
The same thing works for normal pivot table .
NewCat = Worksheets(6).Range("G12").Value
Set pt1 = Worksheets(5).PivotTables("PivotTable3")
Set Field1 = pt1.PivotFields("[Lead_f].[Lead_Generation_Month].
[Lead_Generation_Month]")
With pt1
Field1.CurrentPage = NewCat
pt1.RefreshTable
End With
Assuming that the main idea is having all 10 pivots in sync, the non VBA way to this would be to add a slicer to one of the pivot tables for filtering. Then go to each of the other pivots and set the filter connection to your slicer.

Selecting a measures value with slicers in PowerPivot

I'm using PowerPivot in Excel 2010 to analyse some data. I have a situation where I have several boolean measures evaluating whether or not a condition is met. My Pivot table looks like this:
ROW LABEL VALUE CONDITION_MET
-----------------------------------
AAAA 10.5 TRUE
AAAB 9.5 FALSE
AAAC 11.29 TRUE
The measure to determine whether or not the criteria is met is non-trivial so I can't get users to select the right combination of groups in slicers to find the data.
Ideally I want a solution that allows me to have a slicer that looks like below:
SLICER
-----
TRUE
FALSE
This filters my pivot table accordingly to just the records that match.
Anyone got any ideas? I'm reading about disconnected slicers but not making much progress.
Disconnected slicers is one way to tackle this problem.
Lets say you create a 2 row table called 'TrueFalse' with a column called 'tf' and rows TRUE and FALSE.
You can then write a measure that counts the number of rows in that table that equal the value of the measure - if you then slice on the 'TrueFalse' table the option that you haven't selected will return BLANK() so will not be displayed.
The measure could look something like this:
=CALCULATE (
COUNTROWS ( truefalse ),
FILTER ( truefalse, truefalse[TF] = [Measure] )
)

Use current AutoFilter to load Data-Validation

I have a situation where i am using two Data-Validation cells which are loaded with data from one column (each) from the data table.
Table
market; suprv; store; ....
Data-Validation
cellDV1 - Unique Values from Table[market]
cellDV2 - Unique Values from Table[suprv]
Both cellDV1 & cellDV2 are preloaded with their respective unique values when the user views the worksheet.
When the user selects a value from cellDV1 then it AutoFilters the Table with the selected value. I then want to reload cellDV2 with the refined results visible in Table.
Problem i am having is that it is loading all of the Unique Values from that column/field in the Table and not the AutoFiltered results.
Usage:
LoadDataValidation Range("Table[suprv]"), Range("cellRange")
LoadDataValidation:
Dim str As String
str = DistinctValues(srcrng)
Dim val As Validation
Set val = Range(destrng.Address).Validation
val.Delete
val.Add xlValidateList, xlValidAlertStop, xlBetween, str
Any ideas, how to only select the Filtered results instead of the whole dataset in the Table?
I haven't worked with Tables very much, but can you apply the SpecialCells(xlCellTypeVisible)property to the Range
So Range("Table[suprv]").SpecialCells(xlCellTypeVisible) as your first argument in LoadDataValidation

Moving date period filter in Excel 2010 pivot-table

Is it possible to set some kind of filter for a moving date period?
For example one of the DB views I'm trying to replicate in my pivot-table has this filter :
DATEDIFF(day, dateColumn, GETDATE()) <= 90
So basically I always want to display the last 90 days of whataver data there is in the cube table.
Is this possible?
The answer to this question is here :
http://blogs.socha.com/2010/05/sliding-date-ranges-with-excel-2010.html
Example for a moving period of 30 days :
Select a cell inside a pivot table bound to the cube so that the PivotTable tools are available
Click the Options tab on the ribbon under the PivotTable Tools section:
Click the Fields, Items & Sets drop-down in the Calculations section of this ribbon tab
Click Manage Sets… in the drop-down
Click New… and then Create Set using MDX…
Enter a name for this set in the Set name text box
Enter the MDX expression that defines the date range
Click OK
Filter(
[Date].[Date].[Date],
[Date].[Date].CurrentMember.Member_Value < Now()
AND [Date].[Date].CurrentMember.Member_Value >= DateAdd("d", -30, VBA![Date]())
)

Resources