我知道我必须设置 HDR=NO 以便将标题包含在结果中。但是当我设置它时,我不能在 SQL 语句中使用 where。任何解决方法的想法;
Sub adoExcel()
Set objConnection = CreateObject("ADODB.Connection")
Set objrecordset = CreateObject("ADODB.Recordset")
'*************************************************************************************
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Users\sp\Desktop\test ado excel\test.xls;" & _
"Extended Properties=""Excel 8.0;HDR=No;IMEX=1"";"
'*************************************************************************************
'where Number =2
objrecordset.Open "Select * FROM [Sheet1$] where Number =1", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
ActiveSheet.Cells(2, 1).CopyFromRecordset objrecordset
'*************************************************************************************
' Loop through the recordset and send data to the Immediate Window
'**************************************************************************************
'objrecordset.MoveFirst
'Do
' Debug.Print objrecordset![Name] & " " & objrecordset![Number]
' objrecordset.MoveNext
'Loop Until objrecordset.EOF
'**************************************************************************************
'ActiveSheet.Cells(1, 1).CopyFromRecordset objrecordset![Name] & " " & objrecordset![Number]
'*************************************
End Sub