包含";的SQL字段名"E;”用ADO查询Excel文件

包含";的SQL字段名"E;”用ADO查询Excel文件,sql,vba,ado,Sql,Vba,Ado,无法查询字段名[1-1:1.29.0 kWh]和[1-1:2.29.0 kWh],并抛出“…不包含无效…”错误。罪魁祸首是警察。我也尝试过将字段名括在后面的记号中,但不起作用。有没有不重命名列的方法 Sub smthng() Dim myConn As Object Set myConn = CreateObject("ADODB.connection") Dim myRset As Object Set myRset = CreateObject("ADODB.

无法查询字段名
[1-1:1.29.0 kWh]
[1-1:2.29.0 kWh]
,并抛出“…不包含无效…”错误。罪魁祸首是警察。我也尝试过将字段名括在后面的记号中,但不起作用。有没有不重命名列的方法

Sub smthng()

Dim myConn As Object
Set myConn = CreateObject("ADODB.connection")
Dim myRset As Object
Set myRset = CreateObject("ADODB.recordset")
Dim connstr As String
Dim fileloc As String
Dim qrystr As String
Dim arr As Variant

Debug.Print ThisWorkbook.FullName
fileloc = xxxx

connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fileloc 
& ";Extended Properties=""Excel 12.0 Xml;HDR=YES;"""  

qrystr = "SELECT * FROM [Sheet2$L4:P24] WHERE [1-1:1.29.0 kWh] > 0"

Debug.Print connstr, qrystr
myConn.Open connstr '<-- active connection
With myRset
    .CursorType = 2 ' adOpenDynamic
    .CursorLocation = 3 ' adUseClient
    .Open qrystr, myConn
End With

Debug.Print myRset.Fields.Count, myRset.RecordCount

arr = myRset.getrows

'If Not myRset.BOF And Not myRset.EOF Then
'        myRset.MoveLast 
'        myRset.MoveFirst

'    Do While Not (myRset.EOF)
'      'do stuff
'
'    Loop
'End If

End Sub
Sub-smthng()
作为对象的Dim myConn
设置myConn=CreateObject(“ADODB.connection”)
作为对象的暗淡myRset
Set myRset=CreateObject(“ADODB.recordset”)
Dim connstr As字符串
Dim fileloc作为字符串
作为字符串的Dim qrystr
作为变体的Dim-arr
调试。打印此工作簿。全名
fileloc=xxxx
connstr=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=”&fileloc
&“扩展属性=“Excel 12.0 Xml;HDR=YES;””
qrystr=“从[Sheet2$L4:P24]中选择*,其中[1-1:1.29.0 kWh]>0”
调试。打印connstr,qrystr

myConn.Open connstr'为你做这件事:?@AlexK。是的。谢谢