Reference Pivot Fields and Pivot Items in an Excel Pivot Table, using VBA
This section explains how to access, reference, select or use Pivot Fields and Pivot Items in a Pivot Table, viz. Pivot Field, Column Field, Row Field, Data Field, Page Field, Pivot Items in a Field, Hidden Fields, ...
1. To access a Pivot Field, both visible and hidden, use the PivotTable.PivotFields Method.
Example 1: Refer Image 1, after running below code.
Image 1
Sub PivotTableFieldsItems1()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 10 mentions all the pivot fields:
For Each pvtFld In PvtTbl.PivotFields
strPvtFld = strPvtFld & ", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(10, 1) = Mid(strPvtFld, 3)
'sort the "Country" pivot field in ascending order and color the field label:
PvtTbl.PivotFields("Country").DataRange.Sort Order1:=xlAscending, Type:=xlSortLabels
PvtTbl.PivotFields("Country").LabelRange.Interior.Color = vbYellow
End Sub
2. To access a column field in a PivotTable, use the PivotTable.ColumnFields Property.
Example 2: Refer Image 2, after running below code.
Image 2
Sub PivotTableFieldsItems2()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 10 mentions all the column fields:
For Each pvtFld In PvtTbl.ColumnFields
strPvtFld = strPvtFld & ", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(10, 1) = Mid(strPvtFld, 3)
'sort the "Country" column field in descending order and color the field label:
PvtTbl.ColumnFields("Country").DataRange.Sort Order1:=xlDescending, Type:=xlSortLabels
PvtTbl.ColumnFields("Country").LabelRange.Interior.Color = vbYellow
End Sub
3. To access a row field in a PivotTable, use the PivotTable.RowFields Property.
Example 3: Refer Image 3, after running below code.
Image 3
Sub PivotTableFieldsItems3()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 15 mentions all the row fields:
For Each pvtFld In PvtTbl.RowFields
strPvtFld = strPvtFld & ", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(15, 1) = Mid(strPvtFld, 3)
'sort the "Car Models" row field in descending order and color the field label:
PvtTbl.RowFields("Car Models").DataRange.Sort Order1:=xlDescending, Type:=xlSortLabels
PvtTbl.RowFields("Car Models").LabelRange.Interior.Color = vbYellow
End Sub
4. To access a data field in a PivotTable, use the PivotTable.DataFields Property.
Example 4: Refer Image 4, after running below code.
Image 4
Sub PivotTableFieldsItems4()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 16 mentions all the data fields:
For Each pvtFld In PvtTbl.DataFields
strPvtFld = strPvtFld &", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(16, 1) = Mid(strPvtFld, 3)
'sort the "Sum of Sales" data field in descending order and color the field label:
Set rngKey1 = PvtTbl.DataFields("Sum of Sales").DataRange.Cells(1)
PvtTbl.DataFields("Sum of Sales").DataRange.Sort key1:=rngKey1, Order1:=xlDescending, Type:=xlSortValues
PvtTbl.DataFields("Sum of Sales").LabelRange.Interior.Color = vbYellow
End Sub
5. To access a page field in a PivotTable, use the PivotTable.PageFields Property.
Example 5: Refer Image 5, after running below code.
Image 5
Sub PivotTableFieldsItems5()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 13 mentions all the page fields:
For Each pvtFld In PvtTbl.PageFields
strPvtFld = strPvtFld &", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(13, 1) = Mid(strPvtFld, 3)
'color the "Year" page field label:
PvtTbl.PageFields("Year").LabelRange.Interior.Color = vbYellow
End Sub
6. Reference PivotItem in a PivotField
Example 6a - Hide a specific item in a PivotField:
Sub PivotTableFieldsItems6a()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
PvtTbl.PivotFields("City").PivotItems("London").Visible = False
End Sub
Example 6b - Show all items in a Pivot Field:
Sub PivotTableFieldsItems6b()
Dim PvtTbl As PivotTable
Dim pvtItm As PivotItem
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
For Each pvtItm In PvtTbl.PivotFields("City").PivotItems
pvtItm.Visible = True
Next
End Sub
Example 6c - Loop through all items in a PivotTable Field to hide or show them:
Sub PivotTableFieldsItems6c()
Dim PvtTbl As PivotTable
Dim pvtItm As PivotItem
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
For Each pvtItm In PvtTbl.PivotFields("City").PivotItems
If MsgBox("Hide Item " & pvtItm & "?", vbYesNo) = vbYes Then
pvtItm.Visible = False
End If
Next
End Sub
7. To return hidden fields in a PivotTable, use the PivotTable.HiddenFields Property.
Example 7: Refer Image 6, after running below code.
Image 6
Sub PivotTableAccessFields7()
Dim PvtTbl As PivotTable
Dim pvtFld As PivotField
Dim strPvtFld As String
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
'row 15 mentions all the hidden fields:
For Each pvtFld In PvtTbl.HiddenFields
strPvtFld = strPvtFld &", " & pvtFld.Name
Next
Worksheets("Sheet1").Cells(15, 1) = Mid(strPvtFld, 3)
End Sub
8. Select a part of the PivotTable using the PivotTable.PivotSelect Method.
This method has 3 arguments - Name, Mode & UseStandardName. Name argument is mandatory to specify while other arguments are optional. Name is the PivotTable part to be selected. Mode specifies the PivotTable items to be selected and can be: xlBlanks, xlButton, xlDataAndLabel, xlDataOnly, xlFirstRow, xlLabelOnly and xlOrigin. Default Mode is xlDataAndLabel.
Example 8a: Selecting the Data & Labels for all the items in the "Region" Field - refer Image 7a.
Image 7a
Sub PivotTableAccessFields8a()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect Name:="Region", Mode:=xlDataAndLabel, UseStandardName:=True
'or
'PvtTbl.PivotSelect Name:="Region[All]", Mode:=xlDataAndLabel, UseStandardName:=True
Selection.Interior.Color = vbYellow
End Sub
Example 8b: Selecting the Data & Labels in the Subtotal row for all the items in the "Region" Field - refer Image 7b.
Image 7b
Sub PivotTableAccessFields8b()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect "Region[All;Total]", xlDataAndLabel, True
Selection.Interior.Color = vbYellow
End Sub
Example 8c: Selecting the Data & Labels in the Subtotal row for the "Europe" item only in the "Region" Field - refer Image 7c.
Image 7c
Sub PivotTableAccessFields8c()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect "Region[Europe;Total]", xlDataAndLabel, True
Selection.Interior.Color = vbYellow
End Sub
Example 8d: Selecting the Data & Labels for the "Europe" item only in the "Region" Field - refer Image 7d.
Image 7d
Sub PivotTableAccessFields8d()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect "Region[Europe]", xlDataAndLabel, True
Selection.Interior.Color = vbYellow
End Sub
Example 8e: Selecting the Labels for the "Europe" item only in the "Region" Field - refer Image 7e.
Image 7e
Sub PivotTableAccessFields8e()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect "Region[Europe]", xlLabelOnly, True
Selection.Interior.Color = vbYellow
End Sub
Example 8f: Selecting the complete PivotTable - refer Image 7f.
Image 7f
Sub PivotTableAccessFields8f()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect Name:="", Mode:=xlDataAndLabel, UseStandardName:=True
Selection.Interior.Color = vbYellow
End Sub
Example 8g: Select Grand Totals of Rows - refer Image 7g.
Image 7g
Sub PivotTableAccessFields8g()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect Name:="Row Grand Total", Mode:=xlDataAndLabel, UseStandardName:=True
Selection.Interior.Color = vbYellow
End Sub
Example 8h: Select Grand Totals of Columns - refer Image 7h.
Image 7h
Sub PivotTableAccessFields8h()
Dim PvtTbl As PivotTable
Set PvtTbl = Worksheets("Sheet1").PivotTables("PivotTable1")
Worksheets("Sheet1").Activate
PvtTbl.PivotSelect Name:="Column Grand Total", Mode:=xlDataAndLabel, UseStandardName:=True
Selection.Interior.Color = vbYellow
End Sub
相關文章
- Laravel pivot 新增 loadLaravel
- 透視表pivot_table和交叉表crosstabROS
- [LeetCode] 724. Find Pivot IndexLeetCodeIndex
- (三、四)Superset 1.3圖表篇——透視表-Pivot Table
- Oracle中pivot函式詳解Oracle函式
- SQL 行轉列 PIVOT 學習示例SQL
- Oracle行列轉換及pivot子句的用法Oracle
- Spark實現行列轉換pivot和unpivotSpark
- sql的行轉列(PIVOT)與列轉行(UNPIVOT)SQL
- 155_模型_Power BI & Power Pivot 進銷存之安全庫存模型
- Java的GUI框架如何選擇? Swing、SWT、AWT、SwingX、JGoodies、JavaFX、Apache Pivot?JavaGUI框架GoApache
- T-SQL——透視PIVOT動態獲取待擴充套件元素集SQL套件
- [Javascript] Find Items from the end of the JavaScript Array using at, findLast and findLastIndexJavaScriptASTIndex
- FIN2020 Excel and VBAExcel
- MGTSC 212 using ExcelExcel
- Python與Excel VBA比較PythonExcel
- 用 (Excel) VBA 讀取 OneNote!Excel
- Excel VBA小程式 -使 用VBA實現VLOOKUP函式查詢?Excel函式
- VBA中使用EXCEL工作表函式Excel函式
- Excel VBA 利用FileSystemObject處理檔案ExcelObject
- Excel VBA活動抽獎小程式Excel
- VBA遍歷 Excel 合併到一個 Excel 檔案Excel
- Excel vba自定義函式公式智慧提示Excel函式公式
- HTML Table 輸出ExcelHTMLExcel
- items
- 簡單演示Excel中VBA程式碼的使用Excel
- Excel vba 例項(1) - 批量製作工資表頭Excel
- 在Excel VBA中使用SQL到底優勢在哪兒ExcelSQL
- 百度翻譯 Excel TableExcel
- Vue+element ui table 匯出到excelVueUIExcel
- html table 匯出excel,親測有效HTMLExcel
- 在Excel VBA中寫SQL,是一種什麼體驗ExcelSQL
- 利用poi將Html中table轉為ExcelHTMLExcel
- CSS align-itemsCSS
- C. Splitting Items
- Excel表格中圖片怎麼固定在單元格里(VBA方法)Excel
- MySQL 8.0 Reference Manual(讀書筆記78節-- InnoDB Table and Page Compression (1))MySql筆記
- MySQL 8.0 Reference Manual(讀書筆記79節-- InnoDB Table and Page Compression (2))MySql筆記
- elasticsearch中使用runtime fieldsElasticsearch