|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test1()
- Dim p As String
- ' With Application.FileDialog(msoFileDialogFolderPicker)
- ' .InitialFileName = ThisWorkbook.Path
- ' If .Show Then p = .SelectedItems(1) Else Exit Sub
- ' End With
- ' If Right(p, 1) <> "\" Then p = p & "\"
-
- Range("A2:G" & Rows.Count).ClearContents
- Application.ScreenUpdating = False
-
- Dim Conn As Object, rs As Object, Target As Range, Dict As Object
- Dim strConn As String, SQL As String, f As String, s As String ' p As String,
- Dim Flag As Boolean, i As Integer
-
- Set Target = Range("A3")
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
-
- s = "Excel 12.0;HDR=yes;Database="
- If Application.Version < 12 Then
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls?")
- SQL = "SELECT * FROM [" & s & p & "[f]].[$A2:G] WHERE "
-
- While Len(f)
- If p & f <> ThisWorkbook.FullName Then
- If Not Flag Then
- Set rs = Conn.Execute(Replace(SQL, "[f]", f) & "FALSE")
- For i = 0 To rs.Fields.Count - 1
- If Not rs.Fields(i).Name Like "F[1-9]*" Then Range("A2").Offset(0, i) = rs.Fields(i).Name
- Next
- Set rs = Nothing
- Flag = True
- End If
- Dict.Add Replace(SQL, "[f]", f) & "LEN(姓名)", vbNullString
- If Dict.Count Mod 49 = 0 Then
- Target.CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- Set Target = Cells(Rows.Count, "A").End(xlUp).Offset(1)
- Dict.RemoveAll
- End If
- End If
- f = Dir
- Wend
- If Dict.Count Then Target.CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
-
- Conn.Close
- Set Conn = Nothing
- Set Target = Nothing
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|