×

Excel VBA- Pivot Table Fields

VBA- Pivot Table Fields: The Pivot Fields collection contains all the fields from the data source, including any calculated fields. The main aspect of adding a field is its Position and Orientation. Each pivot field has its own orientation i.e. column orientation, row orientation, page orientation, or data orientation. It is always advisable to explicitly define the field parameter as the developer decides the positioning at the beginning of any report creation. These actions only affect the given Pivot Table, not the Pivot Cache.

The commonly used Pivot fields are as follows:

  1. RowFields
  2. ColumnFields
  3. PageFields
  4. DataFields
  5. HiddenFields
  6. VisibleFields
  7. AddFields

RowFields

It returns a single field PivotTable object or the collection of all row fields. It is used to add the PivotTable’s row field names to a list in the worksheet. It can be created using ‘AddField’ feature.

Program:

Sub PivotTable_RowField()
     Dim PTCache As PivotCache
     Dim PT As PivotTable    
     'Set the Pi   vot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
     'Create the Pivot Table 
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _
         TableDestination:=Range("A3"))
     'Specifying the Row fields in Pivot Table
     PT.AddFields _
         RowFields:="Region", _
         ColumnFields:="Rep"
 End Sub 

Output

RowFields

ColumnField

It returns a single field PivotTable object or the collection of all column fields. It is used to add the PivotTable’s column field names to a list in the worksheet. It can be created using ‘AddField’ feature.

In the below example, we have used to “Region” Field in column orientation with Central, East, and West as its values.

Program:

Sub PivotTable_ColumnField()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
     'Create the Pivot Table
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _ 
         TableDestination:=Range("A3"))
     'Specifying the Column fields in Pivot Table
     PT.AddFields _
         ColumnFields:="Region", _
         RowFields:="Rep"
 End Sub 
ColumnField

PageFields

It returns a single field PivotTable object or the collection of all page fields. It is used to add the PivotTable’s page field names to a list in the worksheet. It can be created using ‘AddField’ feature.

Program:

Sub PivotTable_PageFields()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
     'Create the Pivot Table
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _ 
         TableDestination:=Range("A3"))
     'Specifying the Page fields in Pivot Table
     PT.AddFields _
         ColumnFields:="Region", _
         RowFields:="Rep", _
         PageFields:="Units" 
     'changing the color of the field to yellow
     PT.PageFields("Units").LabelRange.Interior.Color = vbYellow
 End Sub 

Output

PageFields

DataFields

It returns the “sum of” the fields for the PivotTable object. These are not displayed in the PivotFields collection. It is different from the other three fields as it cannot be added using AddField property.

In the below example, we have used the “Units” field as our DataFields and have calculated the sum of each Region’s unit individually. At last, we have formatted the value using number format with two zeros.

Program:

Sub PivotTable_DateField()
     'Specifying the Column fields in Pivot Table
     PT.AddFields _
         ColumnFields:="Region", _
         RowFields:="Rep"
     'Specifying Data field
     'applying the average function
     PT.AddDataField _
         Field:=PT.PivotFields("Units"), _ 
         Function:=XlConsolidationFunction.xlAverage
     'applying the basic number format
     PT.DataFields(1).NumberFormat = "0.00"   
 End Sub 

Output

DataFields

HiddenFields

This field is used to return the hidden fields in a Pivot Table.

Program:

Sub PivotTable_HiddenFields()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     Dim PTFld As PivotField
     Dim strPvtFld As String
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add 
     'Create the Pivot Table
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _
         TableDestination:=Range("A3"))
     'Specifying the Page fields in Pivot Table
     PT.AddFields _ 
         ColumnFields:="Region", _
         RowFields:="Rep"
     'row mentions all the hidden fields:
     For Each PTFld In PT.HiddenFields
         strPvtFld = strPvtFld & ", " & PTFld.Name
     Next
     ActiveSheet.Cells(15, 1) = "Hidden Fields: " & Mid(strPvtFld, 3)
     ActiveCell.WrapText = True 
 End Sub 

Output

HiddenFields

VisibleFields

VisibleFields is the vice-versa of the Hidden Field. It is used to display all the fields used up in the Pivot Table. 

Program:

Sub PivotTable_VisibleFields()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     Dim PTField As PivotField
     Dim strPvtFld As String
     'Set the Pivot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
     'Create the Pivot Table
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _
         TableDestination:=Range("A3")) 
     'Specifying the Page fields in Pivot Table
     PT.AddFields _
         ColumnFields:="Region", _
         RowFields:="Rep"
     'row mentions all the hidden fields:
     ActiveSheet.Cells(19, 1).Value = "Active Fields:" 
     rw = 19
     For Each PTField In PT.VisibleFields
         rw = rw + 1
         ActiveSheet.Cells(rw, 1).Value = PTField.Name
     Next PTField
     ActiveCell.WrapText = True
 End Sub 

Output

VisibleFields

AddFields

The AddFields method is used to add multiple fields (RowFields, ColumnFields, AddtoTable, or PageFields) in our Pivot Table. It accepts all types of fields to accept the DataField. The user can even apply Array Function to add multiple fields in one location.

Program:

Sub PivotTable_AddFields()
     Dim PTCache As PivotCache
     Dim PT As PivotTable
     'Set the Pi   vot Cache
     Set PTCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, Sheets("Pivot Table").Range("A1").CurrentRegion)
     ' Adding a new sheet for Pivot Table
     Worksheets.Add
     'Create the Pivot Table 
     Set PT = ActiveSheet.PivotTables.Add( _
         PivotCache:=PTCache, _
         TableDestination:=Range("A3"))
     'Specifying the Row fields in Pivot Table
     PT.AddFields _
         RowFields:="Region", _ 
         ColumnFields:="Rep", _
         PageFields:="Units"
 End Sub 

Output

AddFields

Related Topics

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

Excel VBA Replace Function

VBA Replace Function: The Replace function in VBA searches for a substring within the specified string and replaces its occurrences with a second substring. Syntax Replace (Expression, Find, Replace, [Start], [Count], [Compare]) Parameter Expression (required) – This parameter represents...

2 minutes read.

Excel VBA Error Handling

What is Errors and Types of Error? Errors are conditions that resist the flow of the program or enables a problem while running any programming. There are three types of errors in VBA...

5 minutes read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

Excel VBA Left Function

VBA Left Function: The Left function in VBA returns a substring from the start of the specified string. Syntax Left (Str, Length) Parameter Str (required) – This parameter represents the string that you want to extract...

1 minute read.

Excel VBA Month Function

The Mont function in VBA returns the month number for the specified date. Syntax Month (Date) Parameter Time (required) – This parameter represents the date. Return This function returns the month number for the specified date. Example 1 Sub MonthFunction_Example1() ...

1 minute read.

Excel VBA DateValue Function

The DateValue function in VBA returns a VBA Date from the given String representation of a date wherein the time information is ignored. It is unable to interpret dates that include the...

1 minute read.

Excel VBA User-Defined Functions

User-Defined Functions One of the advantages of VBA is that you can create your own functions using macros. These functions can be called and used as other functions in excel and use them. You can...

3 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

Excel VBA Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.

Excel VBA IsNumeric Function

VBA IsNumeric Function: The IsNumeric function in VBA returns a Boolean value showing whether the specified Expression contains a numeric value or not. Syntax IsNumeric (Expression) Parameter Expression (required)- This parameter represents the variant that...

1 minute read.

Excel VBA Error Function

VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code. Syntax Error ([ErrorNumber]) Parameter ErrorNumber (optional) – This parameter represents the required error number. By default, the...

1 minute read.

VBA Find Function

VBA Find Function The Excel VBA FIND function finds any information in your Excel. It can be used on a Range object on the worksheet. It works the same, unlike the Excel Find &...

6 minutes read.

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

VBA runtime error 1004

What is 1004 error?  VBA 1004 Error, also known as object-defined or application-defined, is a runtime error in VBA, usually, if the specified range does not exist in the worksheet or if the Application...

6 minutes read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.