×

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 had 30 different age groups say from 20 to 50 – this will effectively increase the size of the pivot table and will make it difficult for us to make any inference from the data. Pivot Tables have a very helpful feature known as Grouping, where buckets can be created, and data can be shown for those buckets rather than each age/item.

The grouping method is used for numeric values and dates.

  1. Grouping Date Values by Group Method
  2. Grouping Date values By Weeks/ Months/ Year
  3. Grouping numeric Items by Group Method

In the below excel sheet, we can observe that the date values in the first column. We can modify the date values by grouping them together based on the year. Individual dates are making the sheet size bigger thus, decreasing the readability and increasing the time.

VBA Pivot Table Grouping

The only solution to the above problem is grouping. If we group the dates based on its year, the sheet will become shorter. So, you can do this manually by right-clicking on the date cell and choosing a group. You will get a dialog box provided with a range of different options to group your data. So, it allows you to select a start date and an end date and then an increment of time or multiple increments.

We can achieve the same result through VBA as well by writing a few lines of code.

Program:

Sub PivotTable_Grouping()
Dim r As Range
Dim ws As Worksheet
Dim PT As PivotTable
'setting the worksheet
Set ws = Worksheets("Pivot")
Set PT = ws.PivotTables("Pivot")
'set range of dates to be grouped
Set r = PT.RowRange.Cells(2, 1)
r.Group _
    Start:=True, End:=True, _
    Periods:=Array(False, False, False, False, False, False)
End Sub
VBA Pivot Table Grouping

Explanation: Firstly, we will create a worksheet, and we will refer to the worksheet to the Excel sheet. Then we will declare our Pivot table and refer it as well wit the worksheet. We will give set a reference to the date value by Range variable and will refer it to a Row range for Cells (2,1). It will select the complete row range values. Now, the task left is to group as per year. So we will fill the values including start as True, End as True and in Periods (we can group by different units and increments of time) we will pass an array of seven Boolean values (True/False) that indicate whether or not I’m grouping by that particular increment of time. The last array refers to Years so we will set six FALSE followed by a True.

Output:

In the below sheet, you will find the dates have been grouped by year and it has become much shorter.

VBA Pivot Table Grouping

Related Topics

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.

Excel VBA LBound Function

Excel VBA LBound Function: The LBound function in VBA returns the lowest subscript for the specified dimension in the given array. Syntax LBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA CDbl Function

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

1 minute 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.

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.

Excel VBA UCase Function

The UCase function in VBA converts a String into upper case text. Syntax UCase (String) Parameter String (required) – This parameter represents the string that you want to convert to upper case. Return This function returns a string...

1 minute 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 RTrim Function

VBA RTrim Function: The Rtrim function in VBA removes the leading spaces from the text in the specified string. Syntax RTrim (String) Parameter String (required) – This parameter represents the string from which you want...

1 minute read.

Excel VBA Year Function

Excel VBA Year Function: The Year function in VBA returns the four-digit year for the specified date. Syntax Year (Date) Parameter Date (required) – This parameter represents the significant date. Return This function returns the four-digit year...

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.

Excel VBA Hour Function

The hour function in VBA returns the hour element for the specified time. Syntax Hour (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the hour element for the specified time. Example 1 Sub HourFunction_Example1() ...

1 minute read.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute read.

Excel VBA Rnd Function

VBA Rnd Function: The Rnd function in VBA returns a random number that is greater than or equal to (>=) 0 and is less than (<) 1. Syntax Rnd ([Number]) Parameter Number (optional) –This...

1 minute read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Steps to Create a Chart in VBA

Steps to Create a Chart Charts are created either by directly working with the chart variable object that defines the chart data or by ChartObject method. In order to get to...

3 minutes read.

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA : With End-with Constructs

With End-with Constructs The With-End With construct enables the user to perform multiple operations on a single object. If you are going to perform several different actions on the same object and typing the same...

4 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.

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.