excel real time refresh of filter - vba

In my Pivot Table in excel, I have a field List about the due date of some reports.
For example, 10/5/2016
My problem is, every day I have to choose the date so that I can see the report code, I don't want it to be this. What I want is, everyday I open the excel, the filter will automatically change from 10/5/2016 to 11/5/2016.
What function / VBA should I use?
Helps

If you want a VBA solution, add the following to the ThisWorkbook code.
Private Sub Workbook_Open()
Dim tDay As Date, pf As PivotField
tDay = Date
Set pf = Sheets("name of worksheet").PivotTables("pivot table name").PivotFields("insert the name of your filter here")
pf.ClearAllFilters
pf.CurrentPage = tDay
End Sub
Change the sheet name, pivot table name and the name of the filter.
If you need to do more VBA manipulation of pivot tables I suggest reading the following http://www.thespreadsheetguru.com/blog/2014/9/27/vba-guide-excel-pivot-tables.

Related

How to link cell value as a filter in PowerPivot Pivot Table in Excel?

I have two Pivot Table (PivotTable1 and PivotTable2) in Sheet PivotTables_Sheetand a cell that shows the year in Sheet YearInput. I tried to link the cell year (J4) in YearInput as a filter to all pivot tables in PivotTables Sheet.
But somehow my code doesn't work as expected. Anybody knows how to correct this code?
This is the code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Target.Address = Range("J4").Address Then Exit Sub
Dim pt As PivotTable
Dim ptItem As PivotItem
On Error Resume Next
For Each pt In Worksheets("PivotTables_Sheet").PivotTables
With pt.PivotFields("Year for Sales")
If .EnableMultiplePageItems = True Then
.ClearAllFilters
End If
Set ptItem = .PivotItems(Target.Value)
If Not ptItem Is Nothing Then
.CurrentPage = Target.Value
End If
End With
Next
End Sub
Note: I use Excel 2010 and built the pivot tables using PowerPivot.
One way to do this without VBA is to use a slicer which is common between all the pivots. Then one year is changed on one, it will also update the other pivot tables.
This is by first clicking on the pivot, which will then show on the ribbon an Analyze option. Under that there is an insert slicer, which you would do for year.
Afterwards you would go to your other pivots and instead do filter connection, and select your year slicer.
Note that the year slicer can then just be moved to a hidden sheet if you do not want the end user to see it, it will still do its job of propagating the year across all the pivots that are referencing it.

Filtering values from OLAP Cube Pivot Table

I have a pivot table with a lots of date that I can choose in the filter (more than 1000 differents values).
What I need is to filter these values which are Dates from a user input.
example: Start date is 01-03-2018 to End date 20-03-2018
I need to do this through VBA code.
I'm not sure if actually the date values shown in the PT are really declared as date variable.
These values come from the Time dimension of the cube.
Here are some screenshots.
Image 1
Image 2
I made a try by using Wroksheet_Change but that works fine when I enter one date only (for example 2018-03-20) and the PT updates correctly, but i don´t know how to do it for a range of dates. Here is the code I used for one date only
Private Sub Worksheet_Change(ByVal Target As Range)
Dim newDate As String
newDate = Worksheets("KPICuboSIGP").Range("M4").Value
If Target.Address = "$M$4" Then
ActiveSheet.PivotTables("KPI-BPOP").PivotFields("[Time].[Date].[Date]"). _
VisibleItemsList = Array("[Time].[Date].&[" & newDate & "T00:00:00]")
End If
End Sub
I turned on the macro recorder too, and this is what it recorded.
ActiveSheet.PivotTables("KPI-BPOP").PivotFields("[Time].[Date].[Date]"). _
VisibleItemsList = Array("[Time].[Date].&[2018-01-21T00:00:00]", _
"[Time].[Date].&[2018-01-22T00:00:00]", "[Time].[Date].&[2018-01-23T00:00:00]", _
"[Time].[Date].&[2018-01-24T00:00:00]", "[Time].[Date].&[2018-01-25T00:00:00]", _
"[Time].[Date].&[2018-01-26T00:00:00]")
Those dates recorded should be selected automatically once I have the range from the user. Maybe I need to cycle through the options in the filter with For cycle, but I don´t know how to do it.
I'm pretty much a rookie in VBA
Can anyone help me with this.
Thanks for any advice
You can use the Labels filter to accomplish this. The problem is that the Labels filter isn't available to fields in the Filters part of a PivotTable. So you need to whip up a second PivotTable with nothing but the Dates field in it as a row field, then connect that PivotTable to the original PivotTable with a Slicer, and then programatically set a Label filter on the second PivotTable as I outline at Use VBA to select and deselect multiple slicer items (OLAP data)

Trying to use VBA to filter my Pivot Table based on Year and Period values in cells D2 and E2

I am trying to filter my Pivot Table using VBA so it will filter based on a Period and a Year in cells D2 and E2 respectively. I have the below code which is to filter by the year and works without having the Fiscal Period in the Columns but when I put the Fiscal Period in the columns it debugs at the Field.ClearAllFilters and Field.CurrentPage = Year lines. Can someone please help me find a fix for this?
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Intersect(Target, Worksheets("CY PT").Range("E2")) Is Nothing Then Exit Sub
Dim PT As PivotTable
Dim Field As PivotField
Dim Year As String
Set PT = Worksheets("CY PT").PivotTables("CY PT")
Set Field = PT.PivotFields("Fiscal Year")
Year = Worksheets("CY PT").Range("E2").Value
With PT
Field.ClearAllFilters
Field.CurrentPage = Year
PT.RefreshTable
End With
End Sub
The .CurrentPage method only applies to PageFields. Sounds like you are trying to filter RowFields. So your options are:
Change your fields to be PageFields; or
Use a Slicer; or
Use a PageField filter dropdown mascarading as a Data Validation
dropdown (like I outline at
http://dailydoseofexcel.com/archives/2014/08/16/sync-pivots-from-dropdown/
); or
Iterate through the PivotItems collection for each field, and set the
.Visible status of everything but the selected items to FALSE.
If you choose the last option, you need to be aware this can take a while. See the code I posted at Filtering a Pivot Table Based on Variable Range to get an understanding of how to do this efficiently. Also give my post at http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/ a read.

Excel VBA to filter Pivot Table and Pivot Chart for previous day - Pivot Filter Field

Here is my problem, every bit of code, every alteration, every type, doesn't work. I'm using Office 360 at my work site (up to date), so it's excel 2016 and VBA 7.1.
What I'm looking to do is automate our end of shift reports. Here's the process:
We enter data into an excel sheet (Log) every hour. At the end of the day, at 5:00 AM, we save and close that log, open another excel sheet that IMPORTS the data into power pivot, and displays it on a PivotTable (formatting for printing for our bosses), and we choose the filter for the previous date using the filter drop down, and print it. We do this with three (3) reports: 2 PivotTables, and 1 PivotChart. Power Pivot imports ALL of the data from the Log sheet to reformat it for printing.
I've successfully managed to get and rewrite the code (beginner at this) for the automation process of: auto saving the log, closing the log, opening the Report workbook, refreshing the data, and printing the data, then closing the report. The only part I'm now missing is the auto-filtering.
The code I've tried is vast, but here's an example of what I've tried recently (I've erased and re-copied so many codes...)
Sub Filter_PivotField()
'Description: Filter a pivot table or slicer for a specific date or period
'Source: excelcampus.com/vba/filter-pivot-table-slicer-recent-date-period
Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As String
Dim pi As PivotFields
'Set the variables
sSheetName = "EOS Report"
sPivotName = "PivotTable1"
sFieldName = "Date"
sFilterCrit = "xlDateYesterday"
'sFilterCrit = ThisWorkbook.Worksheets("EOS Report").Range("O1").Value
With ThisWorkbook.Worksheets(sSheetName).PivotTables(sPivotName).PivotFields(sFieldName)
'Clear all filter of the pivotfield
.ClearAllFilters
'Loop through pivot items of the pivot field
'Hide or filter out items that do not match the criteria
For Each pi In .PivotFields
If pi.Name <> sFilterCrit Then
pi.Visible = False
End If
Next pi
End With
End Sub
To no avail....
When I record a macro doing the manual filter, I get this:
Sub manualfilter()
'
' manualfilter Macro
'
'
ActiveSheet.PivotTables("PivotTable1").PivotFields( _
"[Bi-Hourly Report].[Date].[Date]").VisibleItemsList = Array( _
"[Bi-Hourly Report].[Date].&[2016-09-28T00:00:00]")
End Sub
But it fails when I try to re-run the same macro that I just recorded (after changing the date back). I've enabled and disabled multiple selection option, etc.
Not to mention, trying to auto-filter a chart is a nightmare because tables, yea there's tons of articles on it, but charts? not much comes up on researching.
Here's images of the filter button, because almost everything I've researched is to sort the COLUMN of the Table, not the filter itself with a PivotTable.
Table Filter
Chart Filter
I cannot post the actual excel spreadsheets as they are proprietary property of the company, but I can replicate the format with false data if needed.
check this out.
Dim prev_date As String
prev_date = Month(Date - 1) & "/" & Day(Date - 1) & "/" & Year(Date - 1)
Thisworkbook.Sheets("Sheet1").Activate
'change this line with your sheet where pivot table is present. Change Sheet name.
ActiveSheet.PivotTables("PivotTable1").RefreshTable
ActiveSheet.PivotTables("PivotTable1").PivotFields("Date").CurrentPage = prev_date

select range for Pivot in VBA

I want to create a macro for Pivot table with dynamic range. Pivot table is already made , I am just want to make Dynamic range selection macro. Range is A2:L
Please help Me how to create macro for just range selection and refresh All.
Sum of Amount in Document Currency Document Currency
Company Code Trading Partner BGN EUR
BG05 CH10 272,326.08 1,618.00
HS03 RS31 1,618.00
Below code is Woking for me. Got resolution.
Sub ChangeCaches()
Dim PT As PivotTable
Set PT = Sheets("OPEN ITEMS DETAIL").PivotTables("PivotTable1")
PT.SourceData = Sheets("Download").Range("A1").CurrentRegion.Address(True, True, xlR1C1, True)
End Sub