【问题标题】:Unable to set Visible property of the PivotItem class无法设置 PivotItem 类的 Visible 属性
【发布时间】:2021-03-24 17:55:35
【问题描述】:

我编写了一个 VBA 脚本来遍历多个数据透视表并根据用户输入更改月份。它在测试期间工作了几次,但现在它突然抛出标题中的错误。我不知道为什么。

Sub change_pivot()
Dim Wb As Workbook
Dim Ws As Worksheet:
Dim p As PivotTable:
Dim f As PivotField:
Dim i As PivotItem, s$
Dim i2 As PivotItem
Dim Message, Title, Default, MyValue
Dim curr_year As Integer
Dim pvt_tables As Variant

Set Wb = ThisWorkbook
Set Ws = Wb.Worksheets("Sheet1")
Set p = Ws.PivotTables("PivotTable1")
Set f = p.PivotFields("Month")
Set g = p.PivotFields("Year")

curr_year = year(Date)

pvt_tables = Array("1", "10", "3", "12", "11", "4", "5")

Message = "Enter a month value in the folowing format: (01)"
Title = "Insert Month"    ' Set title.
Default = "01"    ' Set default.
' Display message, title, and default value.
s = InputBox(Message, Title, Default)



Message = "Is the year correct?"
Title = "check year"    ' Set title.
Default = curr_year   ' Set default.
' Display message, title, and default value.
y = InputBox(Message, Title, Default)

        
For Each x In pvt_tables
    Set p = Ws.PivotTables("PivotTable" & x)
    Set f = p.PivotFields("Month")


' change months
    With f
        For Each i In .PivotItems
            If i.Name <> s Then
                i.Visible = False
            Else:
                i.Visible = True
            End If
        Next
    End With
Next

它不适用于数组上的循环,没有它也一样。任何想法我做错了什么?

【问题讨论】:

  • 您在循环中执行此操作 Set p = Ws.PivotTables("PivotTable" &amp; x) 但随后您不使用 p
  • p 在 f 中“包含”。我本可以在 ine 步骤中完成此操作,但出于某种原因我使用了 2
  • 但是 f 只指向PivotTables("PivotTable1").PivotFields("Month") 稍后更改p 不会自动更改f...
  • 好吧,我可能不明白。我编辑了上面的代码。出于某种原因,它曾经工作过一次,但是当我第二次尝试时,我又遇到了同样的错误......我必须在哪里再次使用 p?

标签: arrays excel vba date pivot


【解决方案1】:

您必须至少有一个 PivotItem 可见 - 当您尝试隐藏最后一个可见的时会收到该错误(因此它会失败,除非您显示的项目出现在最后一个之前你试图隐藏)。

试试这样的:

Sub change_pivot()
    Dim Wb As Workbook
    Dim Ws As Worksheet
    Dim p As PivotTable
    Dim f As PivotField
    Dim i As PivotItem, mnth As String, yr As String
    Dim i2 As PivotItem
    Dim pvt_tables As Variant, x, pi As PivotItem
    
    Set Wb = ThisWorkbook
    Set Ws = Wb.Worksheets("Sheet1")
    
    mnth = InputBox("Enter a month number", "Month", "01")
    If Len(mnth) = 1 Then mnth = "0" & mnth 'add leading zero if not entered
    
'    yr = InputBox("Is the year correct?", _
'                 "Insert Month", Year(Date))
    
    pvt_tables = Array("1", "10", "3", "12", "11", "4", "5")
    
    For Each x In pvt_tables
        Set p = Ws.PivotTables("PivotTable" & x)
        With p.PivotFields("Month")
            On Error Resume Next
            Set pi = .PivotItems(mnth) 'check the entered item exists
            On Error GoTo 0
            If Not pi Is Nothing Then
                pi.Visible = True         'first set desired item visible
                For Each i In .PivotItems 'then hide the others
                    If i.Name <> mnth Then i.Visible = False
                Next
            Else
                MsgBox "No item exists for '" & mnth & "'"
            End If
        End With
    Next

End Sub

【讨论】:

  • 是的,这正是问题所在,感谢您的出色解决方案!
猜你喜欢
  • 2012-07-13
  • 2016-11-29
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多