Delete a Query from Excel Workbook with Vba

Delete a query from excel workbook with Vba

Try adding the following line.

The Activeworkbook.Queries() takes a Name or Index

ActiveWorkbook.Queries("Query1").Delete

Delete all queries in a workbook

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

Delete unwanted queries in VBA

You could use Application.Match with the query name.

If IsError(Application.Match(qr.Name, queries, 0)) Then ' query name is not in list


Related Topics



Leave a reply



Submit