I'm trying to create a filter in a pivot table based on a cell value in Excel VBA. I have found this code below but it does not work. I have attached a screenshot containing the pivot table. I want to filter "M?nad" as "May" (which is the input in cell D69).
Sub FilterPageValue()
Dim pvFld As PivotField
Dim strFilter As String
Set pvFld = ActiveSheet.PivotTables("PivotTable7").PivotFields("M?nad")
strFilter = ActiveWorkbook.Sheets("220").Range("D69").Value
pvFld.CurrentPage = strFilter
End Sub
While doing this i reach a run-time error '1004': Application-defined or object-defined error. Name of pivot table are "PivotTable7". Name of sheet are "220". Input value is in cell D69. Note that the data comes from a external source (OLAP Cube) which I think is the main reason of this issue.
Please help! :-)
I tried debug with:
Sub abc() Debug.Print
ActiveSheet.PivotTables("PivotTable7").PivotFields(2).Name
End Sub
Then I get: [Dim_Kalender].[Månad].[Månad]
Therefore i tried following code:
Sub FilterPageValue()
Dim pvFld As PivotField
Dim strFilter As String
Set pvFld = ActiveSheet.PivotTables("PivotTable7").PivotFields("
[Dim_Kalender].[Månad].[Månad]")
strFilter = ActiveWorkbook.Sheets("220").Range("D69").Value
pvFld.CurrentPage = strFilter
End Sub
Now I am stuck on "pvFld.CurrentPage = strFilter". Same error as before.