|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Option Explicit
Sub test1()
Dim j&, cnn As Object, rst As Object, strSQL$, strCnn$, strJoin$, strPath$, strFileName$
Application.ScreenUpdating = False
strJoin = "Excel 12.0;HDR=YES;IMEX=0;Database="
Select Case Application.Version * 1
Case Is <= 11
strJoin = Replace(strJoin, "12.0;", "8.0;")
strCnn = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties='Excel 8.0;HDR=YES;IMEX=0'"
Case Is >= 12
strCnn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=YES;IMEX=0'"
End Select
strPath = ThisWorkbook.Path & "\"
strFileName = Dir(strPath & "*.xls")
Do Until strFileName = ""
If strFileName <> ThisWorkbook.Name Then
strSQL = strSQL & " UNION ALL SELECT * FROM [" & strJoin & strPath & strFileName & "].[$A1:B]"
End If
strFileName = Dir
Loop
If Len(strSQL) = 0 Then
Application.ScreenUpdating = True: MsgBox "未找到待汇总文件!", vbCritical: Exit Sub
Else
strSQL = "SELECT 扫描号码 FROM(" & Mid(strSQL, 12) & ") WHERE 日期=#" & Worksheets(2).[G1].Value & "#"
End If
Set cnn = CreateObject("ADODB.Connection")
cnn.Open strCnn
Set rst = cnn.Execute(strSQL)
With ThisWorkbook.Sheets(1)
.Columns("G").Clear
.Range("Q2").CopyFromRecordset rst
.Activate
End With
rst.Close: cnn.Close
Set rst = Nothing: Set cnn = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
2
查看全部评分
-
|