|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub Main()
- Dim strPath As String, strZipFile As String, vZipFilesList() As String, iCount As Integer
- strPath = ThisWorkbook.Path & "\"
- strZipFile = Dir(strPath & "*.zip")
- While Len(strZipFile)
- UnZipFiles strPath, strZipFile, vZipFilesList, iCount
- strZipFile = Dir
- Wend
- If iCount = 0 Then Exit Sub
- Dim Fso As Object, vExcelFiles() As String, vFoldersList() As String
- Dim i As Long, j As Long, k As Long, n As Long
- Set Fso = CreateObject("Scripting.FileSystemObject")
- For i = 1 To UBound(vZipFilesList)
- If InStr(Dir(vZipFilesList(i), vbDirectory), ".xls") Then
- n = n + 1
- ReDim Preserve vExcelFiles(1 To n)
- vExcelFiles(n) = vZipFilesList(i)
- Else
- GetExcelFiles vZipFilesList(i), Fso, vExcelFiles, n
- k = k + 1
- ReDim Preserve vFoldersList(1 To k)
- vFoldersList(k) = vZipFilesList(i)
- End If
- Next
- Dim Conn As Object, rs As Object, Dict As Object, Cel As Range, vTemp, Flag As Boolean
- Dim strConn As String, SQL As String, strFields As String, s As String, vFields() As String
- ActiveSheet.UsedRange.ClearContents
- Application.ScreenUpdating = False
- Set Cel = Range("A2")
- 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;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
- For i = 1 To UBound(vExcelFiles)
- If i = 1 Then
- Set rs = Conn.Execute("SELECT * FROM [" & s & vExcelFiles(i) & "].[$A1:E] WHERE FALSE")
- ReDim vFields(rs.Fields.Count)
- vFields(0) = "'" & Mid(vExcelFiles(i), InStrRev(vExcelFiles(i), "") + 1) & "' AS 文件名"
- For j = 1 To rs.Fields.Count
- vFields(j) = "[" & rs.Fields(j - 1).Name & "]"
- Next
- End If
- SQL = "SELECT " & Join(vFields, ",") & " FROM [" & s & vExcelFiles(i) & "].[$A1:E] WHERE LEN(" & vFields(1) & ")"
- Dict.Add SQL, vbNullString
- If Dict.Count = 49 Then
- Set rs = Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- If Not Flag Then
- For j = 0 To rs.Fields.Count - 1
- Cel.Offset(-1, j) = rs.Fields(j).Name
- Next
- Flag = True
- End If
- Cel.CopyFromRecordset rs
- Set Cel = Cel.End(xlDown).Offset(1)
- Dict.RemoveAll
- End If
- Next
- If Dict.Count Then
- Set rs = Conn.Execute(Join(Dict.Keys, " UNION ALL "))
- If Not Flag Then
- For j = 0 To rs.Fields.Count - 1
- Cel.Offset(-1, j) = rs.Fields(j).Name
- Next
- End If
- Cel.CopyFromRecordset rs
- End If
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Cel = Nothing
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- For i = 1 To UBound(vExcelFiles)
- Fso.DeleteFile vExcelFiles(i), True
- Next
- If k Then
- For j = 1 To UBound(vFoldersList)
- Fso.DeleteFolder vFoldersList(j), True
- Next
- End If
- Set Fso = Nothing
- End Sub
- Function UnZipFiles(strPath As String, strZipFile As String, vZipFilesList() As String, iCount As Integer)
- Dim objFolder As Object
- Dim objFolderItem As Object
- Dim objFolderItemVerbs As Object
- If Len(strZipFile) Then
- Dim objShell As Object
- Set objShell = CreateObject("Shell.Application")
- With objShell
- Set objFolder = .Namespace(strPath & strZipFile)
- With objFolder.ParentFolder
- .CopyHere objFolder.Items, 4 + 16
- End With
- End With
- Set objFolder = Nothing
- GetAllZipFilesList objShell, strPath & strZipFile, vZipFilesList, iCount
- Set objShell = Nothing
- End If
- End Function
- Function GetAllZipFilesList(objShell As Object, strZipFile, vZipFilesList() As String, iCount As Integer)
- Dim objItem As Object
- For Each objItem In objShell.Namespace(strZipFile).Items
- iCount = iCount + 1
- ReDim Preserve vZipFilesList(1 To iCount)
- vZipFilesList(iCount) = Left(strZipFile, InStrRev(strZipFile, "")) & objItem.Name
- Next
- End Function
- Function GetExcelFiles(strPath As String, objFso As Object, vExcelFiles() As String, n As Long)
- Dim objSubFolder, objExcelFile
- For Each objExcelFile In objFso.GetFolder(strPath).Files
- If InStr(objExcelFile.Name, ".xls") Then
- n = n + 1
- ReDim Preserve vExcelFiles(1 To n)
- vExcelFiles(n) = objExcelFile.Path
- End If
- Next
- For Each objSubFolder In objFso.GetFolder(strPath).SubFolders
- GetExcelFiles objSubFolder.Path, objFso, vExcelFiles, n
- Next
- End Function
复制代码 |
|