Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all queries in a workbook

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

like image 631
Chris Avatar asked Mar 06 '18 15:03

Chris


People also ask

How do I delete a query table?

Just open the table in Datasheet view, select the fields (columns) or records (rows) that you want to delete, and then press DELETE.

How do I remove a Power Query connection?

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.

How do I find the workbook queries in Excel?

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.


1 Answers

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
like image 196
Wedge Avatar answered Sep 22 '22 07:09

Wedge