0

我使用以下宏从关闭的工作簿中检索数据。为什么 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]
'**************************************************************************************
4

1 回答 1

1

当您尝试时,CopyFromRecordset您已经在文件末尾 (EOF),因此没有要复制的数据。CopyFromRecordset在循环之前将语句移到代码中的前面。

这个方法的参数是一个 RecordSet 对象:

.CopyFromRecordset objrecordset
于 2013-09-14T08:30:28.850 回答