【问题标题】:Excel VBA identify pivotItems visible in excel interfaceExcel VBA识别excel界面中可见的pivotItems
【发布时间】:2019-03-30 12:53:23
【问题描述】:

我的数据透视表包含一个过滤器,用于过滤带有"Days_Outstnading_Category"=">180 days" 的项目。

过滤后,我的行字段"Operating Units"只有2个项目显示在excel界面中,“美国”和“印度”,虽然“运营单位”中的所有项目都有Visible=True,但实际上只有2个项目可见在excel界面中。

我想知道如何识别这两个项目,因为 If pivotItem.Visible=True 现在无法使用。

最终我想迭代并选择出现在 excel 界面中的每个项目的 LabelRange(A5:A12、A14:A27、B5、B6:B9 等)并对每个范围进行一些格式更改。我下面的代码带来了一个找不到labelrange的错误。

> For Each ptRowField In pt.RowFields
>      For Each ptRowFieldItem In ptRowField.PivotItems
>          If ptRowFieldItem.Visible = True Then
>              Call SetOusideBorder(ptRowFieldItem.LabelRange)
>          End If
>      Next ptRowFieldItem 
> Next ptRowField

【问题讨论】:

    标签: excel vba pivot-table


    【解决方案1】:

    我用过的最简单的方法是抓取数据透视表中包含行标签的列。

    在我的示例数据集中,我有七种颜色:红色、橙色、黄色、绿色、蓝色、靛蓝和紫色。我已经设置了数据并创建了一个数据透视表,然后对其进行了过滤,因此只显示了三种颜色:

    因此,您可以使用 .RowRange 属性访问数据透视表中的行标签列。一旦你有了它,你就可以降级单元格:

    Option Explicit
    
    Public Sub ListVisibleItems()
        Dim thisPivot As PivotTable
        Set thisPivot = ActiveSheet.PivotTables(1)
    
        Dim rowField As PivotField
        Set rowField = thisPivot.RowFields("Color")
    
        Dim theseRows As Range
        Set theseRows = thisPivot.RowRange
    
        Dim i As Long
        '--- start at 2 to skip the "Row Labels" and end at Count-1
        '    to skip the "Grand Total" 
        For i = 2 To theseRows.Count - 1
            Debug.Print theseRows.Cells(i, 1) & " is visible"
        Next i
    End Sub
    

    编辑:更新示例代码以解析 OP 的示例数据

    Option Explicit
    
    Public Sub ListVisibleItems()
        Dim thisPivot As PivotTable
        Set thisPivot = ActiveSheet.PivotTables(1)
    
        Dim rowField As PivotField
        Set rowField = thisPivot.RowFields("Color")
    
        Dim theseRows As Range
        Set theseRows = thisPivot.RowRange
    
        Dim i As Long
        '--- start at 2 to skip the "Operating Units" and end at Count-1
        '    to skip the "Grand Total" 
        For i = 2 To theseRows.Count - 1
            '--- check to make sure the row is not empty and that
            '    the value does NOT have the word "Total"
            If Len(theseRows.Cells(i, 1)) > 0 Then
                If Not theseRows.Cells(i, 1) Like "*Total" Then
                    Debug.Print theseRows.Cells(i, 1) & " is visible"
                    '--- you can use this result as the field name
                    '    to select and format the data area
                End If
            End If
        Next i
    End Sub
    

    【讨论】:

    • 感谢 Peter 提供了一种很好的迭代方式。但是,对于我自己的数据(对不起,它很敏感,所以我不能截图),一个字段项可以占据不同的行,第一行中的项目名称和其余行为空。我想选择属于该特定项目的所有行并进行一些格式更改。我使用的布局也是表格的。希望我说清楚了。
    • 如果您可以制作一个示例作为屏幕截图,那将有所帮助。它甚至不必是一个真正的数据透视表,只需一小部分格式化为看起来像一个的单元格来说明您的问题。更改字段中的名称以使用颜色或动物名称或其他内容。我可以提供更好的指导。
    • 感谢您的示例。去阅读 Jon Peltier's page on pivot tables 并特别查看 DataRangeLabelRange 看看是否有帮助。
    • 其实我以前读过。在他的示例中,我必须指定数据透视项目的名称以获取标签范围,例如“pt.PivotFields("Years").PivotItems("2004").LabelRange.Select"。我现在正在尝试找出项目的名称并输入此函数,因为如果某些项目在 excel 界面中不可见,则它们没有标签范围。
    • 我已经更新了答案,希望能解决您的问题
    猜你喜欢
    • 2018-05-18
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2017-06-30
    • 2016-10-22
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    相关资源
    最近更新 更多