gpt4 book ai didi

vba - 过滤掉数据透视表中的奇数

转载 作者:行者123 更新时间:2023-12-03 02:10:48 25 4
gpt4 key购买 nike

在我的数据透视表的源表中;包含两列;一个用于名称,另一个用于分数。正如线程标题所示,我想显示所有分数及其对应分数的偶数分数。

实现此目的的另一种方法是创建第三列并运行 IF 公式来检查哪些数字是奇数。然后将第三列包含到数据透视表中并将其用作过滤器。

但是有没有办法在不修改源代码的情况下做到同样的事情?

最佳答案

您可以循环遍历枢轴项,然后将它们除以 2 以检查它们是否为偶数,如果不是,则只需隐藏它们即可。请参阅此示例。请修改它以满足您的需要。另外,我还没有做任何错误处理。我相信你能解决这个问题...

代码

Option Explicit

Sub HideOddNumbers()
Dim ws As Worksheet
Dim pvtItem As PivotItem
Dim pvt As PivotTable
Dim pvtFld As PivotField

'~~> Change this to the respective sheet
Set ws = ThisWorkbook.Sheets("Sheet1")

'~~> Change this to the respective pivot name
Set pvt = ws.PivotTables("PivotTable1")

'~~> This is the column
Set pvtFld = pvt.PivotFields("Scores")

With pvtFld
.CurrentPage = "(All)"
.EnableMultiplePageItems = True

'~~> Loop through all items in the pivot and
'~~> divide the values by 2 and check if they are even or not
For Each pvtItem In pvtFld.PivotItems
If Val(pvtItem.Value) Mod 2 <> 0 Then pvtItem.Visible = False
Next
End With
End Sub

屏幕截图

之前:

enter image description here

之后:

enter image description here

关于vba - 过滤掉数据透视表中的奇数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13008551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com