|
- Sub 分文件汇总()
- On Error Resume Next
- Dim wb As Workbook
- Dim Filename$, fn$, sql$
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties='excel 12.0';data source=" & ThisWorkbook.FullName
- Filename = Dir(ThisWorkbook.Path & "\*.xlsx")
- Application.DisplayAlerts = False
- Do While Filename <> ""
- If Filename <> ThisWorkbook.Name Then
- fn = ThisWorkbook.Path & "" & Filename
- Set wb = Workbooks.Open(fn)
- sql = "select TIME,avg(K1),avg(K2),avg(K3),avg(K4),avg(K5),avg(K6),avg(K7) from [" & fn & "].[汇总$] group by TIME"
- wb.Sheets(2).Delete
- wb.Sheets.Add after:=wb.Sheets(wb.Sheets.Count)
- With wb.Sheets(2)
- .Name = "结果"
- .[a1].Resize(1, 8) = Array("TIME", "K1", "K2", "K3", "K4", "K5", "K6", "K7")
- .[a2].CopyFromRecordset cnn.Execute(sql)
- End With
- End If
- wb.Close True
- Filename = Dir
- Loop
- cnn.Close: Set cnn = Nothing
- Application.DisplayAlerts = True
- End Sub
复制代码 |
|