|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
借用前辈的代码
Sub 合并多本数据()
Dim Conn As Object, dict As Object, target As Range
Dim strConn As String, p As String, f As String, s As String
Set target = Range("A1")
target.CurrentRegion.ClearContents
Application.ScreenUpdating = False
Set dict = CreateObject("Scripting.Dictionary")
Set Conn = CreateObject("ADODB.Connection")
s = "Excel 12.0;HDR=NO;Database="
If Application.Version < 12 Then
s = Replace(s, "12.0", "8.0")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
Else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
End If
Conn.Open strConn & ThisWorkbook.FullName
p = ThisWorkbook.Path & "\"
f = Dir(p & "*.xlsx")
While Len(f)
If p & f <> ThisWorkbook.FullName Then
dict.Add "SELECT * FROM [" & s & p & f & "].[$A1:F] WHERE LEN(F1)", ""
If dict.Count = 49 Then
target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
Set target = Cells(Rows.Count, "A").End(xlUp).Offset(1)
dict.RemoveAll
End If
End If
f = Dir
Wend
If dict.Count Then target.CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
Conn.Close
Set Conn = Nothing
Set target = Nothing
Set dict = Nothing
Application.ScreenUpdating = True
End Sub
|
|