title: VBA code in Excel with pivot tables
body: <p>The data source will change often. After I refresh the pivot table, I have the following code to update the grade pivot item field. Sometimes grade 4 is available and sometimes it is not. Essentially, If grade 4 is available I want it to be selected and if it is not available then all fields can be select. For some reason when I run it, it stops on the else line. Any suggestions? </p>
  
  <pre><code>ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").ClearAllFilters
  
  If IsError(ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4") Then
      ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = _
      "(All)"    
  Else  
      ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4"           
  End If
  </code></pre>