|
本帖最后由 xmyjk 于 2012-1-28 19:12 编辑
翻查了下ADOX对象的说明,发现ADOX居然支持取表内字段的名称,干脆全部用ADO做了。好处就是不用开表。坏处就是貌似汇总语句的效率不高,可能我SQL语句写复杂了吧。。。
------------------------------------------------------
多谢赵老师指正,修改了下SQL语句,整整提高了6秒,看来IIF真的很浪费效率。。。
[code=vb]
Option Explicit
Sub 汇总()
Dim Cat As New ADOX.Catalog, Myfile As String, Mypath As String
Dim i As Integer, d As New Dictionary, arr, j&, k&, m&, s As String
Dim tj(1 To 50, 1 To 3), dc(1 To 50) As New Dictionary, dd As New Dictionary
Dim Cn As New ADODB.Connection, Wb, sh As Worksheet
Dim tnm As String, cnm As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "总" Then sh.Delete
Next
Application.DisplayAlerts = True
Mypath = ThisWorkbook.Path & "\数据源\"
Myfile = Dir(Mypath & "\*.xls")
Do While Myfile <> ""
With Cat
.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=yes';Data Source=" & Mypath & Myfile
For j = 0 To .Tables.Count - 1
With .Tables(j)
If .Type = "TABLE" Then
tnm = .Name
If d.Exists(tnm) Then
tj(d(tnm), 2) = tj(d(tnm), 2) & "|" & Myfile
s = ""
For k = 0 To .Columns.Count - 1
With .Columns(k)
cnm = .Name
If cnm <> "小计" Then
If Not dc(d(tnm)).Exists(cnm) Then
dc(d(tnm))(cnm) = 0
tj(d(tnm), 3) = tj(d(tnm), 3) & " sum(" & cnm & "),"
End If
s = s & cnm & ","
End If
End With
Next
dd(Myfile & Split(tnm, "$")(0)) = s & ","
Else
m = m + 1
d(tnm) = m
tj(m, 1) = Split(tnm, "$")(0)
tj(m, 2) = Myfile
s = ""
For k = 0 To .Columns.Count - 1
With .Columns(k)
cnm = .Name
dc(m)("客户名称") = 0
If cnm <> "小计" Then
If cnm <> "客户名称" Then
dc(m)(cnm) = 0
tj(m, 3) = tj(m, 3) & " sum(" & cnm & "),"
Else
tj(m, 3) = "客户名称," & tj(m, 3)
End If
s = s & cnm & ","
End If
End With
Next
dd(Myfile & Split(tnm, "$")(0)) = s & ","
End If
End If
End With
Next
End With
Set Cat = Nothing
Myfile = Dir
Loop
Set d = Nothing
Dim brr, sql As String
For i = 1 To m
dc(i)("小计") = 0
tj(i, 3) = tj(i, 3) & " sum(小计) "
arr = dc(i).Keys
Set dc(i) = Nothing
Wb = Split(tj(i, 2), "|")
ReDim brr(0 To UBound(Wb))
Cn.Open "Provider=Microsoft.Jet.OleDb.4.0;Extended Properties='Excel 8.0';Data Source=" & Mypath & Wb(0)
For j = 0 To UBound(Wb)
For k = 0 To UBound(arr) - 1
If InStr(dd(Wb(j) & tj(i, 1)), arr(k) & ",") Then
brr(j) = brr(j) & arr(k) & ","
Else
brr(j) = brr(j) & "0 as " & arr(k) & ","
End If
Next
brr(j) = " select " & Left(brr(j), Len(brr(j)) - 1) & ",小计 " _
& " from [Excel 8.0;DATABASE=" & Mypath & Wb(j) & "].[" & tj(i, 1) & "$] "
Next
sql = Join(brr, " union all ") & " ": Erase brr
sql = "select " & tj(i, 3) & " from (" & sql & ") group by " & "客户名称 "
With ThisWorkbook.Sheets.Add(After:=Worksheets(ThisWorkbook.Sheets.Count))
.Name = tj(i, 1)
.Range("a1").Resize(1, UBound(arr) + 1) = arr
.Range("a2").CopyFromRecordset Cn.Execute(sql)
End With
sql = "": Cn.Close
Erase Wb, arr
Next
Set Cn = Nothing: Set dd = Nothing
Erase tj
Sheets("总").Select
Application.ScreenUpdating = True
End Sub
[/code]
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
|