|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 宏1()
Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, p$, a, b, arr(), brr(1 To 100000, 4), i&, j&, n&, m&, c&, d As Object
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set d = CreateObject("scripting.dictionary")
p = ThisWorkbook.Path & "\结果\"
If Dir(p, vbDirectory) = "" Then MkDir p
Set Fso = CreateObject("Scripting.FileSystemObject")
Set cnn = CreateObject("adodb.connection")
ReDim arr(1 To Fso.GetFolder(ThisWorkbook.Path).Files.Count)
For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
If File.Name Like "*.xlsx" Then
n = n + 1
If n = 1 Then cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties='Excel 12.0;hdr=no';data source=" & File
SQL = "select * from [Excel 12.0;hdr=no;Database=" & File & ";].[" & Replace(File.Name, ".xlsx", "") & "$] where f1 is not null"
Set rs = cnn.Execute(SQL)
arr(n) = rs.GetRows
For i = 0 To UBound(arr(n), 2)
d(arr(n)(0, i)) = d(arr(n)(0, i)) & "|" & n & "," & i
Next
End If
Next
k = d.keys
t = d.items
For l = 0 To d.Count - 1
m = 0
a = Split(t(l), "|")
For i = 1 To UBound(a)
m = m + 1
b = Split(a(i), ",")
n = b(0)
c = b(1)
For j = 0 To 4
brr(m, j) = arr(n)(j, c)
Next
Next
With Workbooks.Add(xlWBATWorksheet)
.ActiveSheet.Range("a1").Resize(m, 5) = brr
.SaveAs p & k(l) & ".xlsx"
.Close
End With
Erase brr
Next
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
Set Fso = Nothing
Application.ScreenUpdating = True
MsgBox "OK"
End Sub
|
评分
-
1
查看全部评分
-
|