|
本帖最后由 wpxxsyzx 于 2012-2-4 13:49 编辑
ADO方法做的
Sub 汇总1()
Dim d As New Dictionary, dzd As New Dictionary, dwj As New Dictionary, d1 As New Dictionary
Dim MyFile As String, MyPath As String, sql As String, bm As String, LJ As String
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset, rst1 As ADODB.Recordset
Dim sht As Worksheet
MyPath = ThisWorkbook.Path & "\数据源\"
LJ = " from [Excel 8.0;Database=" & MyPath
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sht In Worksheets
If sht.Name <> "总" Then sht.Delete
Next
MyFile = Dir(ThisWorkbook.Path & "\数据源\*.xls")
Do While MyFile <> ""
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
Set rst1 = cnn.OpenSchema(adSchemaTables)
Do While Not rst1.EOF
bm = CStr(rst1!table_name)
Set rst = cnn.Execute("select * " & LJ & MyFile & "].[" & bm & "]")
If d.Exists(bm) = False Then
Set dzd(bm) = New Dictionary
Set dwj(bm) = New Dictionary
Set d1(bm) = New Dictionary
Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = Replace(bm, "$", "")
End If
For i = 1 To rst.Fields.Count - 2
dwj(bm)(MyFile) = dwj(bm)(MyFile) & rst.Fields(i).Name & ","
If dzd(bm).Exists(rst.Fields(i).Name) = False Then
d(bm) = d(bm) & "Sum(" & rst.Fields(i).Name & "),"
dzd(bm)(rst.Fields(i).Name) = ""
End If
Next
rst1.MoveNext
Loop
Set cnn = Nothing
MyFile = Dir
Loop
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;imex=2';Data Source=" & MyPath & "01月汇总.xls"
For Each kb In d.Keys
sql = "select 客户名称," & d(kb) & "Sum(小计) from "
For Each k In dwj(kb).Keys
For Each Z In dzd(kb).Keys
If InStr(dwj(kb)(k), Z & ",") = 0 Then
d1(kb)(k) = d1(kb)(k) & "0 as " & Z & ","
Else
d1(kb)(k) = d1(kb)(k) & Z & ","
End If
Next
d1(kb)(k) = "select 客户名称," & d1(kb)(k) & "小计" & LJ & k & "].[" & kb & "]"
Next
Range("a1") = sql & "(" & Join(d1(kb).Items, " union all ") & " order by 客户名称" & ") group by 客户名称"
dzd(kb)("小计") = ""
With Worksheets(Replace(kb, "$", ""))
.Range("a1") = "客户名称"
.Range("b1").Resize(1, dzd(kb).Count) = dzd(kb).Keys
.Range("a2").CopyFromRecordset cnn.Execute(sql & "(" & Join(d1(kb).Items, " union all ") & " order by 客户名称" & ") group by 客户名称")
End With
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set cnn = Nothing
End Sub
Sub 汇总2()
Dim cat As New ADOX.Catalog
Dim d As New Dictionary, dzd As New Dictionary, dwj As New Dictionary, d1 As New Dictionary
Dim MyFile As String, MyPath As String, sql As String, bm As String, LJ As String
Dim cnn As New ADODB.Connection
Dim rst As ADODB.Recordset
Dim sht As Worksheet
MyPath = ThisWorkbook.Path & "\数据源\"
LJ = " from [Excel 8.0;Database=" & MyPath
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sht In Worksheets
If sht.Name <> "总" Then sht.Delete
Next
MyFile = Dir(ThisWorkbook.Path & "\数据源\*.xls")
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;imex=2';Data Source=" & MyPath & "01月汇总.xls"
Do While MyFile <> ""
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
For Each tb In cat.Tables
bm = CStr(tb.Name)
Set rst = cnn.Execute("select * " & LJ & MyFile & "].[" & bm & "]")
If d.Exists(bm) = False Then
Set dzd(bm) = New Dictionary
Set dwj(bm) = New Dictionary
Set d1(bm) = New Dictionary
Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = Replace(bm, "$", "")
End If
For i = 1 To rst.Fields.Count - 2
dwj(bm)(MyFile) = dwj(bm)(MyFile) & rst.Fields(i).Name & ","
If dzd(bm).Exists(rst.Fields(i).Name) = False Then
d(bm) = d(bm) & "Sum(" & rst.Fields(i).Name & "),"
dzd(bm)(rst.Fields(i).Name) = ""
End If
Next
Next
MyFile = Dir
Loop
For Each kb In d.Keys
sql = "select 客户名称," & d(kb) & "Sum(小计) from "
For Each k In dwj(kb).Keys
For Each Z In dzd(kb).Keys
If InStr(dwj(kb)(k), Z & ",") = 0 Then
d1(kb)(k) = d1(kb)(k) & "0 as " & Z & ","
Else
d1(kb)(k) = d1(kb)(k) & Z & ","
End If
Next
d1(kb)(k) = "select 客户名称," & d1(kb)(k) & "小计" & LJ & k & "].[" & kb & "]"
Next
Range("a1") = sql & "(" & Join(d1(kb).Items, " union all ") & " order by 客户名称" & ") group by 客户名称"
dzd(kb)("小计") = ""
With Worksheets(Replace(kb, "$", ""))
.Range("a1") = "客户名称"
.Range("b1").Resize(1, dzd(kb).Count) = dzd(kb).Keys
.Range("a2").CopyFromRecordset cnn.Execute(sql & "(" & Join(d1(kb).Items, " union all ") & " order by 客户名称" & ") group by 客户名称")
End With
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Set cnn = Nothing
End Sub
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
1
查看全部评分
-
|