title: VBA—"Error 1004: Unable to get the CurrentPage property of the PivotField class"
body: <p>I have an Excel workbook containing multiple worksheets with multiple pivot tables on each worksheet. I'm attempting to loop through every pivot table and change its 'Report Function' filter to a user-specified value selected from a List Box. All the pivot tables have the "Report Function" filter. </p>
  
  <p>I have around 50 pivot tables, and the code successfully changes the filters for all but around 8 pivots. Those 8 pivot tables' filters don't change; instead, the error "Error 1004: Unable to get the CurrentPage property of the PivotField class" is returned. </p>
  
  <p>wHY is this code returning this error for a small number of pivot tables? </p>
  
  <p>Strangely, it seems to be the exact same pivot tables that are returning this error.</p>
  
  <pre><code>Sub ChangePivotFilter(str As String)
  
  Dim ws As Excel.Worksheet
  Dim aWB As Excel.Workbook
  Dim myPivot As Excel.PivotTable
  Dim myPivotField As Excel.PivotField
  
  Set aWB = ActiveWorkbook
  For Each ws In aWB.Worksheets
  
      For Each myPivot In ws.PivotTables
          Set myPivotField = Nothing
  
          On Error Resume Next
          Set myPivotField = myPivot.PivotFields("Report Function")
          myPivotField.CurrentPage = str
      Next myPivot
  Next ws
  
  End Sub
  </code></pre>
  
  <p>Any help is much appreciated!</p>