I am trying to write a sub to delete all queries in a workbook. I have got the code below:
Dim CN As Variant
Dim qTable As QueryTable
For Each CN In ThisWorkbook.Connections
CN.Delete
Next CN
For Each qTable In Sheets("Property Extract 1").QueryTables
qTable.Delete
Next qTable'
The connection deletion works fine but the queries remain. Any ideas how to delete all queries in a workbook?
I was planning to replicate the query deletion for 2 or 3 sheets.
thanks
Just open the table in Datasheet view, select the fields (columns) or records (rows) that you want to delete, and then press DELETE.
Firstly select one of the connections to be deleted and press the delete button. The dialog box will pop up again and tell you which table it is connected to. Close the dialogue and go to the Power Pivot Diagram View window. Find the table and take note of the relationships that exist.
You may find the Queries & Connections pane is more convenient to use when you have many queries in one workbook and you want to quickly find one. In Excel, select Data > Queries & Connections, and then select the Queries tab.
First off, you cannot delete PQ Queries from VBA unless you are on Excel 2016/365. PQ is not exposed to VBA in earlier versions even when the add-on is installed. If you have the right version of Excel, you can delete the PQ Queries themselves just like you are clearing the connections:
Dim pq As Object
For Each pq In ThisWorkbook.Queries
pq.Delete
Next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With