在VBA生成的数据透视表中进行明确计数

时间:2017-06-21 05:51:13

标签: excel vba excel-vba

有没有办法在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

0 个答案:

没有答案