如何从工作簿中删除所有不需要的查询?
Sub DeleteQuery()
Dim queries As Variant
queries = Array("q1", "q2", "q3")
For Each qr In ThisWorkbook.queries
'Not sure about the syntax of the following line
If qr not in queries Then
qr.Delete
Next qr
End Sub
如果查询不在列表中,则应将其删除
ActiveWorkbook.Queries("Query1").Delete
将不起作用,因为不需要的查询的名称不明确
发布于 2019-10-31 13:11:33
您可以将Application.Match
与查询名称一起使用。
If IsError(Application.Match(qr.Name, queries, 0)) Then ' query name is not in list
发布于 2019-10-31 13:18:51
您需要遍历查询,然后遍历整个数组以查找匹配项,如果不存在匹配项,则删除。
Sub testingPQ()
Dim vQuery As Variant
Dim arrQueries() As Variant
Dim i As Long
arrQueries = Array("q1", "q2", "q3")
For Each vQuery In ThisWorkbook.Queries
'loop through array to check for each query
For i = LBound(arrQueries) To UBound(arrQueries)
If vQuery.Name = arrQueries(i) Then
'do not delete
Exit For
End If
If i = UBound(arrQueries) Then
'delete - no match
vQuery.Delete
End If
Next i
Next vQuery
End Sub
https://stackoverflow.com/questions/58643829
复制相似问题