Sub ADODB_To_Table() Dim Conn As New ADODB.Connection Dim Rs As New ADODB.Recordset Dim Ws As Worksheet Dim Col As Long Dim Row As Long
Set Ws = ThisWorkbook.Worksheets("Sheet1")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
Rs.Open "SELECT * FROM [Sheet2$]", Conn
If Not Rs.EOF Then
Col = Rs.Fields.Count
Do While Not Rs.EOF
Row = Row + 1
For i = 0 To Col - 1
Ws.Cells(Row, i + 1) = Rs.Fields(i).Value
Next
Rs.MoveNext
Loop
End If
Rs.Close
Conn.Close
Set Rs = Nothing
Set Conn = Nothing
Set Ws = Nothing
End Sub
此示例从工作簿中的Sheet2表格中提取数据并将其插入当前工作簿的Sheet1表格中。请注意,仅供参考。您需要根据自己的需要进行修改和调整。
上一篇:ADODB:Sum函数中缺少值。
下一篇:ADODB多用户覆盖数据