How to remove all column fields in a Pivot Table ( Power Pivot)?

我是研究僧i 提交于 2019-12-24 19:34:08

问题


everyone.

I was using the Sub ClearColumns to clear all field in the column area from a Pivot Table.

Sub ClearColumns()

Dim PT As PivotTable, PF As PivotField

Set PT = ActiveSheet.PivotTables("Tb1")

    With PT
    For Each PF In .ColumnFields
        PF.Orientation = xlHidden
    Next PF
End With
Set PT = Nothing

End Sub

But now that i started using PowerPivot, the code doesnt Work. I think the fields are no longer PivotField, but CubeFields. It Is possible to make the same macro above, but for Cubefields? I tried different things, but i cant make it work.

Sub ClearColumnsPP()

Dim PT As PivotTable, PF As CubeFields
Dim strSource As String
Dim strName As String

    Set PT = ActiveSheet.PivotTables("Tb1")

With PT

    For Each PF In .ColumnFields
        strSource = PF.SourceName
        strName = PF.Name
        PF(strSource, strName).Orientation = xlHidden
    Next PF
End With

    Set PT = Nothing

    End Sub

Thanks!!


回答1:


You need to check the orientation of the CubeField, and only set to hidden if it is a Column field:

Sub foo()
    Dim pt As PivotTable
    Dim cf As CubeField

    Set pt = ActiveSheet.PivotTables("Tb1")
    For Each cf In pt.CubeFields
        If cf.Orientation = xlColumnField Then cf.Orientation = xlHidden
    Next cf
End Sub


来源:https://stackoverflow.com/questions/45759538/how-to-remove-all-column-fields-in-a-pivot-table-power-pivot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!