ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 558|回复: 6

[求助] 怎么用VBA把压缩文件中的表格合并在一张表里

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-16 19:24 | 显示全部楼层 |阅读模式
本帖最后由 jin2001h 于 2022-11-16 19:27 编辑

怎么把多个压缩文件中的表格合并在一张总表里,效果如16日总表,谢谢各位大拿

16日.zip

23.94 KB, 下载次数: 11

TA的精华主题

TA的得分主题

发表于 2022-11-17 08:09 来自手机 | 显示全部楼层
本帖最后由 smsn 于 2022-11-17 19:40 编辑

1 压缩格式只有zip? rar 7z 等没有用吧?不加密吧?用什么压缩软件生成?自带zip,winrar,7zip,其他?
2 压缩文件内存在子目录吗? xlsx文件只放在压缩包内,没建目录吧?压缩包内会有多个文件吧?
3 合并的只有 xlsx 格式,有其他格式吗?


根据你现有条件简单写了一个。
  1. Sub unzipNcombine()
  2.     Dim FSO As Object, oApp As Object, fname As Variant, FileNameFolder As Variant, DefPath As String, strDate As String, I As Long, num As Long, J As Long, ListinZIP() As String
  3.     fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=True)
  4.     If IsArray(fname) = False Then
  5.     Else
  6.         DefPath = Application.DefaultFilePath 'DefPath = "C:\Users\aaa\test" '自定解压文件夹
  7.         If Right(DefPath, 1) <> "" Then
  8.             DefPath = DefPath & ""
  9.         End If
  10.         strDate = Format(Now, " dd-mm-yy h-mm-ss")
  11.         FileNameFolder = DefPath & "MyUnzipFolder " & strDate & ""
  12.         MkDir FileNameFolder
  13.         Set oApp = CreateObject("Shell.Application")
  14.         For I = LBound(fname) To UBound(fname)
  15.             num = oApp.Namespace(FileNameFolder).Items.Count
  16.             oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname(I)).Items
  17.             For Each Item In oApp.Namespace(fname(I)).Items
  18.                 J = J + 1
  19.                 Dim orgnName As String, destName As String
  20.                 orgnName = FileNameFolder & Item.Name: destName = FileNameFolder & Split(Dir(fname(I)), ".")(0) & "." & Item.Name
  21.                 Name orgnName As destName
  22.             Next
  23.         Next I
  24.         On Error Resume Next
  25.         Set FSO = CreateObject("scripting.filesystemobject")
  26.         FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
  27.     End If
  28.     Shell "explorer.exe " & FileNameFolder, vbNormalFocus
  29.     'Combine files
  30.     Dim title_row As Integer, last_col As String, not_null_col As Integer, from_path As String, to_path As String, wb As Workbook, n As Integer, temparr, shopname(), lastrow As Integer
  31.     '
  32.     title_row = 1    '定义标题行所在行号
  33.     last_col = "E"   '定义区域尾列所在列名
  34.     from_path = FileNameFolder:     to_path = FileNameFolder
  35.     Application.ScreenUpdating = False
  36.     fname = Dir(from_path)
  37.     Set wb = Workbooks.Add
  38.     '
  39.     With wb.Sheets(1)
  40.         Columns(1).NumberFormat = "@":        Columns(3).NumberFormat = "m/d/yyyy"
  41.         Range("A1:E1") = Split("店铺,单号,下单时间,订单,客户", ",")
  42.         Do While fname <> ""
  43.             Workbooks.Open Filename:=from_path & fname:            Rows("1:1").Clear
  44.             temparr = Sheets(1).UsedRange
  45.             ReDim shopname(1 To UBound(temparr, 1) + 1, 1 To 1)
  46.             Workbooks(fname).Close SaveChanges:=False
  47.             lastrow = Cells(Rows.Count, 2).End(xlUp).Row + 1
  48.             Range("B" & lastrow).Resize(UBound(temparr, 1), 4) = temparr
  49.             Range("A" & lastrow & ":A" & lastrow + UBound(temparr, 1) - 1) = Split(fname, ".")(0)
  50.             fname = Dir()
  51.         Loop
  52.     End With
  53.     Cells.EntireColumn.AutoFit:    wb.SaveAs Filename:=to_path & "CombinF.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
  54.     Application.ScreenUpdating = True
  55.     'MsgBox "Done", vbOK
  56. End Sub
复制代码


zipncombine.zip

32.87 KB, 下载次数: 11

TA的精华主题

TA的得分主题

发表于 2022-11-17 16:44 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Sub Main()
  2.   Dim strPath As String, strZipFile As String, vZipFilesList() As String, iCount As Integer

  3.   strPath = ThisWorkbook.Path & "\"
  4.   strZipFile = Dir(strPath & "*.zip")
  5.   While Len(strZipFile)
  6.     UnZipFiles strPath, strZipFile, vZipFilesList, iCount
  7.     strZipFile = Dir
  8.   Wend
  9.   If iCount = 0 Then Exit Sub

  10.   Dim Fso As Object, vExcelFiles() As String, vFoldersList() As String
  11.   Dim i As Long, j As Long, k As Long, n As Long
  12.   Set Fso = CreateObject("Scripting.FileSystemObject")

  13.   For i = 1 To UBound(vZipFilesList)
  14.     If InStr(Dir(vZipFilesList(i), vbDirectory), ".xls") Then
  15.       n = n + 1
  16.       ReDim Preserve vExcelFiles(1 To n)
  17.       vExcelFiles(n) = vZipFilesList(i)
  18.     Else
  19.       GetExcelFiles vZipFilesList(i), Fso, vExcelFiles, n
  20.       k = k + 1
  21.       ReDim Preserve vFoldersList(1 To k)
  22.       vFoldersList(k) = vZipFilesList(i)
  23.     End If
  24.   Next
  25.   Dim Conn As Object, rs As Object, Dict As Object, Cel As Range, vTemp, Flag As Boolean
  26.   Dim strConn As String, SQL As String, strFields As String, s As String, vFields() As String

  27.   ActiveSheet.UsedRange.ClearContents
  28.   Application.ScreenUpdating = False

  29.   Set Cel = Range("A2")
  30.   Set Dict = CreateObject("Scripting.Dictionary")
  31.   Set Conn = CreateObject("ADODB.Connection")

  32.   s = "Excel 12.0;HDR=yes;Database="
  33.   If Application.Version < 12 Then
  34.     s = Replace(s, "12.0", "8.0")
  35.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
  36.   Else
  37.     strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
  38.   End If
  39.   Conn.Open strConn & ThisWorkbook.FullName

  40.   For i = 1 To UBound(vExcelFiles)
  41.     If i = 1 Then
  42.       Set rs = Conn.Execute("SELECT * FROM [" & s & vExcelFiles(i) & "].[$A1:E] WHERE FALSE")
  43.       ReDim vFields(rs.Fields.Count)
  44.       vFields(0) = "'" & Mid(vExcelFiles(i), InStrRev(vExcelFiles(i), "") + 1) & "' AS 文件名"
  45.       For j = 1 To rs.Fields.Count
  46.         vFields(j) = "[" & rs.Fields(j - 1).Name & "]"
  47.       Next
  48.     End If
  49.     SQL = "SELECT " & Join(vFields, ",") & " FROM [" & s & vExcelFiles(i) & "].[$A1:E] WHERE LEN(" & vFields(1) & ")"
  50.     Dict.Add SQL, vbNullString
  51.     If Dict.Count = 49 Then
  52.       Set rs = Conn.Execute(Join(Dict.Keys, " UNION ALL "))
  53.       If Not Flag Then
  54.         For j = 0 To rs.Fields.Count - 1
  55.           Cel.Offset(-1, j) = rs.Fields(j).Name
  56.         Next
  57.         Flag = True
  58.       End If
  59.       Cel.CopyFromRecordset rs
  60.       Set Cel = Cel.End(xlDown).Offset(1)
  61.       Dict.RemoveAll
  62.     End If
  63.   Next
  64.   If Dict.Count Then
  65.     Set rs = Conn.Execute(Join(Dict.Keys, " UNION ALL "))
  66.     If Not Flag Then
  67.       For j = 0 To rs.Fields.Count - 1
  68.         Cel.Offset(-1, j) = rs.Fields(j).Name
  69.       Next
  70.     End If
  71.     Cel.CopyFromRecordset rs
  72.   End If

  73.   Set rs = Nothing
  74.   Conn.Close
  75.   Set Conn = Nothing
  76.   Set Cel = Nothing
  77.   Set Dict = Nothing

  78.   Application.ScreenUpdating = True
  79.   Beep

  80.   For i = 1 To UBound(vExcelFiles)
  81.     Fso.DeleteFile vExcelFiles(i), True
  82.   Next

  83.   If k Then
  84.     For j = 1 To UBound(vFoldersList)
  85.       Fso.DeleteFolder vFoldersList(j), True
  86.     Next
  87.   End If

  88.   Set Fso = Nothing
  89. End Sub

  90. Function UnZipFiles(strPath As String, strZipFile As String, vZipFilesList() As String, iCount As Integer)
  91.   Dim objFolder As Object
  92.   Dim objFolderItem As Object
  93.   Dim objFolderItemVerbs As Object
  94.   If Len(strZipFile) Then
  95.     Dim objShell As Object
  96.     Set objShell = CreateObject("Shell.Application")
  97.     With objShell
  98.       Set objFolder = .Namespace(strPath & strZipFile)
  99.       With objFolder.ParentFolder
  100.         .CopyHere objFolder.Items, 4 + 16
  101.       End With
  102.     End With
  103.     Set objFolder = Nothing

  104.     GetAllZipFilesList objShell, strPath & strZipFile, vZipFilesList, iCount

  105.     Set objShell = Nothing
  106.   End If
  107. End Function

  108. Function GetAllZipFilesList(objShell As Object, strZipFile, vZipFilesList() As String, iCount As Integer)
  109.   Dim objItem As Object
  110.   For Each objItem In objShell.Namespace(strZipFile).Items
  111.     iCount = iCount + 1
  112.     ReDim Preserve vZipFilesList(1 To iCount)
  113.     vZipFilesList(iCount) = Left(strZipFile, InStrRev(strZipFile, "")) & objItem.Name
  114.   Next
  115. End Function

  116. Function GetExcelFiles(strPath As String, objFso As Object, vExcelFiles() As String, n As Long)
  117.   Dim objSubFolder, objExcelFile
  118.   For Each objExcelFile In objFso.GetFolder(strPath).Files
  119.     If InStr(objExcelFile.Name, ".xls") Then
  120.       n = n + 1
  121.       ReDim Preserve vExcelFiles(1 To n)
  122.       vExcelFiles(n) = objExcelFile.Path
  123.     End If
  124.   Next
  125.   For Each objSubFolder In objFso.GetFolder(strPath).SubFolders
  126.     GetExcelFiles objSubFolder.Path, objFso, vExcelFiles, n
  127.   Next
  128. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2022-11-17 16:45 | 显示全部楼层
本帖最后由 fzxba 于 2022-11-17 18:49 编辑

更改51行放在55行后.rar (45.64 KB, 下载次数: 17)

审核效率还可以(2h)

TA的精华主题

TA的得分主题

发表于 2022-11-18 08:32 | 显示全部楼层
本帖最后由 smsn 于 2022-11-18 09:28 编辑
smsn 发表于 2022-11-17 08:09
1 压缩格式只有zip? rar 7z 等没有用吧?不加密吧?用什么压缩软件生成?自带zip,winrar,7zip,其他?
2 压 ...

49行留了个开关控制首列内容:
= Split(fname, ".")(0)  '显示店名
= Split(fname, ".")(1)  '显示文件名
=Split(fname, ".")(1) & "." & Split(fname, ".")(2) ' 显示文件名+扩展名
=fname '显示 店名.文件名.扩展名

TA的精华主题

TA的得分主题

发表于 2022-11-18 08:48 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
fzxba 发表于 2022-11-17 16:45
审核效率还可以(2h)

是什么样的回复,会触发论坛的审核?

TA的精华主题

TA的得分主题

发表于 2022-11-18 08:56 | 显示全部楼层
wengjl 发表于 2022-11-18 08:48
是什么样的回复,会触发论坛的审核?

出现了某些字符吧
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-21 02:24 , Processed in 0.033719 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表