Queries not Detected in WorkbookQueries Collection

Hashiru

Active Member
Joined
May 29, 2011
Messages
286
Hi all the below code could not detect queries on my workbook

Code from https://stackoverflow.com/questions...h-vba?newreg=fd32a0270b5149668f4768387e94fc23

Need this code to be able to copy queries and data from the master template to a new workbook

Rich (BB code):
Public Sub FunctionToTest_ForStackOverflow()
                ' Doug.Long
                Dim wb As Workbook
                ' create empty workbook
                Set NewBook = Workbooks.Add
                Set wb = NewBook
                ' copy queries
                CopyPowerQueries ThisWorkbook, wb, True
            End Sub
            Public Sub CopyPowerQueries(wb1 As Workbook, wb2 As Workbook, Optional ByVal copySourceData As Boolean)
                ' Doug.Long
                ' copy power queries into new workbook
                Dim qry As WorkbookQuery
                For Each qry In wb1.Queries
                    ' copy source data
                    If copySourceData Then
                        CopySourceDataFromPowerQuery wb1, wb2, qry
                    End If
                    ' add query to workbook
                    wb2.Queries.Add qry.Name, qry.Formula, qry.Description
                Next
           End Sub
            Public Sub CopySourceDataFromPowerQuery(wb1 As Workbook, wb2 As Workbook, qry As WorkbookQuery)
                ' Doug.Long
                ' copy source data by pulling data out from workbook into other
                Dim qryStr As String
                Dim sourceStrCount As Integer
                Dim i As Integer
                Dim tbl As ListObject
                Dim sht As Worksheet
                sourceStrCount = (Len(qry.Formula) - Len(Replace$(qry.Formula, "Source = Excel.CurrentWorkbook()", ""))) / Len("Source = Excel.CurrentWorkbook()")
                For i = 1 To sourceStrCount
                    qryStr = Split(Split(qry.Formula, "Source = Excel.CurrentWorkbook(){[Name=""")(1), """]}")(0)
                    For Each sht In wb1.Worksheets
                        For Each tbl In sht.ListObjects
                            If tbl.Name = qryStr Then
                                If Not sheetExists(sht.Name) Then
                                    sht.Copy After:=wb2.Sheets(wb2.Sheets.Count)
                                End If
                            End If
                        Next tbl
                    Next sht
                Next i
                qryStr = qry.Formula
            End Sub
            Function sheetExists(sheetToFind As String) As Boolean
                'http://stackoverflow.com/questions/6040164/excel-vba-if-worksheetwsname-exists
                sheetExists = False
                For Each Sheet In Worksheets
                    If sheetToFind = Sheet.Name Then
                        sheetExists = True
                        Exit Function
                    End If
                Next Sheet
            End Function
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
What kind of queries are they?
 
Upvote 0
Which version of Excel are you using?
 
Upvote 0
Hi RoryA,

Home Excel 2016 64 bit on a 64 bit OS
Work Excel 2016 32 bit on a 64 bit OS

Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,869
Members
449,054
Latest member
juliecooper255

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top