title: How can I use VBA to control the order of elements in an Excel Pivot Table Field List?
body: <p>I have a pivot table, which is intended to summarize my data at two different levels of aggregation depending on selections the user has made.</p>
  
  <p>In its original state, here are the feild lists for my pivot table:
  <a href="https://i.stack.imgur.com/q5lpI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q5lpI.png" alt="enter image description here"></a></p>
  
  <p>When the user changes certain settings, the following code hides IndexA from the field list:</p>
  
  <pre><code>Sheets("test").PivotTables("test").PivotFields("IndexA").Orientation = xlHidden
  </code></pre>
  
  <p>And when the changes are reversed, the following code makes IndexA visible again:</p>
  
  <pre><code>Sheets("test").PivotTables("test").PivotFields("IndexA").Orientation = xlRowField
  </code></pre>
  
  <p>The resulting field list now looks like this, with a modified ordering of the elements of the field list:
  <a href="https://i.stack.imgur.com/Duefw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Duefw.png" alt="enter image description here"></a></p>
  
  <p>Note that in the ROWS field list, IndexA is now at the bottom as opposed to at the top, which destroys the consistency and readability of my report.</p>
  
  <p>My question is.. is there a way to control the ordering of the pivot field list through VBA? or an alternative way to acheive the same result I am looking for? (turning on and off a certain item in the field list and having the order preserved)</p>
  
  <p>I appreciate any help/advice anyone can provide on this topic.</p>