简体   繁体   中英

Unable to set Visible property of the PivotItem class

I wrote a VBA script to loop over several pivot tables and change the month according to user input. It worked a few times during testing it, but now it suddenly throws out the error in the title. I can´t figure out why.

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

It doesn´t work with the loop over the array and without it as well. Any ideas what I did wrong?

You must have at least one PivotItem visible - you get that error when you try to hide the last visible one (so it fails unless the items you're showing comes before the last one you try to hide).

Try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM