|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 LMY123 于 2017-4-21 15:34 编辑
ADO多文件、多表 合并、汇总-无标题行.rar
(20.96 KB, 下载次数: 47)
请问怎么修改代码:
- Sub addExcel()
- Dim d As New Dictionary, arr(), i As Long
- Dim cnn As New ADODB.Connection
- Dim rst As New ADODB.Recordset
- Dim cog As New Catalog
- Dim Sql As String
- Dim myDatePath As String
- Dim ThisExcelName As String
- Dim Dirs As String
- On Error GoTo ErrMsg
- Cells = Empty
- ThisExcelName = ThisWorkbook.Name
- Dirs = Dir(ThisWorkbook.Path & "\*.xls")
- If ThisExcelName <> Dirs Then
- myDatePath = ThisWorkbook.Path & "" & Dirs
- d(myDatePath) = 0
- End If
- Do While Dirs <> ""
- Dirs = Dir
- If ThisExcelName <> Dirs And Dirs <> "" Then
- myDatePath = ThisWorkbook.Path & "" & Dirs
- d(myDatePath) = 0
- End If
- Loop
- If d.Count = 0 Then MsgBox "没找到数据文件": Exit Sub
-
- arr = d.Keys
- d.RemoveAll
- For i = 0 To UBound(arr)
- Sql = "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties='Excel 8.0;hdr=no';Data Source=" & arr(i)
- cnn.Open Sql
- Set cog.ActiveConnection = cnn
-
- For Each Tabs In cog.Tables
- If Tabs.Name = "王[ DISCUZ_CODE_0 ]quot; Then
- Sql = "Select * From [Excel 8.0;DATABASE=" & arr(i) & "].[" & Tabs.Name & "A3:C5]"
- d(Sql) = 0
- End If
- Next
- cnn.Close
- Next
- Sql = Join(d.Keys, " UNION ALL ") & " order by F1"
- If MsgBox("汇总请选“是”,合并请选“否”", 4) = vbYes Then
- Sql = "SELECT F1, Sum(F3) AS 产量, Count(F1) AS 工作次数 from (" & Sql & ") GROUP BY F1"
- End If
- cnn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties='Excel 8.0;hdr=no';Data Source" & myDatePath
- Set rst = cnn.Execute(Sql)
- Range("e1") = "总表数 : " & d.Count
- Range("a2").CopyFromRecordset rst
- MsgBox "Excel表格汇总成功!", , "表格汇总"
- Exit Sub
- ErrMsg:
- MsgBox Err.Description, , "错误报告"
- End Sub
复制代码
|
|