|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test1() '练习,灌水增产……
-
- Dim i As Long, p As Long, iCount As Long
- Dim Conn As Object, dict As Object ', Fso As Object
- Dim strConn As String, strSQL As String, s As String
- Dim path_ As String, files_(1 To 2345) As String
-
- path_ = ThisWorkbook.Path & "\"
- ' If Not GetFileName(Files_, Path_, ".xlsx") Then MsgBox "!": Exit Sub
- GetFiles path_, CreateObject("Scripting.FileSystemObject"), files_(), iCount, "~$", ".xls"
- If iCount = 0 Then MsgBox "!": Exit Sub
-
- p = 4
- Rows(p & ":" & Rows.Count).ClearContents
-
- Application.ScreenUpdating = False
-
- Set Conn = CreateObject("ADODB.Connection")
- Set dict = CreateObject("Scripting.Dictionary")
-
- s = "Excel 12.0;HDR=YES;IMEX=1;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
-
- strSQL = "SELECT * FROM [" & s & "[.File]].[$B1:BP] WHERE `Lot Number_批号` IS NOT NULL"
- For i = 1 To iCount
- If files_(i) <> ThisWorkbook.FullName Then
- If Conn.State <> 1 Then Conn.Open strConn & files_(i)
- dict.Add Replace(strSQL, "[.File]", files_(i)), ""
- If dict.Count = 49 Then
- Range("A" & p).CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
- p = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
- dict.RemoveAll
- End If
- End If
- Next
- If dict.Count Then Range("A" & p).CopyFromRecordset Conn.Execute(Join(dict.Keys, " UNION ALL "))
-
- With ActiveSheet.UsedRange
- .Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- End With
-
- Conn.Close
- Set Conn = Nothing
- Set dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
- Function GetFiles(path_ As String, Fso As Object, files_() As String, iCount As Long, strExclude As String, Optional strFilter As String = ".xls")
- Dim folder_ As Object, file_ As Object
- For Each folder_ In Fso.GetFolder(path_).SubFolders
- For Each file_ In folder_.Files
- If InStr(LCase(file_.Name), strFilter) Then
- If Not file_.Name Like strExclude & "*" Then
- iCount = iCount + 1
- files_(iCount) = file_.Path
- End If
- End If
- Next
- Next
- End Function
复制代码 |
评分
-
2
查看全部评分
-
|