title: Error '1004': Unable to set the Visible property of the PivotItem class
body: <p>i got the below code from here: <a href="https://stackoverflow.com/questions/14387938/looping-through-report-filters-to-change-visibility-doesnt-work">Looping through report filters to change visibility doesn&#39;t work</a> where the solution is marked as working. After modifying according to my need, it is like this:</p>
  
  <pre><code>With pt.PivotFields(6)
      .ClearAllFilters
       If .PivotItems.Count &gt; 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 &lt;&gt; 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 &lt; 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 &gt; 2000 Then
                    firstPi.Visible = True
                Else
                    firstPi.Visible = False
                End If
             End If
      End With
  </code></pre>
  
  <p>I see that the whole code is working fine and I'm facing error only the lines <code>pi.Visible = True</code> or <code>pi.Visible = False</code></p>
  
  <p>I'm not sure where I've done wrong for the code not to work.</p>
  
  <p>When I was searching internet for the soltuion, I came across this link: <a href="https://support.microsoft.com/en-us/kb/114822" rel="nofollow noreferrer">https://support.microsoft.com/en-us/kb/114822</a> where MS mentioned that <strong>Only</strong> <strong>contiguous items in a PivotTable Field can be hidden.</strong>
  Does that mean that items in my table are not contiguous? Can anyone help me? I'm lost here.</p>