{"id":5355,"date":"2020-12-14T13:18:11","date_gmt":"2020-12-14T13:18:11","guid":{"rendered":"http:\/\/officetuts.net\/excel\/?p=5355"},"modified":"2024-03-29T15:55:39","modified_gmt":"2024-03-29T15:55:39","slug":"filter-excel-pivot-table-using-vba","status":"publish","type":"post","link":"https:\/\/officetuts.net\/excel\/vba\/filter-excel-pivot-table-using-vba\/","title":{"rendered":"How to Filter Excel Pivot Table using VBA"},"content":{"rendered":"\n
Excel offers a robust tool for filtering Pivot Tables with the VBA code. In this article, I’m going to show you different ways you can do it.<\/p>\n\n\n\n
We have the following data:<\/p>\n\n\n\n It’s a list of names with cities, states, and birth dates. I’m going to use this data to demonstrate filtering options.<\/p>\n\n\n\n Make sure that the sheet with the data is active and run this code:<\/p>\n\n\n\n In the beginning, this subprocedure gets the data range for the pivot table (A1:E11<\/strong>).<\/p>\n\n\n\n Next, it creates a new sheet with the default name.<\/p>\n\n\n\n The myPivotTableStart<\/strong> variable determines where the Pivot Table starts. In this case, it’s cell A1<\/strong>.<\/p>\n\n\n\n Next, we are going to create myPivotCache<\/strong> to hold the replica of the data source.<\/p>\n\n\n\n At the end of the procedure, a pivot table name “PivotTable1” will be created from this cache.<\/p>\n\n\n\n It’s time to start filtering Pivot Table data. You can assign 5 different values to a field.<\/p>\n\n\n\n The following code inserts Pivot Table elements into fields:<\/p>\n\n\n\n At the beginning of the code, there is a Pivot Table assignment to the myPivotTable<\/strong> variable.<\/p>\n\n\n\n Two table headers (“Name” and “City”) are added to the Rows<\/strong> Field. Now, “City” is after “Name”, but it’s important to have “City” as the first position. The next line of code do just that.<\/p>\n\n\n\n The “State” field is added as a Pivot Table filter.<\/p>\n\n\n\n The last line before the end is xlHiddenField<\/strong>. It makes sure that the “Birth” header is not used in any field of the Pivot Table.<\/p>\n\n\n\nSub CreatePivotTable() \n Dim mySheet As Worksheet\n Dim myPivotCache As PivotCache\n Dim myPivotTable As PivotTable\n Dim myPivotTableStart As String\n Dim myString As String\n \n myString = ActiveSheet.Name & \"!\" & Range(\"A1:D11\").Address(ReferenceStyle:=xlR1C1)\n \n Set mySheet = Sheets.Add\n \n myPivotTableStart = mySheet.Name & \"!\" & mySheet.Range(\"A1\").Address(ReferenceStyle:=xlR1C1)\n \n Set myPivotCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=myString)\n \n Set myPivotTable = myPivotCache.CreatePivotTable(TableDestination:=myPivotTableStart, TableName:=\"PivotTable1\") \nEnd Sub<\/code><\/pre>\n\n\n\n
Filtering Pivot Tables<\/h2>\n\n\n\n
Name<\/strong><\/td> Value<\/strong><\/td> Field<\/strong><\/td><\/tr> xlHidden<\/td> 0<\/td> No field<\/td><\/tr> xRowField<\/td> 1<\/td> Rows<\/td><\/tr> xColumnField<\/td> 2<\/td> Columns<\/td><\/tr> xlPageField<\/td> 3<\/td> Filters<\/td><\/tr> xlDataField<\/td> 4<\/td> Values<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n Sub AddPivotFields()\n Set myPivotTable = ActiveSheet.PivotTables(\"PivotTable1\")\n \n myPivotTable.PivotFields(\"Name\").Orientation = xlRowField\n myPivotTable.PivotFields(\"City\").Orientation = xlRowField\n myPivotTable.PivotFields(\"City\").Position = 1\n myPivotTable.PivotFields(\"State\").Orientation = xlPageField\n myPivotTable.PivotFields(\"Birth\").Orientation = xlHiddenField\nEnd Sub<\/code><\/pre>\n\n\n\n