vba 使用vba过滤数据透视表中的行标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16567604/
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
Filtering row labels in pivot table using vba
提问by Francisco Corrêa
I have searched a lot of forums but still haven't found the answer for this:
我已经搜索了很多论坛,但仍然没有找到答案:
I am trying to filter my row label which varies from 1 to 10, in order to only show me 5 and 10.
我正在尝试过滤从 1 到 10 不等的行标签,以便只显示 5 和 10。
I wrote the following code, but it produces error 1004 "Unable to get the PivotFields property of the PivotTable class".
我编写了以下代码,但它产生错误 1004“无法获取 PivotTable 类的 PivotFields 属性”。
ActiveWorkbook.Sheets("SideCalculations-KPIs").Activate
With ActiveSheet.PivotTables("PivotTable1").PivotFields("ToStateId")
.ClearAllFilters
.PivotItems("5").Visible = True
.PivotItems("10").Visible = True
End With
thanks in advance
提前致谢
回答by Vikas
It is clearly mentioned in the error, the name of the PivotField is incorrect.
错误中明确提到,PivotField 的名称不正确。
The possibility is that the Name of the Pivot Field has been changed from "ToStateId". In order to find the appropriate name, please run the following code:
可能是数据透视字段的名称已从“ToStateId”更改。为了找到合适的名称,请运行以下代码:
For each pField in ActiveSheet.PivotTables("PivotTable1").PivotFields
Debug.Print pField.Name
Next pField
Go to VBA Editor, press Ctrl+G, it will open the immediate window. It will show all the available pivot fields.Then please use the correct Pivot Field and try.
转到 VBA 编辑器,按 Ctrl+G,它将打开立即窗口。它将显示所有可用的数据透视字段。然后请使用正确的数据透视字段并尝试。
回答by Our Man in Bananas
you could try something like this:
你可以尝试这样的事情:
dim pvItem as pivotitem
For Each pvtitem In Sheets("SideCalculations-KPIs").PivotTables("PivotTable1").PivotItems
if (pvitem.name="5" or pvitem.name="10) then
pvitem.visible=true
else
pvitem.visible=false
end if
Next