title: Changing pivot table external data source path with Excel macro
body: <p>I'm working on a project in MS Excel where I have several Pivot Tables that read data from a tab-delimited text file with column names in the first row (in the same directory as the Excel file) using the Microsoft Text Driver. I've run into a problem where when I copy the text and Excel files to a new directory and try to refresh the data it says it can't find the text files. Unfortunately there seems to be no way to tell Excel I want the paths to the text files to be relative, not absolute. All of the pivot tables use the same data connection, so I figured it shouldn't be too challenging to write a macro that would update the data connection to refer to the correct text file and have a button linked to the macro that would update the file paths and refresh the data for me.</p>
  
  <p>I'm not overly familiar with VBA and the online documentation seem to be pretty bad, so I haven't been able to get this working -- I can create the correct file path and can refresh the data, but I haven't been able to figure out how to update the connection to use the new file path but retain all its old import/file parsing settings. I have also tried recording a macro while manually updating the data source, but for some reason that always gives me errors which interrupt the recording, so that hasn't helped.</p>
  
  <p>The following is the connection string and command text currently used by the connection, but there's nothing about how to parse/import the data (the file being tab-delimited or having headers in the first column, etc), so I'm not sure how to make sure the connection keeps that data.</p>
  
  <p>Connection String:</p>
  
  <pre><code>DefaultDir=C:/directoryPath;Driver={Microsoft Text Driver (*.txt; *.csv)};
    DriverId=27;FIL=text;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;
    SafeTransactions=0;Threads=3;UserCommitSync=Yes;
  </code></pre>
  
  <p>Command Text:</p>
  
  <pre><code>SELECT *
  FROM tableName.txt tableName
  </code></pre>
  
  <p>If anyone knows how to write a macro that will update the path in the connection to the text file please share the code, or if you know how to just make the path relative that'd be great too. Any help would be greatly appreciated!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've been messing around with it a bit more and I was able to change the connection strings to use the new path. However, when I go to refresh the pivot table it imports all the data as text instead of guessing whether it should be numeric, etc, (although it does get the column headers from the first line of the text file, at least). Any ideas on how to tell it to guess the data types (or just keep the old data types)? The code I'm using right now is:</p>
  
  <pre><code>Public Sub Test()
      Dim wb As Excel.Workbook
      Dim pc As PivotCache
      Dim path As String
  
      Set wb = ActiveWorkbook
      path = wb.path
  
      For Each pc In wb.PivotCaches
          'Debug.Print pc.Connection
          pc.Connection = "ODBC;DBQ=" &amp; path &amp; ";DefaultDir=" &amp; path &amp; ";Driver={Microsoft Text Driver (*.txt; *.csv)};DriverId=27;FIL=text;MaxBufferSize=2048;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UserCommitSync=Yes"
  
      Next
  End Sub
  </code></pre>