【问题标题】:Error '1004': Unable to set the Visible property of the PivotItem class错误“1004”:无法设置 PivotItem 类的 Visible 属性
【发布时间】:2016-11-29 01:05:36
【问题描述】:

我从这里得到了以下代码:Looping through report filters to change visibility doesn't work,其中解决方案被标记为有效。根据我的需要修改后是这样的:

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 提到 只有 可以隐藏数据透视表字段中的连续项目。 这是否意味着我表中的项目不连续?谁能帮我?我迷路了。

【问题讨论】:

  • 你表中的项目是连续的吗?我怀疑 SO 上的任何人都可以回答这个问题。
  • 如何判断是否连续?我不知道注册这个
  • 我可以查看您的 Excel 文件吗?如果是,那么请将其上传到免费的文件共享站点并在此处共享链接。还要确保如果您有任何机密数据,则将其替换为虚拟数据。
  • 嗨@SiddharthRout 感谢您的回复。但是所有文件共享站点都受到限制。我不能这样做。但是,我找到了解决任务的方法。请找到我发布的答案。

标签: excel hide pivot-table pivotitem vba


【解决方案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())

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-13
      • 2017-05-06
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多