在VBA生成的透视表中执行不同计数

在VBA生成的透视表中执行不同计数,vba,excel,Vba,Excel,有没有办法在VBA生成的透视表中进行disticnt计数? 下面的代码用于创建数据透视表,但是它没有进行不同计数的选项。有什么办法可以改进吗?谢谢 Dim wsData3 As Worksheet Set wsData3 = Worksheets("ProcessIT GE") Dim PvtTblCache3 As PivotCache Set PvtTblCache3 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _

有没有办法在VBA生成的透视表中进行disticnt计数? 下面的代码用于创建数据透视表,但是它没有进行不同计数的选项。有什么办法可以改进吗?谢谢

Dim wsData3 As Worksheet
Set wsData3 = Worksheets("ProcessIT GE")

Dim PvtTblCache3 As PivotCache
Set PvtTblCache3 = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
        SourceData:="GE")
    PvtTblCache3.CreatePivotTable TableDestination:=wsPvtTbl.Range("B" & LastRow_p + 8), _
        TableName:="PivotTable3"

Dim PvtTb3 As PivotTable
Set PvtTb3 = wsPvtTbl.PivotTables("PivotTable3")

PvtTbl.ManualUpdate = True

Set pvtFld = PvtTb3.PivotFields("Period")
pvtFld.Orientation = xlPageField

With wsPvtTbl.PivotTables("PivotTable3").PivotFields("Period")
        On Error Resume Next
        .PivotItems("Y").Visible = True
        .PivotItems("N").Visible = False
        On Error GoTo 0
    End With

Set pvtFld = PvtTb3.PivotFields("INVOICE_TYPE")
pvtFld.Orientation = xlPageField

With wsPvtTbl.PivotTables("PivotTable3").PivotFields("INVOICE_TYPE")
        On Error Resume Next
        .PivotItems("GE").Visible = True
        .PivotItems("PO Matched").Visible = False
        On Error GoTo 0
    End With

    For Each pivot_item In PvtTb3.PivotFields("Period").PivotItems
        If pivot_item.name = "Y" Then
            Set pvtFld = PvtTb3.PivotFields("Submission Date 1")
                pvtFld.Orientation = xlColumnField


            '.AddDataField PvtTb3.PivotFields("THEMIS_INV_ID"), "Distinct Count of ID", xlDistinctCount

            With PvtTb3.PivotFields("THEMIS_INV_ID")
               .Orientation = xlDataField
                .Function = xlCount
                .NumberFormat = "#,##0"
                .Position = 1
            End With
        End If
    Next pivot_item

标准Excel数据透视表没有独特的计数功能,您可以使用PowerPivot而不是Excel 2013提供的
'DistinctCount
功能。我相信,您可以在线(或Microsoft)搜索。只有选中“将此数据添加到数据模型”复选框,将数据添加到数据模型时,独特的计数选项才可用创建数据透视表时。然后您将在“值字段设置”中找到“不同计数”选项。@ShaiRado Mine是Excel 2016。我可以手动完成,但我不知道如何使用VBA代码实现这一点:(这是一般VBA方法的答案。