Pivot Row Filter:如果pivotitem是值不相等的值列表

时间:2016-03-22 13:36:38

标签: excel vba excel-vba pivot pivot-table

在编写vba代码以使用户能够通过单击将数据透视表更改为特定条件时,我遇到了一个小问题。我希望有人有解决方案或想法。

我需要过滤列表的所有帐户,以便仅提供咨询费用。我知道你基本上必须“取消选择”你不想看到的所有标准/值。问题是,有很多帐号需要花费很长时间才能记下每个值.PivotItems("90000000").Visible = False。我相信有一个优雅的解决方案。

有没有办法获得类似if PivotItem<> [several values with OR operator] Then Visible = False的内容?

除此之外,还有另一个障碍:有时帐户没有值,因此没有显示在余额列表中(rawdata / pivotsource)。如果我现在有PivotItem("x").Visible = False且X不存在,则子程序会出错。如果某个值不存在,有没有办法绕过这种情况,忽略它?

这是设置: 数据透视表基本上包含一个余额列表,其中包含列中的句点(月),行中的帐户和帐户说明以及页面域中的合作伙伴ID。

到目前为止的代码如下

Sub Pivot_FS10N_consulting()
Dim pvt As PivotTable
Dim pf As PivotField

Application.ScreenUpdating = False

Sheets("FS10N Pivot").Range("A8").clear

Set pvt = Sheets("FS10N Pivot").PivotTables("PivotTable2")
With pvt
    .ClearTable
    .AddDataField pvt.PivotFields("Value in local currency"), "Value", xlSum

        With .PivotFields("Partner-ID")
            .Orientation = xlPageField
            .Position = 1
            .EnableMultiplePageItems = True
            .PivotItems("246").Visible = False
            .PivotItems("247").Visible = False
            .PivotItems("457").Visible = False
            .PivotItems("631").Visible = False
            .PivotItems("(blank)").Visible = False
        End With

        With .PivotFields("Period")
            .Orientation = xlColumnField
            .Position = 1
        End With

        With .PivotFields("Account")
            .Orientation = xlRowField
            .Position = 1

            'Filter criteria should be here right now

        End With

        With .PivotFields("Account description")
            .Orientation = xlRowField
            .Position = 2
        End With

    For Each pf In .PivotFields
    pf.Subtotals(1) = False
    Next pf

    .ColumnGrand = True
    .RowGrand = True
    .DataBodyRange.NumberFormat = "#,##0.00;-#,##0.00"
    .RowAxisLayout xlTabularRow
    .TableStyle2 = "FS10N"
End With

With Sheets("FS10N Pivot").Range("A8")
    .Font.Bold = True
    .Font.ThemeColor = xlThemeColorDark1
    .Interior.Color = 1200359
    .Interior.Pattern = xlSolid
    .Value = "Consulting"
End With

End Sub

提前多多感谢。非常感谢任何帮助。

编辑澄清: 我需要选择所有不等一组帐号或帐号列表,以避免在ERP中创建新帐户并且我在代码中没有帐户时保持帐户可见的风险。而且我有一种错觉,就是每个月都没有检查数字的有效性。 如果我将帐号复制到另一张纸上或同一张纸上,我可以参考一系列单元格进行检查吗?然后它只需要检查数字是否在那里,如果不是这样,则可见性应设置为false。

系统信息: 操作系统:Windows 7 Excel 2007 64位

2 个答案:

答案 0 :(得分:0)

将您要保留的所有帐号存储在脚本词典中。然后遍历.PivotItems并检查字典中是否存在每个.PivotItems。如果没有,请隐藏该项目。

Sub test()

'this Dictionary will store the account numbers we want to NOT hide
Dim hideAccounts As Object
Set hideAccounts = CreateObject("Scripting.Dictionary")

'say, for example, the accounts you want to hide are on sheet one cells 1--20
'loop through the cells and add the accounts to the collection
With Sheets(1).Range("A1:A20")
    For i = 1 To .Count
        hideAccounts.Add .Cells(i),.Cells(i) 'adding to the dict in the format key, value
    Next i
End With

'
'...insert some code to create a pivot table
'
With myPivot
    for each itm in .PivotItems
        if not hideAccounts.Exists(itm) then 'if it does not exist in the dictionary
            .PivotItems(itm).visible=False'set it to invisible
        end if
    next

End With


End Sub

答案 1 :(得分:0)

我已经写了两篇关于这类事情的帖子,这些帖子也为你提供了一种更快的方法来取消选择大量的PivotItem而无需迭代。

http://dailydoseofexcel.com/archives/2013/11/14/filtering-pivots-based-on-external-ranges/

http://dailydoseofexcel.com/archives/2013/12/03/inversely-filter-a-pivot-based-on-an-external-range/

另请注意,似乎存在一个错误,即过滤包含切片器的Pivots可能会非常慢。在这里看我的帖子: http://dailydoseofexcel.com/archives/2015/11/17/filtering-pivottables-with-vba-deselect-slicers-first/