title: Expand only those pivot items with more than one entry (Excel VBA)
body: <p>Ok, so I am trying to have Excel VBA code go through a pivot table and expand out only those entries in the last row field that don't have ONLY one entry in them. By default, the last row field is collapsed.</p>
  
  <p>What I have so far:</p>
  
  <pre><code>Sub Test()
      With ActiveSheet.PivotTables("OBJSUMMARYPT")
          Dim pvtitem4, pvtitem5  As PivotItem
          For Each pvtitem4 In .RowFields("OBJECT CODE GROUPING").PivotItems
              For Each pvtitem5 In .RowFields("CONTRACT &amp; CONTRACT TITLE").PivotItems
                  If pvtitem5.Name &lt;&gt; "NON-CONTRACT" And pvtitem4.RecordCount &gt; 0 And pvtitem5.RecordCount &gt; 0 Then
                      .PivotFields("OBJECT CODE GROUPING").PivotItems(pvtitem4.Name).ShowDetail = True
                  End If
              Next pvtitem5
          Next pvtitem4
      End With
  End Sub
  </code></pre>
  
  <p>This expands too many of them, because it isn't cross-referencing if there are any pvtitem5s in pvtitem4 that have records, and I am not figuring out the syntax to do so.</p>
  
  <p>I recognize that single entries that aren't listed as "NON-CONTRACT" will be shown and I don't take issue with that; that is fine to happen. I want to get fewer false positives than what I am getting now.</p>