How to append All Query in Excel to Only One (1 Viewer)

zezo2021

Member
Local time
Today, 15:42
Joined
Mar 25, 2021
Messages
381
friends

Is there any VBA code to append all existing queries

Bard Code Is

Code:
Sub CombineAllQueries()
    Dim query As WorkbookQuery
    Dim combinedQuery As String
    For Each query In ActiveWorkbook.Queries
        combinedQuery = combinedQuery & " " & query.Formula & " " & Chr(13) & " " & Chr(10)
    Next query
    ThisWorkbook.Queries.Add Name:="CombinedQuery", Formula:=combinedQuery
End Sub

but not work
 

zezo2021

Member
Local time
Today, 15:42
Joined
Mar 25, 2021
Messages
381
I need to load all query to one and the load them to one table

this code worked but I can't convert it to table load to table
because list query only as a name in a new combined query

Code:
Sub CombinePowerQueries()
    Dim conn As WorkbookConnection
    Dim qry As WorkbookQuery
    Dim i As Integer
    Dim str As String
    Dim wb As Workbook

    Set wb = ActiveWorkbook

    'Create a new query table
    Set qry = wb.Queries.Add("CombinedQuery", "let" & vbCrLf & "    Source = Excel.CurrentWorkbook()" & vbCrLf & "in" & vbCrLf & "    Source")

    'Loop through all connections
    For i = 1 To wb.Connections.Count
        Set conn = wb.Connections.Item(i)

        'Check if the connection is a Power Query connection
        If conn.Type = xlConnectionTypeOLEDB Then
            str = conn.OLEDBConnection.CommandText

            'Check if the connection is a Power Query command
            If InStr(str, "let") > 0 Then
                'Add the query to the combined query
                qry.Formula = qry.Formula & vbCrLf & "    " & str
            End If
        End If
    Next i

    'Refresh the combined query
    qry.Refresh

    'Delete all other queries
    For i = wb.Queries.Count To 1 Step -1
        If wb.Queries.Item(i).Name <> "CombinedQuery" Then
            wb.Queries.Item(i).Delete
        End If
    Next i
End Sub
 

zezo2021

Member
Local time
Today, 15:42
Joined
Mar 25, 2021
Messages
381
Can you check this question
@arnelgp

thanks in advance
 

Users who are viewing this thread

Top Bottom