|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Sub test1()
Dim ar, i&, 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 & "].[$A4:M] WHERE 年级 IS NOT NULL"
End If
strFileName = Dir
Loop
If Len(strSQL) = 0 Then
Application.ScreenUpdating = True: MsgBox "未找到待汇总文件!", vbCritical: Exit Sub
Else
strSQL = Mid(strSQL, 12)
End If
Set cnn = CreateObject("ADODB.Connection")
cnn.Open strCnn
Set rst = cnn.Execute(strSQL)
With ThisWorkbook.Sheets(1)
.[A5].CopyFromRecordset rst
With .[A4].CurrentRegion
ar = .Columns(1).Value
If IsArray(ar) Then
For i = 2 To UBound(ar)
ar(i, 1) = i - 1
Next i
.Columns(1).Value = ar
End If
End With
.Activate
End With
rst.Close: cnn.Close
Set rst = Nothing: Set cnn = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
2
查看全部评分
-
|