title: .PivotCache.Refresh not working
body: <p>I included all the code in case there was something that is causing this but I dont think there is. If I step through the code the last line works properly. If I run it in real time it does not. Any thoughts?</p>
  
  <pre><code>Sub Updatequery()
  For a = 1 To 2
  oldconnstring = ActiveWorkbook.Connections(a).ODBCConnection.Connection
  Debug.Print oldconnstring
  P1 = Left(oldconnstring, 9)
  P2 = Mid(oldconnstring, InStr(10, oldconnstring, ";"), 8)
  P3 = Mid(oldconnstring, (InStr(10, oldconnstring, "DATABASE") - 11), 20)
  P4 = Mid(oldconnstring, (InStr(10, oldconnstring, "AUTHENTIC") - 1), 200)
  O1 = Worksheets("Data").Cells(2, 4).Value
  O2 = Worksheets("Data").Cells(2, 5).Value
  newconnstring = P1 &amp; O2 &amp; P2 &amp; O1 &amp; P3 &amp; Replace(O2, "_", "") &amp; P4
  Debug.Print oldconnstring
  With ActiveWorkbook.Connections(a).ODBCConnection
      .Connection = newconnstring
   End With
  
  Debug.Print ActiveWorkbook.Connections(a).ODBCConnection.Connection
  ActiveWorkbook.Connections(a).Refresh
  DoEvents
  Next a
  DoEvents
  Sheets("Summary").Select
  Range("A1").Select
  ActiveSheet.PivotTables("SummaryP").PivotCache.Refresh
  </code></pre>