How to count distinct values in a PivotTable using VBA?

To count distinct values in a PivotTable using VBA, you will need to create a data model and use Data Model functionality in Excel, specifically with PowerPivot. Here’s a step-by-step guide on how to create a PivotTable with distinct counts using VBA:

Step 1: Enable PowerPivot

Before you start, ensure that the PowerPivot add-in is enabled in Excel:

  • Go to `File` > `Options`.
  • Click on `Add-Ins`.
  • At the bottom, in the `Manage` box, select `COM Add-ins` and click `Go`.
  • Check `Microsoft Office PowerPivot for Excel` and click `OK`.

Step 2: Prepare Your Data

Ensure your dataset is formatted as a table. This is important for best practices and easier handling through VBA.

Step 3: Use VBA to Create a PivotTable with Distinct Count

Here’s a sample VBA code to create a PivotTable that displays a distinct count:

Sub CreatePivotTableWithDistinctCount()
    Dim ws As Worksheet
    Dim dataSheet As Worksheet
    Dim dataRange As Range
    Dim pivotCache As PivotCache
    Dim pivotTable As PivotTable
    Dim pivotField As PivotField
    
    ' Set references to the sheets
    Set dataSheet = ThisWorkbook.Sheets("YourDataSheetName") ' change to your data sheet name
    Set ws = ThisWorkbook.Sheets.Add ' Create a new sheet or reference your existing sheet

    ' Set the range for your data table (assuming data starts from A1)
    Set dataRange = dataSheet.Range("A1").CurrentRegion
    
    ' Create the Pivot Cache
    Set pivotCache = ThisWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=dataRange, _
        Version:=xlPivotTableVersion15) ' Version 15 for Excel 2013 and later

    ' Create the Pivot Table
    Set pivotTable = pivotCache.CreatePivotTable( _
        TableDestination:=ws.Range("A3"), _
        TableName:="PivotTableWithDistinctCount")

    ' Add a field to row area
    pivotTable.PivotFields("YourFieldName").Orientation = xlRowField ' change to actual field name
    
    ' Add a field to the data area with distinct count
    Set pivotField = pivotTable.PivotFields("YourFieldName") ' change to actual field name
    pivotField.Orientation = xlDataField
    pivotField.Function = xlDistinctCount
    pivotField.Name = "Distinct Count of YourFieldName"

End Sub

Notes:

  • Change `”YourDataSheetName”` and `”YourFieldName”` to your actual sheet name and field name.
  • The `.CurrentRegion` method is a simple way to refer to a contiguous dataset. Adjust the `dataRange` as necessary for your dataset.
  • This code assumes you are working with Excel 2013 or later (indicated by `xlPivotTableVersion15`) since distinct count in a PivotTable was introduced in Excel 2013.
  • Ensure the data range (`dataRange`) only includes the columns that are necessary for the PivotTable, as working with unnecessary columns might produce errors or delay processing.
  • This code adds a new worksheet for the PivotTable, but you can modify it to place the PivotTable in a specific existing sheet if needed.

By following the above steps, you should be able to create a PivotTable with distinct counts using VBA in Excel.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project