+ Reply to Thread
Results 1 to 12 of 12

VBA Pivot Table Distinct Count

  1. #1
    Registered User
    Join Date
    04-26-2014
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    14

    VBA Pivot Table Distinct Count

    Hi

    I have created the following code to create a pivot table, but I can't get a distinct count of the Groupage Number Column.

    Sub HUEMUELM()
    'Declare Variables
    Dim PSheet As Worksheet
    Dim DSheet As Worksheet
    Dim PCache As PivotCache
    Dim PTable As PivotTable
    Dim PRange As Range
    Dim LastRow As Long
    Dim LastCol As Long

    'Insert a New Blank Worksheet
    On Error Resume Next
    Application.DisplayAlerts = False
    Worksheets("PivotTable").Delete
    Sheets.Add Before:=ActiveSheet
    ActiveSheet.Name = "Summary"
    Application.DisplayAlerts = True
    Set PSheet = Worksheets("Summary")
    Set DSheet = Worksheets("SCSGroupageT")

    'Define Data Range
    LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

    'Define Pivot Cache
    Set PCache = ActiveWorkbook.PivotCaches.Create _
    (SourceType:=xlDatabase, SourceData:=PRange). _
    CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
    TableName:="HUEMUELM")

    'Insert Blank Pivot Table
    Set PTable = PCache.CreatePivotTable _
    (TableDestination:=PSheet.Cells(1, 1), TableName:="HUEMUELM")

    'Insert Row Fields
    With ActiveSheet.PivotTables("HUEMUELM").PivotFields("grp route")
    .Orientation = xlRowField
    .Position = 1
    .Name = "Route"
    End With

    'Insert Data Field
    With ActiveSheet.PivotTables("HUEMUELM").PivotFields("Groupage Number")
    .Orientation = xlDataField
    .Position = 1
    .Function = xlDistinctCount
    .NumberFormat = "#,##0"
    .Name = "Consols"
    End With

    'Insert Data Field
    With ActiveSheet.PivotTables("HUEMUELM").PivotFields("Job Number")
    .Orientation = xlDataField
    .Position = 2
    .Function = xlCount
    .NumberFormat = "#,##0"
    .Name = "Jobs"
    End With
    'Insert Data Field
    With ActiveSheet.PivotTables("HUEMUELM").PivotFields("Pieces")
    .Orientation = xlDataField
    .Position = 3
    .Function = xlSum
    .NumberFormat = "#,##0"
    .Name = "Pcs"
    End With

    End Sub

    Any help would be much appreciated

    Thanks

    Matt

  2. #2
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA Pivot Table Distinct Count

    You can't get a distinct count with a regular pivot table. You'd have to add the data to the data model as a linked table.
    Rory

  3. #3
    Registered User
    Join Date
    04-26-2014
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: VBA Pivot Table Distinct Count

    Thanks for the quick response Rory

    How can I achieve this using VBA?

    Matt

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA Pivot Table Distinct Count

    No idea, I'm afraid, as I only have 2010 here which doesn't support the data model. What do you get from recording a macro while doing it? (I can't recall if there's any programmatic access to the data model in 2013)
    Last edited by rorya; 01-10-2019 at 08:34 AM.

  5. #5
    Registered User
    Join Date
    04-26-2014
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: VBA Pivot Table Distinct Count

    Recorded macro gave the following, but no idea how to apply this code to the original code

    Sub huetest()
    '
    ' huetest Macro
    '

    '
    ActiveCell.Offset(-32, -6).Range("A1:I17").Select
    Workbooks("HUEMUELMTEST.xlsm").Connections.Add2 _
    "WorksheetConnection_SCSGroupageT!$A$1:$I$17", "", _
    "WORKSHEET;C:\Users\mharri\Desktop\[HUEMUELMTEST.xlsm]SCSGroupageT", _
    "SCSGroupageT!$A$1:$I$17", 7, True, False
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
    ActiveWorkbook.Connections("WorksheetConnection_SCSGroupageT!$A$1:$I$17"), _
    Version:=6).CreatePivotTable TableDestination:="Sheet10!R3C1", TableName _
    :="PivotTable2", DefaultVersion:=6
    Sheets("Sheet10").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable2").CubeFields("[Range].[grp route]")
    .Orientation = xlRowField
    .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable2").CubeFields.GetMeasure _
    "[Range].[Groupage Number]", xlCount, "Count of Groupage Number"
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").CubeFields("[Measures].[Count of Groupage Number]"), _
    "Count of Groupage Number"
    With ActiveSheet.PivotTables("PivotTable2").PivotFields( _
    "[Measures].[Count of Groupage Number]")
    .Caption = "Distinct Count of Groupage Number"
    .Function = xlDistinctCount
    End With
    ActiveSheet.PivotTables("PivotTable2").CubeFields.GetMeasure _
    "[Range].[Job Number]", xlCount, "Count of Job Number"
    ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
    "PivotTable2").CubeFields("[Measures].[Count of Job Number]"), _
    "Count of Job Number"
    End Sub

  6. #6
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA Pivot Table Distinct Count

    Please use code tags when posting code.

    Obviously I can't test this, but it should be something like:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    04-26-2014
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: VBA Pivot Table Distinct Count

    HI Rory

    That works perfectly for the range I used for the recorded macro, but when i try to run on the original data which has 2000 rows & 10 columns, it only creates a Pivot Table for Range("A1:I17"), though I can't see any reference to this range in your code?

  8. #8
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA Pivot Table Distinct Count

    Try deleting the existing connection first?

  9. #9
    Registered User
    Join Date
    04-26-2014
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: VBA Pivot Table Distinct Count

    That's brilliant

    Thanks very much for your help Rory

    Regards

    Matt

  10. #10
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: VBA Pivot Table Distinct Count

    No worries - glad it worked!

  11. #11
    Registered User
    Join Date
    11-06-2009
    Location
    Delhi, India
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: VBA Pivot Table Distinct Count

    This saved my life man! 13 years old Solution, still works like a charm! Thanks Man

  12. #12
    Registered User
    Join Date
    11-02-2022
    Location
    Cairo, Egypt
    MS-Off Ver
    2019
    Posts
    1

    Re: VBA Pivot Table Distinct Count

    Hello guys

    can you please show us the code eventually

    I did not understand the last step

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Distinct Count in Pivot Table not linking properly?
    By xcrunner in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 11-08-2018, 04:42 AM
  2. Help with distinct count on Pivot table
    By Jonathan11235 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 01-27-2016, 07:25 AM
  3. No distinct count function in pivot table
    By AS Mcclain in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 3
    Last Post: 09-25-2015, 12:37 PM
  4. How to count distinct values from table in pivot table
    By gopijadhav in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 01-04-2014, 07:14 AM
  5. [SOLVED] How do I set up Distinct Count in a Pivot Table?
    By STOFF in forum Excel General
    Replies: 1
    Last Post: 04-18-2006, 01:25 PM
  6. count distinct in Pivot table
    By soe in forum Excel General
    Replies: 1
    Last Post: 02-22-2005, 10:06 AM
  7. [SOLVED] Count Distinct Values by Group Using Pivot Table (NM)
    By MCP in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-11-2005, 06:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1