I have a workbook with multiple sheets, and each sheet has the same set of named ranges (IE they are scoped to the sheet, not workbook).
I want to query based on a n
The name of the sheet with spaces followed by named range can be written as ['My Sheet$'MyData]
Here is how you get to list the tables contained in the workbook
1) Code to get the list of tables in the workbook
dim i as Integer
Set objRecordSet = objConn.OpenSchema(adSchemaTables)
Do While Not objRecordSet.EOF
i = 1
For i = 0 To objRecordSet.Fields.Count - 1
Debug.Print objRecordSet.Fields(i).Name, objRecordSet.Fields(i).Value
Next
objRecordSet.MoveNext
Loop
EDIT: For your scenario, it will be
strQuery = "Select * from ['With Spaces$'Ingredients]"
EDIT2: I am sorry, I pasted the wrong code the first time. Please use the above code in the listing 1 and look for TABLE_NAME
in the immediate window. The listing of named ranges prefixed with sheet name will be shown against TABLE_NAME
(on which you can query).
Also, make sure that the named range is scoped to the worksheet. Make sure that the casing of the sheet name and range name matches with query.