|
这里随意给一段吧。你不给实例我也不方便讲:
Sub test()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ssql As String
Dim i As Long
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;" _
& "data source=" & ThisWorkbook.FullName
For i = 1 To 30
ssql = ssql & " select * from [" & Sheets(i).Name & "$A7:H37] " & "union"
Next
ssql = Left(ssql, Len(ssql) - 6)
rst.Open ssql, cnn, adOpenKeyset, adLockOptimistic
For i = 0 To rst.Fields.Count - 1
Sheets("总表").[a1].Offset(0, i) = rst.Fields(i).Name
Next
Sheets("总表").[a2].CopyFromRecordset rst
'这个是把记录集复制到Excel表里,如果需要复制到Access里,则需要建立Access的数据链接,再进行处理。
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing
End Sub
|
|