Unable to get PivotFields property of the PivotTable class - vba

I'm trying to make a macro that changes the filters of several PivotTables (not all), but i'm getting an error on the PivotFields, here's a sample of my code:
Sheets("Sheet1").PivotTables("PivotTable" & pivot_counter).PivotFields( _
"[year].[year].[year]").VisibleItemList = Array("")
My questions are:
1- Why do we use PivotFields("[year].[year].[year]") when using VisibleItemList? Why do we have to repeat it and what's the meaning of it, couldn't anything about it anywhere.
2- What is supposedly wrong with this code? My pivot table has a field called "year" and the filter is set for a specific year (let's say 2013) and i wanted it change for all possible years.

Sub Tester()
ShowAll ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
End Sub
Sub ShowAll(pf As PivotField)
Dim pi As PivotItem
Application.ScreenUpdating = False
For Each pi In pf.PivotItems
pi.Visible = True
Next pi
Application.ScreenUpdating = True
End Sub

Related

Switch pivot table filter using VBA macro (button)

I need help with some rather easy macro, but I can't do that. So all I want is to switch between filters - I have two possibilities, by example two colours - black and white, and I only want to have one of them in the moment (I want to add button, so it will work as switch). Here is the situation
I've tried did it with conditional, of course it doesn't work, but hope it will helps you to understand my idea. If one filter is enable, the other one is disabled (so the objects connected with that obviously too).
Sub Makro5()
ActiveSheet.PivotTables("PivotTable3").PivotFields("colour"). _
CurrentPage = "(All)"
If ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
Then ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = True
.PivotItems("white").Visible = False
Else: ActiveSheet.PivotTables("PivotTable3").PivotFields ("colour")
.PivotItems("black").Visible = False
.PivotItems("white").Visible = True
End Sub
Something is wrong with If, because it's on red and I get "Complie error: Syntax error" info
This is what slicers are built for.
Put your cursor inside the the pivottable data area. Then goto insert > slicer > and choose colour. You will then have a slicer which will allow you to select either or. I don't know if you can disable multiselect in current excel version so user could press ctrl and select both. But this seems a simple way.
Or you could run code such as the following and then user just types in Black or White in to field and filter is applied:
Sub Test()
Dim wb As Workbook
Dim ws As Worksheet
Dim pvt As PivotTable
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet3")
Set pvt = ws.PivotTables("PivotTable3")
Dim pvtField As PivotField
Dim item As Long
Dim item2 As Long
Set pvtField = pvt.PivotFields("Colour")
pvtField.EnableItemSelection = False
End Sub
You need to have the Then on the same line as the If. (You can use _ to tell VBA that "code continues on next line")
You also don't have your And set up correctly, and you are making both "Black" and "White" visible before you check if they are visible?
Try this:
Sub ToggleColour()
With ActiveSheet.PivotTables("PivotTable3").PivotFields("colour")
If .PivotItems("white").Visible=True Then
.CurrentPage = "black"
Else
.CurrentPage = "white"
End If
End With
End Sub

VBA macro to change filters in pivot table

I'm trying do automate a daily report and therefore I want to create two buttons which change the filters of three pivot tables. In detail the buttons shall change the day which is shown. The first filters on yesterday the second one is a reset button do clear all filters and show all days.
The "Resest"-Button is working but the "Yesterday"-Button not.
At the moment the macro looks like that:
Private Sub CommandButton2_Click()
MsgBox ActiveSheet.Range("B1")
With ActiveSheet.PivotTables("Detail_Digital").PivotFields("Tag").CurrentPage = _
ACtiveSheet.Range("B1").Value
End With
End Sub
I've also tried PivotFilters.Add _ , Type:=xlDateYesterday but that isn't working either.
Any suggestions?
Try the code below, it should work, unless your "Date" is formatted differently between the Pivot's data source and Range("B1").
Note: try to avoid using ActiveSheet, instead use referenced objects. In the case below, replace Worksheets("Sheet1") with your sheet's name.
Code
Option Explicit
Private Sub CommandButton2_Click()
Dim PvtTbl As PivotTable
Dim PvtItm As PivotItem
' set the Pivot Table
Set PvtTbl = Worksheets("Sheet1").PivotTables("Detail_Digital")
With PvtTbl
.PivotFields("Tag").ClearAllFilters ' <-- clear all filters to "Tag"
'Debug.Print Worksheets("Sheet1").Range("B1").Value
For Each PvtItm In .PivotFields("Tag").PivotItems
If PvtItm.Name = Worksheets("Sheet1").Range("B1").Value Then
PvtItm.Visible = True
Else
PvtItm.Visible = False
End If
Next PvtItm
End With
End Sub

Excel VBA Error 1004 cant set visibility

I have a weird error with excel vba. I am trying to set the visibility to false inside a pivot table for date values. My code works fine on a dummy table, but it return an error (1004) on my real table.
Sub MultiItemPivotFilter2()
Dim PI As PivotItem
Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1").RefreshTable
For Each PI In Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1").PivotFields("date2").PivotItems
If DateValue(PI.Name) < DateValue(Sheets("Pivot_stocks_1").Range("J13").Value) Then
PI.Visible = False
Else
PI.Visible = True
End If
Next PI
End Sub
The error occurs in line PI.Visible = False
(Unable to set Visible property of the PivotItem class)
Maybe this would help you:
unable to set the visible property of the pivotitem class
Excel sometimes does not remove old entries from pivotcache on which pivot table is based.In that some item may be there which is not in source data but excel keeps it in cache for optimizing.Refreshing cache does not solve issue.Only way is remove the cache and recreate it.
You should always do your select true loop and then do a select false loop. The reason being that Excel does them one at a time and will not allow you to have everything hidden in a pivot table, it will error out (or what have you) so:
Sub MultiItemPivotFilter2()
Dim PI As PivotItem
With Sheets("Pivot_stocks_1").PivotTables("Pivot_Stocks_1")
.RefreshTable
'Loop first setting all to "True"
For Each PI In .PivotFields("date2").PivotItems
PI.Visible = True
Next PI
'Loop then setting any "False"
For Each PI In .PivotFields("date2").PivotItems
If DateValue(PI.Name) < DateValue(Sheets("Pivot_stocks_1").Range("J13").Value) Then
PI.Visible = False
End If
Next PI
End With
End Sub
Hope that makes sense!
Edit
Even if it doesn't fix your problem you should always implement this logic when hiding pivotItems

VBA PivotTable Filters

So I'm having some issues with my VBA and toggling a PivotTable filter. This is my code:
Sub Macro2()
Sheets("Report").Visible = True
Sheets("Report").PivotTables("PivotTable1").PivotCache.Refresh
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").ClearAllFilters
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").CurrentPage = "1"
Sheets("Report").Activate
End Sub
I've tried using "1", 1, 1.0 and haven't had any luck. The error I get is:
"Application-defined or object-defined error"
Any help is appreciated.
For some reason Excel 2013 might have been the issue here. I managed to find a workaround should anyone else run into this issue. I used a loop which would let me edit visibility of the fields. Here was the solution:
Sub CreateReport()
Sheets("Report").Visible = True
Sheets("Report").PivotTables("PivotTable1").PivotCache.Refresh
Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").ClearAllFilters
Dim pi As PivotItem
For Each pi In Sheets("Report").PivotTables("PivotTable1").PivotFields("dwm").PivotItems
If pi.Value = 0 Then
pi.Visible = False
End If
Next pi
Sheets("Report").Activate
End Sub

Excel VBA: clear items in pivot table

I am new to VBA...
I am trying to write a macro that will clear all the selections within a pivot table filter named "Product Family" and select only the item whose name is contained in cell "B33". I am referencing the pivot table in one sheet "sheet8" and trying to change a graph on "Dashboard".
Here is the code...
Sub thisisalsotemp()
'
' thisisalsotemp Macro
'
'
Sheets("Dashboard").Select
ActiveSheet.ChartObjects("Chart 1").Activate
Sheet8.PivotTables("capbylp").PivotFields("Product Family").PivotFields.ClearAllFilters
With Sheet8.PivotTables("capbylp").PivotFields("Product Family")
.PivotItems(Range("B33")).Visible = True
End With
End Sub
The error is in the following line:
Sheet8.PivotTables("capbylp").PivotFields("Product Family").PivotFields.ClearAllFilters
The error message is:
Object doesn't support this property or method
#SeanCheshire: Thanks for the help. I feel this is much closer to what I want. However, I couldnt get it to work. I played around with it a little bit and am closer. here is what i have...
Sub thisisalsotemp2()
Sheets("Dashboard").Select
Sheet8.PivotTables("capbylp").PivotFields("Product Family") = Range("B33")
End Sub
Error 1004 reads: unable to set the pivotfields property of the pivottable class
in the line: Sheet8.PivotTables("capbylp").PivotFields("Product Family") = Range("B33")
you need to set CurrentPage (and you shouldn't need to clear it first).
Using what is shown in your code, I would have something like:
Sheet8.PivotTables("capbylp").PivotFields("Product Family"). _
PivotFields("MyPivotField").CurrentPage = Range("B33").Value
(broken into 2 lines for readability)
This is slightly related; I wanted to clear multiple-selections whenever the user makes them. Apparently, setting the VisibleItemsList can do that.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xPF As PivotField
Dim nms As Variant
nms = Array("Calculation", _
"Rate Type", _
"xx Hierarchy")
Set xPT = Application.ActiveSheet.PivotTables(1)
For Each xPF In xPT.PageFields
For Each nm In nms
If xPF.Name Like "*" & nm & "*" Then
If UBound(xPF.VisibleItemsList) > 1 Then
xPF.VisibleItemsList = Array("")
End If
End If
Next nm
Next xPF
End Sub