I am having some problems in linking an Active X combobox to a Pivot table filter. Basically I will need to have the users to select from the combobox an ID that needs to be linked to the Pivot table filter. I am using the following code:
Private Sub ComboBox1_Change()
Dim sheet As Worksheet
Dim pt As PivotTable
Dim ptField As PivotField
Set sheet = ThisWorkbook.Worksheets("Overview")
Set pt = sheet.PivotTables("Main")
Set ptField = pt.PivotFields("CustomersId")
ptField.ClearAllFilters
ptField.CurrentPage = Me.ComboBox1.Value
End Sub
The error I get is: "Unable to get the PivotFields property of the PivotTable Class".
Any idea of what is wrong with the above code? Something that I m missing?
Thank you in advance
I've tried many changes but can't make this work... Help plz :)
I want to change a pivot table filter according to a cell range.
Creating the sub inside the proper sheet (Graficos). Have tried renaming it to "Sheet6" and also the code.
Pivot Table Field: "Customer"
Pivot Table Name: "sellin"
Sheet: "Grafico"
Error 1004 on line 21: Field.CurrentPage = NewCat
Application-defined or object-defined error
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("F1:F2")) Is Nothing Then Exit Sub
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
Set pt = Worksheets("Grafico").PivotTables("sellin")
Set Field = pt.PivotFields("Customer")
NewCat = Worksheets("Grafico").Range("F1").Value
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
Make sure that cell F1 has a value and that that value is one of the customer names. Then the code runs fine.
I am trying to manipulate a slicer for a regular Pivot table in VBA (not PowerPivot), but I cannot reference the VisibleSlicerItemsList. I get an error:
Application-defined or object-defined error
Any idea what I am doing wrong?
Screenshot showing data and pivot table with slicer from test workbook, along with the error message "Application-defined or object-defined error" for the VisibleSlicerItemsList in the Locals window
You can download the test workbook here.
Check the MSDN entry on this property - it states that:
The VisibleSlicerItemsList property is only applicable for slicers that are based on OLAP data sources (SlicerCache.OLAP = True).
In your example you are using a pivot table connected to data in the sheet and not to an OLAP cube.
You can work around it with code like this which iterates the SlicerItems collection of the SlicerCache:
Option Explicit
Sub Test()
Dim objCache As SlicerCache
Dim objItem As SlicerItem
Dim varChoices As Variant
Dim lngCounter1 As Long
Dim lngCounter2 As Long
Set objCache = ThisWorkbook.SlicerCaches("Slicer_Company")
varChoices = Array("1", "3", "5")
' iterate slicers
For lngCounter1 = 1 To objCache.SlicerItems.Count
Set objItem = objCache.SlicerItems(lngCounter1)
'assume not for selection
objItem.Selected = False
'iterate choices to check for activation
For lngCounter2 = 0 To UBound(varChoices)
If objItem.Name = varChoices(lngCounter2) Then
'activate and exit loop
objItem.Selected = True
Exit For
End If
Next lngCounter2
Next lngCounter1
End Sub
I am trying to set the page filter on pivot table based on the value in a cell. I am a novice with vba, so I found some code to start with. I am getting an error "unable to set the currentpage property of the pivotfield class" on the command field.currentpage = newcat
If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As Long
Set pt = Worksheets("REPORT - Acct Plan").PivotTables("PT_Activities")
Set Field = pt.PivotFields("[Activities].[BPID].[BPID]")
NewCat = Worksheets("REPORT - Acct Plan").Range("B1").Value
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
I did some research and found a similar question, but it was never answered completely. I also verified the data types of my control cell B1 and pivot table were the same.
Any assistance is greatly appreciated!
I have a filter field called week_of_year (1,2,3,4,5,6,7,8....), another pivot field year(2012,2013,2014), the vba code belows filter on the current week and year of 2014/2015 and hide everything else. But the problem is the code loops each record and runs slow when it comes to big pivot table. Im trying on a new code but encountering some error.
Sub datefilter()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("LO").PivotTables("PivotTable3")
Dim dd As Integer
dd = Format(Date, "ww")
Dim pf As PivotField
Dim pf1 As PivotField
Dim PI As PivotItem
Set pf =
Worksheets("LO").PivotTables("PivotTable3").PivotFields("week_of_year")
Set pf1 =
Worksheets("LO").PivotTables("PivotTable3").PivotFields("year")
PvtTbl.ClearAllFilters
For Each PI In pf.PivotItems
If PI.Name = CStr(dd) Then
PI.Visible = True
Else
PI.Visible = False
End If
Next
For Each PI In pf1.PivotItems
If PI.Name = "2014" Or PI.Name = "2015" Then
PI.Visible = True
Else
PI.Visible = False
End If
Next
End Sub
New code that I develop is:
Sub datefilter1()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("LO").PivotTables("PivotTable3")
Dim dd As Integer
dd = Format(Date, "ww")
Dim pf As PivotField
Set pf =
Worksheets("LO").PivotTables("PivotTable3").PivotFields("week_of_year")
pf.PivotFilters.Add2 xlValueEquals, 3
End Sub
The code failed at
pf.PivotFilters.Add2 xlValueEquals, CStr(dd)
I also tried:
pf.PivotFilters.Add2 Type:= xlValueEquals, Value1 := CStr(dd)
The error is Invalid procedure call or argument.Any idea How I can fix this error?????
Any faster way to filter on multiple filter criteria? Thanks!
If you only want to filter on one value, drag the field of interest to the Report Filter pane, then you can do this very quickly without looping using the following code:
Sub FilterOnWeek()
Dim pt As PivotTable
Dim pf As PivotField
Set pt = Worksheets("LO").PivotTables("PivotTable3")
Set pf = pt.PivotFields("week_of_year")
pf.CurrentPage = Format(Date, "ww")
End Sub
If for some reason this field absolutely must stay in the PivotTable as a row field, you can still filter without looping by the following method if you have Excel 2010 or later
Make a copy of the PivotTable, and drag the 'week_of_year' field
in the copy to the Report Filter area
Set up a slicer on the week_of_year field on one PivotTable, and
connect it to the other PivotTable via the Report Connections
dialog. (Right click on the Slicer, select 'Report Connections')
Amend the above code so it runs on the 2nd PivotTable.
The Slicer will then sync the two PivotTables.
If you want to filter on more than one value, drag the field to the ROWS area, and use this:
Sub FilterOnWeek2()
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim lStart As Long
Dim lEnd As Long
lEnd = Format(Date, "ww")
lStart = Val(Format(Date, "ww")) - 1
Set pt = Worksheets("LO").PivotTables("PivotTable3")
Set pf = pt.PivotFields("week_of_year")
With pf
.ClearAllFilters
.PivotFilters.Add Type:=xlCaptionIsBetween, Value1:=lStart, Value2:=lEnd
End With
End Sub
If for some reason you don't want this field in the PivotTable as a row field, follow the same general approach as above: make a copy of the PivotTable, put the field that you want to filter on in the copy as a ROWS field, connect the fields of interest together as a slicer, hide the slicer somewhere, and amend the code so it runs on the copy of the PivotTable with the rowfield in it.
I recommend you read my article at http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/ if you want to learn more about efficiently filtering PivotTables.