|
发表于 2024-4-4 21:51
来自手机
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 baofa2 于 2024-4-5 07:38 编辑
据描述,真还是SQL做的事。
- Sub Main() '补
-
- Dim strPath As String
- strPath = ThisWorkbook.Path & "\"
-
- Dim vrtFiles(1 To 2345) As String
- Dim Conn As Object, Dict As Object, Fso As Object
- Dim i As Integer, iCount As Integer, pos As Long
- Dim strConn As String, strSQL As String, s As String
-
- Cells.ClearContents
- DoApp False
-
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
- Set Fso = CreateObject("Scripting.FileSystemObject")
-
- s = "Excel 12.0;HDR=NO;IMEX=1;Database="
- If Application.Version < 12 Then 'Or InStr(Application.Path, "WPS")
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO';Data Source="
- End If
- 'Conn.Open strConn & ThisWorkbook.FullName
-
- GetFiles strPath, Fso, vrtFiles, iCount, ThisWorkbook.Name, "待重设格式.xls"
- For i = 1 To iCount
- If i = 1 Then Conn.Open strConn & vrtFiles(i)
- strSQL = "SELECT * FROM [" & s & vrtFiles(i) & "].[Sheet1$A" & 1 - CInt(i > 1) & ":N] WHERE F1 IS NOT NULL"
- Dict.Add strSQL, vbNullString
- If Dict.Count = 49 Then
- Range("A" & pos + 1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- pos = Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
- Dict.RemoveAll
- End If
- Next
- If Dict.Count Then Range("A" & pos + 1).CopyFromRecordset Conn.Execute(Join(Dict.Keys, " UNION ALL "))
-
- Conn.Close
- Set Conn = Nothing
- Set Dict = Nothing
- Set Fso = Nothing
-
- DoApp
- Beep
- End Sub
- Function GetFiles(strPath As String, objFso As Object, vrtFiles() As String, iCount As Integer, strExclude As String, Optional strFilter As String = ".xls")
- Dim objSubFolder As Object, objFilterFile As Object
- For Each objFilterFile In objFso.GetFolder(strPath).Files
- If InStr(LCase(objFilterFile.Name), strFilter) Then
- If InStr(objFilterFile.Name, strExclude) = 0 Then
- iCount = iCount + 1
- vrtFiles(iCount) = objFilterFile.Path
- End If
- End If
- Next
- For Each objSubFolder In objFso.GetFolder(strPath).SubFolders
- GetFiles objSubFolder.Path, objFso, vrtFiles, iCount, strExclude, strFilter
- Next
- End Function
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- .Calculation = -b * 30 - 4135
- End With
- End Function
复制代码
|
评分
-
1
查看全部评分
-
|