|
楼主 |
发表于 2011-11-22 11:27
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 冻豆腐 于 2011-11-22 13:23 编辑
小结:
本题主要测试ADO实现多工作簿多表(超过49个)联合查询,本来有速度要求,在冻版主建议下取消了要求,只要结果正确就可以得分
为了减小难度,加了80个工作簿和“每个工作簿都有3个格式相同的工作表,名称分别是1部门、2部门和3部门”的条件
下面给出3个参考da'an(系统不准出现这两个字,歉),其中第一个不使用上面给出的条件,请广大会员斧正:
[code=vb]Sub Macro1() '通用,不使用条件:已知“每个工作簿都有3个格式相同的工作表,名称分别是1部门、2部门和3部门”
tt = Timer
Dim cnn As Object, cat As Object, d As Object
Dim SQL$, MyFile$, arr(), i&, j&, t$, s$, wk$, m&, n&
temp = [c2]
Application.ScreenUpdating = False
ActiveSheet.UsedRange.Offset(3).ClearContents
MyPath = ThisWorkbook.Path & "\"
MyFile = Dir(MyPath & "*.xls")
Set cnn = CreateObject("adodb.connection")
Set cat = CreateObject("ADOX.Catalog")
Set d = CreateObject("scripting.dictionary")
Do While MyFile <> ""
If MyFile <> ThisWorkbook.Name Then
wk = Replace(MyFile, ".xls", "")
n = n + 1
If n = 1 Then cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.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
SQL = "select 序号,姓名,客户号,合同号,档案编号,期限,'" & wk & "','" & Replace(s, "$", "") & "' from [Excel 8.0;Database=" & MyPath & MyFile & "].[" & s & "a3:f65536] where 姓名='" & temp & "'"
d(SQL) = ""
If m Mod 49 = 0 Then
SQL = Join(d.Keys, " UNION ALL ")
[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 ")
[a65536].End(3).Offset(1).CopyFromRecordset cnn.Execute(SQL)
End If
Set cat = Nothing
Set tb1 = Nothing
cnn.Close
Set cnn = Nothing
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub
[/code]
[code=vb]Sub Macro2() '简易,速度较快,只适用已知“每个工作簿都有3个格式相同的工作表,名称分别是1部门、2部门和3部门”
tt = Timer
Dim cnn As Object
Dim SQL$, MyPath$, MyFile$, a, arr(), i%, ii%, j%, t$, m%
t = [c2]
a = Array("1部门$a3:f65536", "2部门$a3:f65536", "3部门$a3:f65536")
Application.ScreenUpdating = False
ActiveSheet.UsedRange.Offset(3).ClearContents
MyPath = ThisWorkbook.Path & "\"
MyFile = Dir(MyPath & "*.xls")
Do While MyFile <> ""
If MyFile <> ThisWorkbook.Name Then
m = m + 1
ReDim Preserve arr(1 To m)
arr(m) = MyFile
End If
MyFile = Dir()
Loop
Set cnn = CreateObject("adodb.connection")
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & arr(1)
For i = 1 To m Step 16
SQL = ""
For ii = i To i + 15
If ii > m Then Exit For
For j = 0 To 2
If Len(SQL) Then SQL = SQL & " union all "
SQL = SQL & "select 序号,姓名,客户号,合同号,档案编号,期限,'" & Replace(arr(ii), ".xls", "") & "','" & Left(a(j), 3) & "' from [Excel 8.0;Database=" & MyPath & arr(ii) & "].[" & a(j) & "] where 姓名='" & t & "'"
Next
Next
[a65536].End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
Next
cnn.Close
Set cnn = Nothing
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub
[/code]
[code=vb]Sub Macro3() '简易,速度较快,只适用已知“每个工作簿都有3个格式相同的工作表,名称分别是1部门、2部门和3部门”
tt = Timer
Dim cnn As Object
Dim SQL$, MyPath$, MyFile$, wk$, a, i%, t$, m%, n%
t = [c2]
a = Array("1部门", "2部门", "3部门")
Application.ScreenUpdating = False
ActiveSheet.UsedRange.Offset(3).ClearContents
MyPath = ThisWorkbook.Path & "\"
MyFile = Dir(MyPath & "*.xls")
Set cnn = CreateObject("adodb.connection")
Do While MyFile <> ""
If MyFile <> ThisWorkbook.Name Then
n = n + 1
If n = 1 Then cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
wk = Replace(MyFile, ".xls", "")
For i = 0 To 2
m = m + 1
If m > 49 Then
[a65536].End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
m = 1
SQL = ""
End If
If Len(SQL) Then SQL = SQL & " union all "
SQL = SQL & "select 序号,姓名,客户号,合同号,档案编号,期限,'" & wk & "','" & a(i) & "' from [Excel 8.0;Database=" & MyPath & MyFile & "].[" & a(i) & "$a3:f65536] where 姓名='" & t & "'"
Next
End If
MyFile = Dir()
Loop
If Len(SQL) Then [a65536].End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
Application.ScreenUpdating = True
MsgBox Timer - tt
End Sub
[/code]
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?免费注册
x
评分
-
6
查看全部评分
-
|