Forum Discussion
JerryDNYC
Oct 16, 2022Copper Contributor
VBA to delete Power Query Connections or Folder
I have a master workbook with 10 Power Query connections. That workbook is a Read-Only file, and has a macro that saves the workbook with a different file name. Once the file has been saved I wan...
- Oct 17, 2022
JerryDNYC You need code like this to delete the queries:
Sub DelQueries() Dim q As WorkbookQuery For Each q In ActiveWorkbook.Queries If MsgBox("Delete query '" & q.Name & "'?", vbYesNo) = vbYes Then q.Delete End If Next End Sub
sal_iv
Feb 08, 2023Copper Contributor
Thanks for the Quick response!!
For me the above quries doesn't remove the Queries in the worksheet.
In the Data & Connections pane, I can see I only have Queries. (Sorry I'm new to these.)
So I edited your code to the following.
```
Sub DelQueries()
Dim q As WorkbookQuery
Dim c As WorkbookConnection
For Each q In ActiveWorkbook.Queries
If q.Ranges.Count > 0 Then
If q.Ranges(1).Parent.Name = ActiveSheet.Name Then
q.Delete
End If
Next
End Sub
```
However, it errors out “Object doesn't support this property” for the line "If q.Ranges.Count > 0 Then"
JerryDNYC
Mar 07, 2023Copper Contributor
That is a VBA script, not a Query.