|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub test()
Dim Cn As Object, Rs As Object, p$, f$, Sq$, s$
Dim ar$(1 To 2345, 1 To 13), br, i&, j&, k&
Application.ScreenUpdating = False
Set Cn = CreateObject("ADODB.Connection")
If Application.Version < 12 Then
Cn.Open "Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
s = "Excel 8.0;HDR=no;Database="
Else
Cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
s = "Excel 12.0;HDR=no;Database="
End If
p = ThisWorkbook.Path & "\"
f = Dir(p & "*.xls?")
While Len(f)
If f <> ThisWorkbook.Name Then
i = i + 1: k = 1
ar(i, k) = Split(f, ".xls")(0)
Sq = "SELECT f1 FROM [" & s & p & f & "].[$N12:n24]"
br = Cn.Execute(Sq).GetRows()
For j = 0 To UBound(br, 2)
If Not IsNull(br(0, j)) Then
k = k + 1
ar(i, k) = br(0, j)
End If
Next
End If
f = Dir
Wend
Range("A2").Resize(UBound(ar), UBound(ar, 2)) = ar
Cn.Close: Set Cn = Nothing
Application.ScreenUpdating = True
Beep
End Sub |
|