|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
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 & "\"
ActiveSheet.UsedRange.Offset(1).ClearContents
Application.ScreenUpdating = False
Dim ar, Conn As Object, Dict As Object
Dim s As String, f As String
Dim strConn As String, SQL As String, SQL_ As String
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;HDR=YES';Data Source="
Else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
End If
Conn.Open strConn & ThisWorkbook.FullName
'p = ThisWorkbook.Path & "\"
f = Dir(p & "*.xls?")
SQL = "SELECT 序号,姓名,身份证号,'[.Name]' AS 工程名称,'[.Date]' AS 时间 FROM [" & s & p & "[.File]].[$A3:E] WHERE 身份证号 IS NOT NULL"
SQL_ = "SELECT * FROM [" & Replace(s, "YES", "NO") & p & "[.File]].[$A1:A2]"
Do
If p & f <> ThisWorkbook.FullName Then
ar = Conn.Execute(Replace(SQL_, "[.File]", f)).GetRows
ar(0, 0) = Split(ar(0, 0), "工资信息")(0)
'ar(0, 1) = Split(ar(0, 1), ":")(1)
Dict.Add Replace(Replace(Replace(SQL, "[.File]", f), "[.Name]", ar(0, 1)), "[.Date]", ar(0, 0)), vbNullString
If Dict.Count = 49 Then '一次最多能联合查询49个工作表 当工作簿很多时超作用
Cells(Rows.Count, "A").End(xlUp).Offset(1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
Dict.RemoveAll
End If
End If
f = Dir
Loop While Len(f)
If Dict.Count Then
Cells(Rows.Count, "A").End(xlUp).Offset(1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
Dict.RemoveAll
End If
Conn.Close
Set Conn = Nothing
Set Dict = Nothing
Application.ScreenUpdating = True
Beep
End Sub
Sub test2()
Dim Fls As FileDialogSelectedItems
With Application.FileDialog(msoFileDialogFilePicker) '多选文件
.InitialFileName = ThisWorkbook.Path
With .Filters
.Clear
.Add "Excel Files", "*.xls?"
End With
.AllowMultiSelect = True
If .Show Then Set Fls = .SelectedItems Else Exit Sub
End With
ActiveSheet.UsedRange.Offset(1).ClearContents
Application.ScreenUpdating = False
Dim ar, Conn As Object, Dict As Object, f
Dim strConn As String, SQL As String, SQL_ As String, s As String
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;HDR=YES';Data Source="
Else
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
End If
Conn.Open strConn & ThisWorkbook.FullName
SQL = "SELECT 序号,姓名,身份证号,'[.Name]' AS 工程名称,'[.Date]' AS 时间 FROM [" & s & "[.File]].[$A3:E] WHERE 身份证号 IS NOT NULL"
SQL_ = "SELECT * FROM [" & Replace(s, "YES", "NO") & "[.File]].[$A1:A2]"
For Each f In Fls
If f <> ThisWorkbook.FullName Then
ar = Conn.Execute(Replace(SQL_, "[.File]", f)).GetRows
ar(0, 0) = Split(ar(0, 0), "工资信息")(0)
'ar(0, 1) = Split(ar(0, 1), ":")(1)
Dict.Add Replace(Replace(Replace(SQL, "[.File]", f), "[.Name]", ar(0, 1)), "[.Date]", ar(0, 0)), vbNullString
If Dict.Count = 49 Then '一次最多能联合查询49个工作表 当工作簿很多时超作用
Cells(Rows.Count, "A").End(xlUp).Offset(1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
Dict.RemoveAll
End If
End If
Next
If Dict.Count Then
Cells(Rows.Count, "A").End(xlUp).Offset(1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
Dict.RemoveAll
End If
Conn.Close
Set Conn = Nothing
Set Dict = Nothing
Application.ScreenUpdating = True
Beep
End Sub |
|