Delete Queries in a workbook

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
Hi I am looking for a way to delete queries in a workbook.

I have code that creates a copy of sheets in the active workbook then putting them into a created workbook, but when creating that new workbook it brings over the queries too. Is there a way to not bring over the queries as well or delete the queries in that created workbook.

i use

thisworkbook.worksheets(sheet1).copy before:=ActiveWorkbook.Worksheets("Sheet1")

to create a sheet, i do this multiple times to do different sheets.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I use this to delete the queries when no longer needed.
VBA Code:
Sub ClearQueries()

Dim pq As Object

Dim q As String

For Each pq In ThisWorkbook.Queries

q = pq

ActiveWorkbook.Queries(q).Delete

Next

End Sub
 
Upvote 0
I use this
VBA Code:
Sub ClearQueries()

Dim pq As Object

Dim q As String

For Each pq In ThisWorkbook.Queries

q = pq

ActiveWorkbook.Queries(q).Delete

Next

End Sub

I use this to delete the queries when no longer needed.
VBA Code:
Sub ClearQueries()

Dim pq As Object

Dim q As String

For Each pq In ThisWorkbook.Queries

q = pq

ActiveWorkbook.Queries(q).Delete

Next

End Sub
is there anything to add if it is not finding a query in the activeworkbook.delete? getting a run time error where it cannot find a query by the name
 
Upvote 0
Do you have other Objects in the Workbook? i.e. - ListBoxes, Pictures, etc. ?
 
Upvote 0
is there anything to add if it is not finding a query in the activeworkbook.delete? getting a run time error where it cannot find a query by the name
In @johnny51981's code change the ThisWorkbook line to Activeworkbook.
Note: Make sure you are working on a copy of your workbook or have a backup.

Rich (BB code):
For Each pq In ActiveWorkbook.Queries
 
Upvote 0
I am still getting a run time error, would there be a way to just copy the sheet without the query? The table within the sheet is created by the query, is there a way to copy the values over and the format?


I assume the code i used in the post copies the whole sheet and the includes the query if it has a table created by a query.
 
Upvote 0
I can see that @johnny51981's delete code doesn't seem to fully remove all the query links.
If you are confident that there are no queries in the "created workbook" that you need to keep, then try the code below.

You can probably copy all your sheets across first then once done run this

VBA Code:
Sub ClearQueriesAndConnections()

    Dim wbCreated As Workbook
    Set wbCreated = ActiveWorkbook

    ' Delete all connections
    Dim cn As WorkbookConnection

    For Each cn In wbCreated.Connections
        cn.Delete
    Next cn

    ' Delete all Queries
    Dim pq As Object
    Dim q As String

    For Each pq In wbCreated.Queries
        q = pq
        wbCreated.Queries(q).Delete
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top