title: Unique name for Query/Pivot table
body: <p>I need to create a macro that can be used by other people that will import data monthly and create two pivot tables from two Excel workbooks.</p>
  <p>The best method I devised is to have the person download the two files into the same folder as this macro-enabled workbook and name them the same for the purpose of querying and pulling the data (i.e. Output1 and Output2) then append with the date and move to a storage folder once they have run the macro and gotten the tables they want. (If I am misunderstanding how querying works and the data needs to remain available in the same location and name for the pivot tables to continue existing please let me know.)
  This way I can hardcode the files to pull from and it's minimal effort on their part.</p>
  <p><strong>The problem</strong>
  When I record the macro, it makes a static name for each query with</p>
  <pre><code>ActiveWorkbook.Queries.Add
  </code></pre>
  <p>Which means that when I run it for the second month, I get an error that the query connection already exists and the macro errors out.</p>
  <p>I found this code that is supposed to check if the query already exists, and if it does; delete it, but I can't make it work.</p>
  <pre class="lang-vb prettyprint-override"><code>Dim qry As WorkbookQuery
  
  If DoesQueryExist(TS) Then
      ' Deleting the query
      Set qry = ThisWorkbook.Queries(TS)
      qry.Delete
  End If
  </code></pre>
  <p>I placed this inside my macro sub.</p>
  <p>I wasn't able to find very much on the function &quot;DoesQueryExist&quot;.<br />
  Is there another solution to instead create unlimited queries with arbitrary names instead of having to check and delete?</p>