|
XUYUJING2007 发表于 2012-6-25 11:14
就是对文件夹里的表格中的数据按日期进行分类汇总 - Sub Macro1()
- '引用Microsoft AD0 Ext 2.8 for DDL and Security
- '引用Microsoft ActiveX Data Objects 2.x Library
- Dim cnn As New ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim cat As New ADOX.Catalog, tb1 As Table
- Dim d As Object
- Dim SQL$, MyFile$, m%, i%, temp$, strField$, s$, t$, t2$, n%
- Application.ScreenUpdating = False
- Set d = CreateObject("scripting.dictionary")
- Sheets("项目取数").[a1].CurrentRegion.Offset(3).ClearContents
- Sheets("基础信息").[a1].CurrentRegion.Offset(3).ClearContents
- Mypath = ThisWorkbook.Path & ""
- MyFile = Dir(Mypath & "*.xlsx")
- Do While MyFile <> ""
- If MyFile <> ThisWorkbook.Name Then
- n = n + 1
- If n > 1 Then
- t = "[Excel 12.0;Database=" & Mypath & MyFile & "]."
- Else
- cnn.Open "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & Mypath & MyFile
- End If
- t2 = "[Excel 12.0;HDR=No;Database=" & Mypath & MyFile & "]."
- cat.ActiveConnection = "Provider=Microsoft.Ace.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No';Data Source=" & Mypath & MyFile
- For Each tb1 In cat.Tables
- If tb1.Type = "TABLE" Then
- s = Replace(tb1.Name, "'", "")
- If Right(s, 1) = "$" Then
- m = m + 1
- If m = 1 Then
- Set rs = cnn.Execute("[" & s & "a5:d]")
- For i = 0 To rs.Fields.Count - 1
- temp = temp & rs.Fields(i).Name & ","
- Next
- End If
- SQL = "select * from " & t & "[" & s & "a5:d]"
- d(SQL) = ""
- If m Mod 49 = 0 Then
- SQL = Join(d.Keys, " UNION ALL ")
- Sheets("项目取数").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- d.RemoveAll
- End If
- End If
- End If
- Next
- End If
- MyFile = Dir()
- Loop
- If d.Count > 0 Then
- SQL = Join(d.Keys, " UNION ALL ")
- Sheets("项目取数").[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
- End If
- With Sheets("项目取数")
- SQL = "select 日期,sum(项目1),sum(项目2),sum(项目3) from [Excel 12.0;Database=" & ThisWorkbook.FullName & "].[项目取数$a3:d] group by 日期"
- Set rs = New ADODB.Recordset
- rs.Open SQL, cnn, 1, 3
- .Range("a4").CopyFromRecordset rs
- .Range("a1").CurrentRegion.Offset(rs.RecordCount + 3).ClearContents
- End With
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set cat = Nothing
- Set tb1 = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|