VBA:如何从活动工作表中删除查询?

huangapple go评论69阅读模式
英文:

VBA: How to delete Queries from active worksheet?

问题

以下是翻译好的部分:

"I have a Workbook that has a "live" tab, which has around 8 Queries."
"我有一个工作簿,其中有一个名为“live”的标签,其中包含大约8个查询。"

"Everyday I duplicate this sheet, before refreshing. In the duplicated sheet, I would like to remove all queries, as I don't need them anymore."
"每天在刷新之前,我都会复制这张工作表。在复制的工作表中,我希望删除所有查询,因为我不再需要它们。"

"I'm trying to create a macro to remove all Queries."
"我正在尝试创建一个宏来删除所有查询。"

"I tried following code, but it doesn't work. error: object doesn't support this method for the line 5."
"我尝试了以下代码,但它不起作用。在第5行出现错误:对象不支持此方法。"

"I also tried the code from this question, with some modification, but gets syntax error for line 3."
"我还尝试了来自这个问题的代码,并进行了一些修改,但在第3行出现了语法错误。"

英文:

I have a Workbook that has a "live" tab, which has around 8 Queries.

Everyday I duplicate this sheet, before refreshing. In the duplicated sheet, I would like to remove all queries, as i dont need them anymore.

I'm trying to create a macro to remove all Queries.

I tried following code, but it doesn't work. error: object doesn't support this method for the line 5.

Sub DelQueries()
    Dim q As WorkbookQuery
   
    For Each q In ActiveWorkbook.Queries
        If q.Parent.Name = ActiveSheet.Name Then
            q.Delete
        End If
    Next
End Sub

I also tried the code from this question, with some modification, but gets syntax error for line 3.

Sub loop_del_query()
For Each Worksheet In ThisWorkbook.Worksheets
    If Worksheet.Name = ActiveSheet.Name
        Qcount = Worksheet.Queries.Count
        If Qcount > 0 Then
            For Each Query In Worksheet.Queries
                Query.Delete
            Next
        End If
    End If
Next Worksheet
End Sub

答案1

得分: 1

以下是代码的翻译部分:

Option Explicit

Sub DeleteQueries()

    Dim wb As Workbook, ws As Worksheet
    Dim wq As WorkbookQuery, qname As String
    Dim qt As QueryTable, tbl As ListObject
    Dim msg As String, dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
    
    ' existing queries
    For Each wq In wb.Queries
        dict.Add wq.Name, 1
    Next
    
    ' scan table object for query tables
    For Each tbl In ws.ListObjects
        Set qt = Nothing
        On Error Resume Next
        Set qt = tbl.QueryTable
        On Error GoTo 0
        If Not qt Is Nothing Then
            qname = qt.WorkbookConnection.Name
            If Left(qname, 8) = "Query - " Then
                qname = Mid(qname, 9)
                'Debug.Print tbl.Name & " Query:" & qname
                 
                'delete query if exists
                If dict.Exists(qname) Then
                    wb.Queries(qname).Delete
                    msg = msg & vbCrLf & qname
                Else
                    Debug.Print "Not found", qname
                End If
            End If
        End If
    Next
    
    If msg = "" Then
        MsgBox "No Queries deleted", vbInformation
    Else
        MsgBox "Queries deleted:" & msg, vbInformation
    End If

End Sub

希望这有所帮助。如果您有其他问题,请随时提出。

英文:

As explained on this post try ;

Option Explicit

Sub DeleteQueries()

    Dim wb As Workbook, ws As Worksheet
    Dim wq As WorkbookQuery, qname As String
    Dim qt As QueryTable, tbl As ListObject
    Dim msg As String, dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
    
    ' existing queries
    For Each wq In wb.Queries
        dict.Add wq.Name, 1
    Next
    
    ' scan table object for query tables
    For Each tbl In ws.ListObjects
        Set qt = Nothing
        On Error Resume Next
        Set qt = tbl.QueryTable
        On Error GoTo 0
        If Not qt Is Nothing Then
            qname = qt.WorkbookConnection.Name
            If Left(qname, 8) = "Query - " Then
                qname = Mid(qname, 9)
                'Debug.Print tbl.Name & " Query:" & qname
                 
                'delete query if exists
                If dict.exists(qname) Then
                    wb.Queries(qname).Delete
                    msg = msg & vbCrLf & qname
                Else
                    Debug.Print "Not found", qname
                End If
            End If
        End If
    Next
    
    If msg = "" Then
        MsgBox "No Queries deleted", vbInformation
    Else
        MsgBox "Queries deleted:" & msg, vbInformation
    End If

End Sub

</details>



huangapple
  • 本文由 发表于 2023年2月8日 20:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定