vba 无法获取 PivotTable 类的 PivotFields 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18408649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:56:47  来源:igfitidea点击:

Unable to get PivotFields property of the PivotTable class

vbaexcel-vbafieldpivot-tableexcel

提问by Miticus

I'm trying to make a macro that changes the filters of several PivotTables (not all), but i'm getting an error on the PivotFields, here's a sample of my code:

我正在尝试制作一个宏来更改几个数据透视表(不是全部)的过滤器,但我在数据透视表上遇到错误,这是我的代码示例:

Sheets("Sheet1").PivotTables("PivotTable" & pivot_counter).PivotFields( _
"[year].[year].[year]").VisibleItemList = Array("")

My questions are:

我的问题是:

1- Why do we use PivotFields("[year].[year].[year]") when using VisibleItemList? Why do we have to repeat it and what's the meaning of it, couldn't anything about it anywhere.

1- 为什么我们在使用 VisibleItemList 时使用 PivotFields("[year].[year].[year]")?为什么我们必须重复它以及它的含义是什么,在任何地方都没有任何关系。

2- What is supposedly wrong with this code? My pivot table has a field called "year" and the filter is set for a specific year (let's say 2013) and i wanted it change for all possible years.

2- 这段代码有什么问题?我的数据透视表有一个名为“年份”的字段,过滤器设置为特定年份(假设是 2013 年),我希望它在所有可能的年份都发生变化。

回答by Tim Williams

Sub Tester()
    ShowAll ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
End Sub


Sub ShowAll(pf As PivotField)
    Dim pi As PivotItem
    Application.ScreenUpdating = False
    For Each pi In pf.PivotItems
        pi.Visible = True
    Next pi
    Application.ScreenUpdating = True
End Sub