0

我从这里得到了以下代码:循环通过报告过滤器来更改可见性在解决方案标记为有效的情况下不起作用。根据我的需要修改后是这样的:

With pt.PivotFields(6)
    .ClearAllFilters
     If .PivotItems.Count > 0 Then

        'goofy but necessary
        Set firstPi = .PivotItems(1)

        For Each pi In .PivotItems

            If firstPi.Visible = False Then
                firstPi.Visible = True
            End If

             'Don't loop through firstPi
            If pi.Value <> firstPi.Value Then
                itemValue = pt.GetPivotData("[Measures].[Nr of Cancelled]", "[Characteristics].[Reason]", pi.Name).Value

                rw = rw + 1
                nwSheet.Cells(rw, 1).Value = pi.Name
                nwSheet.Cells(rw, 2).Value = pi.Visible
                If itemValue < 2000 Then
                    If pi.Visible = True Then
                        pi.Visible = False 'Error here
                    End If
                Else
                    MsgBox pi.Value
                    If pi.Visible = False Then
                        pi.Visible = True 'Error here
                    End If
                End If
            End If
        Next

              'Finally perform the check on the first pivot item
              If firstPi > 2000 Then
                  firstPi.Visible = True
              Else
                  firstPi.Visible = False
              End If
           End If
    End With

我看到整个代码工作正常,我只面临错误行pi.Visible = Truepi.Visible = False

我不确定我在哪里做错了代码不起作用。

当我在互联网上搜索解决方案时,我遇到了这个链接:https ://support.microsoft.com/en-us/kb/114822 ,其中 MS 提到只能 隐藏数据透视表字段中的连续项目。 这是否意味着我表中的项目不连续?谁能帮我?我在这里迷路了。

4

2 回答 2

1

我没有找到任何错误的解决方案。但我找到了另一种方法来完成这项任务。我使用数组来存储所有要隐藏的项目和要显示的项目,以便我可以调用 HiddenItemsList 或 VisibleItemsList:

    For Each pvtitem In pt.PivotFields(6).PivotItems
        On Error GoTo skipreason
        itemValue = pt.GetPivotData("[Measures].[Cancelled]", "[Characteristics].[Reason]", pvtitem.Name).Value
        If itemValue < 2000 Then
            hiddenReasons(hiddenCount) = pvtitem.Name
            hiddenCount = hiddenCount + 1
        Else
            visibleReasons(visibleCount) = pvtitem.Name
            visibleCount = visibleCount + 1
        End If

Sheets("Cancels").PivotTables("Cancels").PivotFields( _
    "[Characteristics].[Reason].[Reason]" _
    ).VisibleItemsList = Array(visibleReasons())
于 2016-07-27T14:59:06.967 回答
0

这已经晚了 5 年零 6 个月,但我可能在考虑我的脚本时找到了解决方案。当数据透视表必须调整大小并且数据透视表下方有元素时,只能在我这边复制错误。这与调整大小是否会导致它溢出无关(例如,我应用了“前 10 名”过滤器)——事实上,对我来说,表格正试图缩小。一旦我将数据透视表并排而不是垂直堆叠,我就无法再复制错误。

于 2022-02-04T20:18:46.253 回答