Coding the DistinctCount function in VBA for creating pivot table

vergab

New Member
Joined
Jun 21, 2016
Messages
22
Hi,
I tried to run this code, but it calculate "count" instead of "distinctcount" in case of datafield "TT". If I do pivot table manually, in the Create PivotTable dialog box I have to check the box next to the label “Add this data to the Data Model”. But I don’t know how it works in VBA code. Can anybody help me?
Tis is the code:


Code:
Option Explicit
 Sub Pivottable()
Dim PSheet As Worksheet
 Dim DSheet As Worksheet
 Dim LastPivRow As Long
 Dim LastPivCol As Long
 Dim PCache As PivotCache
 Dim PTable As Pivottable
 Dim PRange As Range
On Error Resume Next
 Application.DisplayAlerts = False
 Worksheets("Pivot").Delete
 Sheets.Add After:=Worksheets("Data")
 ActiveSheet.Name = "Pivot"
 Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot")
 Set DSheet = Worksheets("Data")
LastPivRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
 LastPivCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
 Set PRange = DSheet.Cells(1, 1).Resize(LastPivRow, LastPivCol)
Set PCache = ActiveWorkbook.PivotCaches.Create _
 (SourceType:=xlDatabase, SourceData:=PRange). _
 CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
 TableName:="PivotTable")
Set PTable = PCache.CreatePivotTable _
 (TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")
With ActiveSheet.PivotTables("PivotTable").PivotFields("Name")
         .Orientation = xlRowField
         .Position = 1
 End With
 With ActiveSheet.PivotTables("PivotTable").PivotFields("DB")
         .Orientation = xlColumnField
         .Position = 1
 End With
 With ActiveSheet.PivotTables("PivotTable").PivotFields("DS")
         .Orientation = xlColumnField
         .Position = 2
 End With
 With ActiveSheet.PivotTables("PivotTable").PivotFields("TT")
         .Orientation = xlDataField
         .Position = 1
         .Function = xlDistinctCount
 End With
 With ActiveSheet.PivotTables("PivotTable").PivotFields("Month")
     .Orientation = xlPageField
     .CurrentPage = 12
 End With
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi Sergio,
Thank you for this link. It is very useful. But I can not see how could I connect this solution to my problem. Because as far as I understood this is not a pivot table related thing.
 
Upvote 0
Yes you are right, I got confused because it is quite strange what happens with distinct count (xlDistinctCount) in some version of Excel does not show at all so I am used to doing it with an extra column, your code seems to be using xlDistinctCount so if it does no t work as you think you will have to do with an extra column, like in excel - Simple Pivot Table to Count Unique Values - Stack Overflow
Cheers
Sergio
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,098
Messages
6,170,103
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top