|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
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
这都什么意思呀?版主能否给注释一下?让菜鸟也学习一下 |
|