Unable to query named range on sheet with spaces in name in Excel

前端 未结 5 1496
我寻月下人不归
我寻月下人不归 2021-01-18 14:21

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

5条回答
  •  走了就别回头了
    2021-01-18 14:55

    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.

提交回复
热议问题