VBA - 将PivotField过滤器添加到数据透视表

时间:2018-04-09 09:55:57

标签: excel vba excel-vba

我有代码创建一个数据透视表并添加所需的" PivotFields"到目前为止......我似乎无法弄清楚的一件事是如何添加一个" Pivot Field"在"过滤器"区域。

这是我的代码:

Sub CreatePivotTable()
    'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
    'Source: www.TheSpreadsheetGuru.com

    Dim pvtSht As Worksheet
    Dim summarySht As Worksheet
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable
    Dim StartPvt As String
    Dim SrcData As String
    Dim pf As PivotField

    'Determine the data range you want to pivot
    SrcData = ActiveSheet.UsedRange.Address(ReferenceStyle:=xlR1C1)

    'Create a new worksheet
    Set pvtSht = Sheets.Add
    ActiveSheet.Name = "PivotTable"

    'Where do you want Pivot Table to start?
    StartPvt = pvtSht.Name & "!" & pvtSht.Range("A3").Address(ReferenceStyle:=xlR1C1)

    'Create Pivot Cache from Source Data
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=SrcData)

    'Create Pivot table from Pivot Cache
    Set pvt = pvtCache.CreatePivotTable( _
    TableDestination:=StartPvt, _
    TableName:="P1")

    'Insert Row Fields
    With ActiveSheet.PivotTables("P1").PivotFields(" Vulnerability Name")
        .Orientation = xlRowField
        .Position = 1
    End With

    With ActiveSheet.PivotTables("P1").PivotFields(" DNS Name")
        .Orientation = xlRowField
        .Position = 2
    End With

    With ActiveSheet.PivotTables("P1").PivotFields(" Operating System")
        .Orientation = xlRowField
        .Position = 3
    End With

    With ActiveSheet.PivotTables("P1")
        .AddDataField ActiveSheet.PivotTables( _
        "P1").PivotFields("IP Address"), "Count of IP Addresses"
        .PivotFields("IP Address").Orientation = xlRowField
    End With

    With Sheets("PivotTable").PivotTables("P1").PivotFields("IP Address")
        .Orientation = xlRowField
        .Position = 2
    End With

    Set pf = ActiveSheet.PivotTables("P1").PivotFields(" Vulnerability Name")

    'Collapse Pivot Field
    pf.ShowDetail = True

    'Expand Pivot Field
    pf.ShowDetail = False

    'Create a new worksheet
    Set summarySht = Sheets.Add
    ActiveSheet.Name = "Summary"

End Sub

这是它在数据透视表上输出的内容:

enter image description here

这就是我需要输出的内容(在过滤器下注意风险等级):

enter image description here

这是需要在工作表上显示的内容:

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个

    With ActiveSheet.PivotTables("P1").PivotFields("Risk Rating")
        .Orientation = xlPageField
        .Position = 1
    End With

答案 1 :(得分:1)

与您在此处提出的错误无关,但只是通知您要大量缩短代码(并使其更清晰易读):

您已经Set PivotTable这么好了:

Set pvt = pvtCache.CreatePivotTable( _
    TableDestination:=StartPvt, _
    TableName:="P1")

那么为什么不使用它,并完成剩余的PivotTable设置:

With pvt
    With .PivotFields(" Vulnerability Name")
        .Orientation = xlRowField
        .Position = 1
    End With

    With .PivotFields(" DNS Name")
        .Orientation = xlRowField
        .Position = 2
    End With

    With .PivotFields(" Operating System")
        .Orientation = xlRowField
        .Position = 3
    End With

    .AddDataField .PivotFields("IP Address"), "Count of IP Addresses"
    .PivotFields("IP Address").Orientation = xlRowField

    With .PivotFields("IP Address")
        .Orientation = xlRowField
        .Position = 2
    End With

    Set pf = .PivotFields(" Vulnerability Name")
End With