Excel 如何使用VBA将透视表过滤器放置在彼此上方?

Excel 如何使用VBA将透视表过滤器放置在彼此上方?,excel,vba,filter,pivot-table,Excel,Vba,Filter,Pivot Table,基本上,我有多个选项卡,每个选项卡都有多个枢轴,每个枢轴都内置了多个过滤器。因为非开发人员不知道如何访问或理解VBA代码,所以这些代码必须是可见的,以确认您看到的数据 我希望:透视表过滤器以列表形式相互覆盖: 我目前得到的: 这里是一个简化版的代码…请让我知道,如果有更好的方法做这件事 我尝试在excel中使用“录制宏”按钮,并按照我希望看到的格式设置所有内容,但一旦我实际运行宏,过滤器将并排运行,而不是彼此重叠 Sub Macro5() Columns("A:A").Select

基本上,我有多个选项卡,每个选项卡都有多个枢轴,每个枢轴都内置了多个过滤器。因为非开发人员不知道如何访问或理解VBA代码,所以这些代码必须是可见的,以确认您看到的数据

我希望:透视表过滤器以列表形式相互覆盖:

我目前得到的:

这里是一个简化版的代码…请让我知道,如果有更好的方法做这件事

我尝试在excel中使用“录制宏”按钮,并按照我希望看到的格式设置所有内容,但一旦我实际运行宏,过滤器将并排运行,而不是彼此重叠

Sub Macro5()
    Columns("A:A").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False


    Dim dataname As String
    Dim datasheetname As String
    Dim pivotsheetname As String

    dataname = ActiveSheet.ListObjects(1).Name
    datasheetname = ActiveSheet.Name
    pivotsheetname = datasheetname & " Pivot"


    Sheets.Add

    ActiveSheet.Name = pivotsheetname

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        dataname, Version:=6).CreatePivotTable TableDestination:= _
        "'" & pivotsheetname & "'!R3C1", TableName:="PivotTable15", 
    DefaultVersion:=6
    Sheets(pivotsheetname).Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable15")
        .ColumnGrand = False
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = 2
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = False
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 3
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlTabularRow
    End With
    With ActiveSheet.PivotTables("PivotTable15").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable15").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("PivotTable15").PivotFields("Billable?")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable15").PivotFields("Billed")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable15").PivotFields("Amount")
    enter code here       .Orientation = xlPageField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable15").AddDataField 
    ActiveSheet.PivotTables( _
        "PivotTable15").PivotFields("Qty"), "Sum of Qty", xlSum
End Sub

过滤器字段的顺序由参数
PageFieldOrder
定义,可以是:

  • xlOverThenDown
    (=2,您以前的结果)
  • xlDownThenOver
    (=1,您想要的结果)
我还优化了您的代码:

  • 通常情况下,我没有必要
  • 我添加了两个变量来引用数据透视缓存和数据透视表对象
  • 可以添加与其他数据透视字段类似的数据字段,但之后必须设置其名称


我不明白你现在的行为和你预期的行为-你能再上传一次照片吗?“由于非开发人员不知道如何访问或理解VBA代码,因此这些代码必须是可见的,以确认您看到的数据。”这对我来说毫无意义-为什么您的用户会看到任何VBA代码?嗨,讽刺的是,我添加了所需结果的图像与我从代码中获得的图像。很抱歉措辞混乱。图像显示了数据过滤器,这些过滤器需要可见,以便最终用户可以进行调整,或者仅仅知道整个数据是如何过滤的。基本上,我使用相同的数据集创建5个单独的选项卡,其中每个选项卡显示相同的数据集,但过滤方式不同。因此,当查看每个选项卡时,最终用户可以通过查看顶部的过滤器来了解差异。我不认为这很容易,但是的,这是可行的。非常感谢。
Sub GenerateNewPivottable()
    Dim datasheetname As String
    Dim dataname As String
    Dim pivotsheetname As String
    Dim pc As PivotCache
    Dim pt As PivotTable

    Application.CutCopyMode = False

    dataname = ActiveSheet.ListObjects(1).Name
    datasheetname = ActiveSheet.Name
    pivotsheetname = datasheetname & " Pivot"

    Sheets.Add
    ActiveSheet.Name = pivotsheetname

    Set pc = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=dataname)
    With pc
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault ' better: xlMissingItemsNone
    End With

    Set pt = pc.CreatePivotTable( _
        TableDestination:="'" & pivotsheetname & "'!R3C1", _
        TableName:="PivotTable15")

    With pt
        .ColumnGrand = False
        .HasAutoFormat = True
        .DisplayErrorString = False
        .DisplayNullString = True
        .EnableDrilldown = True
        .ErrorString = ""
        .MergeLabels = False
        .NullString = ""
        .PageFieldOrder = XlOrder.xlDownThenOver
        .PageFieldWrapCount = 0
        .PreserveFormatting = True
        .RowGrand = False
        .SaveData = True
        .PrintTitles = False
        .RepeatItemsOnEachPrintedPage = True
        .TotalsAnnotation = False
        .CompactRowIndent = 3
        .InGridDropZones = False
        .DisplayFieldCaptions = True
        .DisplayMemberPropertyTooltips = False
        .DisplayContextTooltips = True
        .ShowDrillIndicators = True
        .PrintDrillIndicators = False
        .AllowMultipleFilters = False
        .SortUsingCustomLists = True
        .FieldListSortAscending = False
        .ShowValuesRow = False
        .CalculatedMembersInFilters = False
        .RowAxisLayout xlTabularRow
        .RepeatAllLabels xlRepeatLabels
    End With


    With pt.PivotFields("Billable?")
        .Orientation = xlPageField
        .Position = 1
    End With
    With pt.PivotFields("Billed")
        .Orientation = xlPageField
        .Position = 1
    End With
    With pt.PivotFields("Amount")
        .Orientation = xlPageField
        .Position = 1
    End With

    With pt.PivotFields("Qty")
        .Orientation = xlDataField
        .Function = xlSum
        .Name = "Sum of Qty"
    End With

End Sub