title: Loop through PivotItems: runtime error 91
body: <p>I have a dataset in a worksheet that can be different every time. I am creating a pivottable from that data, but it is possible that one of the PivotItems is not there. For example:</p>
  
  <pre><code>.PivotItems("Administratie").Visible = False
  </code></pre>
  
  <p>If that specific value is not in my dataset, the VBA script fails, saying that it can't define the item in the specified Field. (error 1004)</p>
  
  <p>So I thought a loop might work. 
  I have the following: </p>
  
  <pre><code>Dim pvtField As PivotField
  Dim pvtItem As PivotItem
  Dim pvtItems As PivotItems
  
  For Each pvtItem In pvtField.pvtItems
          pvtItem.Visible = False
  Next
  </code></pre>
  
  <p>But that gives me an 91 error at the For Each pvtItem line:</p>
  
  <pre><code>Object variable or With block variable not set
  </code></pre>
  
  <p>I thought I declared the variables well enough, but I am most likely missing something obvious... </p>