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.
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.
The problem When I record the macro, it makes a static name for each query with
ActiveWorkbook.Queries.Add
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.
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.
Dim qry As WorkbookQuery
If DoesQueryExist(TS) Then
' Deleting the query
Set qry = ThisWorkbook.Queries(TS)
qry.Delete
End If
I placed this inside my macro sub.
I wasn't able to find very much on the function "DoesQueryExist".
Is there another solution to instead create unlimited queries with arbitrary names instead of having to check and delete?