Excel SQL 中的 ListObject(格式为 Table)

Excel ListObject (Format as Table) in SQL

您可以在 Excel VBA 中使用 SQL 来查询这样的工作表:

Dim relConn As ADODB.Connection
Dim rs As ADODB.Recordset
Set relConn = CreateObject("ADODB.Connection")
relConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
    & ThisWorkbook.FullName _
    & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT [MyWorksheet$].MyFirstLineHeader FROM [MyWorksheet$]", relConn
While (Not rs.EOF)
  ...
Wend

→ 工作表在 SQL 中被称为 [WorksheetName$]

在 SQL 中,如何从主页功能区引用 ListObject which is created by selecting a range and choosing Format as Table?在 VBA 中,它们的访问方式如下:ThisWorkbook.Sheets("MyWorksheet").ListObjects("MyTableName").

尝试

Dim s as string
s = ThisWorkbook.Sheets("MyWorksheet").ListObjects("MyTableName").Range.Address(0, 0)
rs.Open "SELECT [MyFirstLineHeader] FROM [MyWorkSheet$" & s & "]", relConn