我使用以下宏从关闭的工作簿中检索数据。为什么 debug.print 将数据返回到即时窗口,但数据既不使用语句复制到工作表ActiveSheet.Cells(1, 1).CopyFromRecordset objrecordset![Name] & " " & objrecordset![Number]
,也不使用语句,ActiveSheet.Cells(1,1).CopyFromRecordset objrecordset
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=Yes;"";"
'*************************************************************************************
objrecordset.Open "Select * FROM [Sheet1$]", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
'*************************************************************************************
' 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]
'**************************************************************************************