×

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 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 Functions

What is a function? A function is also called a procedure, but it is not a sub procedure, it’s a function procedure. You have already been using some function in Excel,...

7 minutes read.

Excel VBA GoTo Statement

GoTo Statement he GoTo statement branches unconditionally to a specified line in a procedure. It is used to transfer the program control to a new statement, which is headed by a label. It sends...

2 minutes read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

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.

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.

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 String Function

The String function in VBA creates a String, consisting of several repeated characters. Syntax String (Number, Character) Parameter Number (required) – This parameter represents the number of characters in the returned String. Character (required) – This parameter...

1 minute read.

Excel VBA Weekday Function

Excel VBA Weekday Function: The TimeValue function in VBA returns an integer (1 to 7), signifying the day of the week for the specified date. Syntax Weekday (Date, [FirstDayOfWeek]) Parameter Date (required) – This parameter...

2 minutes read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

1 minute read.

Procedures in VBA

Procedures in VBA A procedure is a block of statements or units of computer code that performs some action. It is enclosed with a declaration statement, and its primary purpose is to carry out...

3 minutes read.

Excel VBA CLng Function

VBA CLng Function: The CLng function in VBA converts an expression into a Long data type. It returns a long value ranging between -2,147,483,648 and 2,147,483,647. Syntax CLng (Expression) Parameter Expression (required) – This...

1 minute read.

CodeIgniter File Uploading Class

The Codeigniter provides a file Uploading library class which is used to upload any file such as images, pdf, mp3, etc. to the codeigniter’s application. It also allows to set various preferences such...

5 minutes 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.

Looping in VBA

Looping in VBA There are many situations where a programmer needs to execute a block of the repetitive code number of times. Writing the same statement will make the program tedious and monotonous....

3 minutes read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

1 minute read.

Excel VBA CInt Function

VBA CInt Function: The VBA Cint function converts the specified expression into an Integer. Syntax Cint (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to an Integer wherein...

1 minute 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 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 Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.