Question: 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

Answer: 
Just add another loop to loop through sheets.

```
Dim cn
Dim qt As QueryTable
Dim ws As Worksheet
For Each cn In ThisWorkbook.Connections
    cn.Delete
Next
For Each ws In ThisWorkbook.Worksheets
    For Each qt In ws.QueryTables
        qt.Delete
    Next
Next ws

```

**Edit:** If you are adding using Query Wizard then perhaps you'd need modified code below.

```
Dim cn
Dim qt As QueryTable
Dim lo As ListObject
Dim ws As Worksheet
For Each cn In ThisWorkbook.Connections
    cn.Delete
Next
For Each ws In ThisWorkbook.Worksheets
    For Each qt In ws.QueryTables
        qt.Delete
    Next qt
    On Error Resume Next 'Ignore error if there's no query in table.
    For Each lo In ws.ListObjects
        lo.QueryTable.Delete
    Next lo
    On Error Goto 0
Next ws

```
Clear all connection in activeworkbook without any error. you must convert the Object to as String in order delete all connections

```
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

```