|
- Sub test1()
- Dim Cnn As Object, Cel As Range, Dict As Object
- Dim SQL As String, p As String, f As String, s As String
-
- Range("A1").CurrentRegion.Offset(2).ClearContents
- Application.ScreenUpdating = False
-
- Set Cel = Range("A3")
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Cnn = CreateObject("ADODB.Connection")
-
- s = "Excel 12.0;HDR=no;Database="
- If Application.Version < 12 Then
- Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
- s = Replace(s, "12.0", "8.0")
- Else
- Cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName
- End If
-
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls?")
- SQL = "SELECT f1,f2,f5,'[s]' FROM [" & s & p & "[f]].[$C4:G] WHERE f1 LIKE '%" & Range("b1").Value & "%'"
-
- While Len(f)
- If f <> ThisWorkbook.Name Then
- s = Split(f, ".")(0)
- Dict.Add Replace(Replace(SQL, "[s]", s), "[f]", f), vbNullString
- If Dict.Count Mod 49 = 0 Then
- Cel.CopyFromRecordset Cnn.Execute(Join(Dict.Keys, " UNION ALL "))
- Set Cel = Cells(Rows.Count, 1).End(xlUp).Offset(1)
- Dict.RemoveAll
- End If
- End If
- f = Dir
- Wend
- If Dict.Count Then Cel.CopyFromRecordset Cnn.Execute(Join(Dict.Keys, " UNION ALL "))
-
- Cnn.Close
- Set Cnn = Nothing
- Set Cel = Nothing
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|