|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test1()
- Dim Conn As Object, Dict As Object, Cel As Range
- Dim strConn As String, p As String, f As String, s As String
- ActiveSheet.UsedRange.ClearContents
- Application.ScreenUpdating = False
- Range("A1").Resize(, 4) = Split("簿名,语文,数学,英语", ",")
- s = "Excel 12.0;HDR=yes;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
- Set Conn = CreateObject("ADODB.Connection")
- Conn.Open strConn & ThisWorkbook.FullName
- Set Cel = Range("A2")
- Set Dict = CreateObject("Scripting.Dictionary")
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls*")
- While Len(f)
- If p & f <> ThisWorkbook.FullName Then
- Dict.Add "SELECT '" & Split(f, ".xls")(0) & "' AS 簿名,语文,数学,英语 FROM [" & s & p & f & "].[$A2:I]", vbNullString
- If Dict.Count = 49 Then
- Cel.CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- Set Cel = Cells(Rows.Count, "A").End(xlUp).Offset(1)
- Dict.RemoveAll
- End If
- End If
- f = Dir
- Wend
- If Dict.Count Then Cel.CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- Conn.Close
- Set Conn = Nothing
- Set Cel = Nothing
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|