|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub ADO法_多夹_多薄_首表_多条件查询_无标题行_参考()
Dim arr$(), m&
Set Fso = CreateObject("Scripting.FileSystemObject"): Set Folder = Fso.GetFolder(ThisWorkbook.Path)
Application.ScreenUpdating = False
Call GetFiles(Folder, arr, m)
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.ace.oledb.12.0;extended properties='excel 12.0;HDR=NO';data source=" & arr(1)
ActiveSheet.UsedRange.Offset(2).ClearContents
For i = 1 To m
Set rs = cnn.OpenSchema(20)
Do Until rs.EOF
If rs.Fields("TABLE_TYPE") = "TABLE" Then
s = Replace(rs("TABLE_NAME").Value, "'", "")
If Right(s, 1) = "$" Then
SQL = "select F7,F23,F18,F12 from [A5:AD] WHERE F2*1= " & Split(Range("a1"), "(")(0) & " "
Range("a65536").End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
SQL = "select F7,F23,F18,F12 from [A5:AD] WHERE F2*1= " & Split(Range("E1"), "(")(0) & " "
Range("E65536").End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
End If
End If
rs.MoveNext
Loop
Next
Set Folder = Nothing: Set Fso = Nothing: cnn.Close:
Set Fso = Nothing: Set File = Nothing: Set cnn = Nothing
Application.ScreenUpdating = True
End Sub
Sub GetFiles(ByVal Folder As Object, arr$(), m&)
Dim SubFolder As Object
Dim File As Object
If Folder.Path <> ThisWorkbook.Path Then
For Each File In Folder.Files
If File.Name Like "*.xls*" Then
m = m + 1
ReDim Preserve arr(1 To m)
arr(m) = File
End If
Next
End If
For Each SubFolder In Folder.SubFolders
N = UBound(Split(SubFolder, "\"))
If (Split(Split(SubFolder, "\")(N), "年")(0) = 2017 _
And Val(Split(Split(Split(SubFolder, "\")(N), "月")(0), "年")(1)) > 8) _
Or (Split(Split(SubFolder, "\")(N), "年")(0) = 2018 _
And Val(Split(Split(Split(SubFolder, "\")(N), "月")(0), "年")(1)) < 2) Then
Call GetFiles(SubFolder, arr, m)
End If
Next
End Sub |
|