ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
Python自动化办公应用大全 Excel 2021函数公式学习大典 Kutools for Office 套件发布 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: olin12345

[已解决] 多文件数据汇总

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-11-1 13:10 | 显示全部楼层
cjc209 发表于 2025-11-1 13:06
你的表问题 关键是表列数不一致 而且是乱序 要汇总后数据对位 就要在提取数据的时候 先对好列的位置 这才是 ...

一语中的,这也是求助的核心,列是乱序,就是搞不懂怎么操作了,所以求助了。

TA的精华主题

TA的得分主题

发表于 2025-11-1 13:12 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2025-11-1 17:17 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
插件解法...仅供参考
2025-11-01_171553.png

TA的精华主题

TA的得分主题

发表于 2025-11-1 20:03 | 显示全部楼层
附件供参考。。。

新建文件夹.zip

43.78 KB, 下载次数: 12

TA的精华主题

TA的得分主题

发表于 2025-11-1 20:04 | 显示全部楼层
  1. Sub ykcbf()     '//2025.11.1
  2.     With Application
  3.         .ScreenUpdating = False
  4.         .DisplayAlerts = False
  5.         .Calculation = xlCalculationManual
  6.         .EnableEvents = False
  7.     End With
  8.     Set fso = CreateObject("scripting.filesystemobject")
  9.     Set d = CreateObject("Scripting.Dictionary")
  10.     Set d1 = CreateObject("Scripting.Dictionary")
  11.     p = ThisWorkbook.Path & Application.PathSeparator
  12.     Dim tm: tm = Timer
  13.     arr = Sheets("表2").UsedRange.Value
  14.     For j = 1 To UBound(arr, 2)
  15.         d1(arr(1, j)) = j
  16.     Next
  17.     arr = Sheets("表1").Range("a1").CurrentRegion.Value
  18.     For i = 2 To UBound(arr)
  19.        If Len(arr(i, 1)) Then d(arr(i, 1) & "") = CStr(arr(i, 2))
  20.     Next
  21.     ReDim brr(1 To 10 ^ 5, 1 To d1.Count)
  22.     For Each f In fso.GetFolder(p).Files
  23.         If LCase(f.Name) Like "*.xls*" _
  24.             And Not f.Name Like "~$*" _
  25.             And f.Path <> ThisWorkbook.FullName Then
  26.             fn = fso.GetBaseName(f)
  27.             If d.exists(fn) Then
  28.                 Set wb = Workbooks.Open(f.Path, 0, True, , False)
  29.                 arr = wb.Sheets(1).UsedRange.Value
  30.                 wb.Close False
  31.                 For i = 2 To UBound(arr)
  32.                     If CStr(arr(i, 2)) = d(fn) Then
  33.                         m = m + 1
  34.                         For j = 1 To UBound(arr, 2)
  35.                             s = arr(1, j)
  36.                             If d1.exists(s) Then
  37.                                 brr(m, d1(s)) = arr(i, j)
  38.                             End If
  39.                         Next
  40.                     End If
  41.                 Next
  42.             End If
  43.         End If
  44.     Next f
  45.     With Sheets("表2")
  46.         .UsedRange.Offset(1).ClearContents
  47.         .[a2].Resize(m, d1.Count) = brr
  48.         .[a2].Resize(m, d1.Count).Borders.LineStyle = 1
  49.     End With
  50.     With Application
  51.         .DisplayAlerts = True
  52.         .ScreenUpdating = True
  53.         .Calculation = xlCalculationAutomatic
  54.         .EnableEvents = True
  55.     End With
  56.     MsgBox "共用时:" & Format(Timer - tm, "0.000") & "秒!"
  57. End Sub
复制代码


评分

2

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2025-11-1 21:06 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
ykcbf1100 发表于 2025-11-1 20:03
附件供参考。。。

感谢草爷老师帮助。

TA的精华主题

TA的得分主题

发表于 2025-11-3 20:46 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Sub CopyMatchedColumnsByHeader()
    Dim ws1 As Worksheet, ws2 As Worksheet ' 总表中的表1和表2
    Dim externalWb As Workbook ' 外部目标工作簿
    Dim targetWs As Worksheet ' 外部工作簿中的目标工作表
    Dim lastRow1 As Long, lastRowExternal As Long, lastRow2 As Long
    Dim lastCol2 As Long ' 表2的最后一列(用于获取表头范围)
    Dim i As Long, j As Long, k As Long, extIndex As Long
    Dim baseName As String, fullWbName As String ' 文件名(无扩展名/完整名)
    Dim targetValue As String ' 表1B列的匹配值
    Dim folderPath As String ' 总表所在路径
    Dim copyCount As Long ' 复制行数计数
    Dim headerMatch As Boolean ' 标记是否找到表头匹配的列
    ' 常见Excel扩展名(按优先级排序)
    Dim extensions As Variant: extensions = Array(".xlsx", ".xlsm", ".xls")
   
    ' ----------------------需确认的参数----------------------
    Set ws1 = ThisWorkbook.Worksheets("表1") ' 表1名称(可修改)
    Set ws2 = ThisWorkbook.Worksheets("表2") ' 表2名称(可修改)
    Dim targetSheetName As String: targetSheetName = "" ' 外部表名(空则默认第一个表)
    Dim externalMatchCol As String: externalMatchCol = "B" ' 外部表中用于匹配的列(如B列)
    ' --------------------------------------------------------
   
    ' 初始化路径和表2基础信息
    folderPath = ThisWorkbook.Path & "\"
    lastRow1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row ' 表1最后一行
    lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row + 1 ' 表2开始粘贴的行
    lastCol2 = ws2.Cells(1, ws2.Columns.Count).End(xlToLeft).Column ' 表2最后一列(表头范围)
   
    ' 遍历表1的每一行(从第2行开始)
    For i = 2 To lastRow1
        baseName = ws1.Cells(i, "A").Value ' 表1A列的文件名(无扩展名)
        targetValue = ws1.Cells(i, "B").Value ' 表1B列的匹配值
        fullWbName = "" ' 重置完整文件名
        copyCount = 0 ' 重置复制计数
        
        ' 跳过空文件名
        If baseName = "" Then
            MsgBox "表1行" & i & "的文件名为空,跳过", vbExclamation
            GoTo NextRow
        End If
        
        ' 自动匹配扩展名
        For extIndex = LBound(extensions) To UBound(extensions)
            If Dir(folderPath & baseName & extensions(extIndex)) <> "" Then
                fullWbName = baseName & extensions(extIndex)
                Exit For
            End If
        Next extIndex
        
        ' 未找到文件
        If fullWbName = "" Then
            MsgBox "未找到文件:" & baseName & "(格式:.xlsx/.xlsm/.xls),跳过行" & i, vbExclamation
            GoTo NextRow
        End If
        
        ' 打开外部工作簿
        On Error Resume Next
        Set externalWb = Workbooks.Open(folderPath & fullWbName, ReadOnly:=True)
        On Error GoTo 0
        If externalWb Is Nothing Then
            MsgBox "文件 " & fullWbName & " 无法打开,跳过行" & i, vbExclamation
            GoTo NextRow
        End If
        
        ' 指定外部工作表
        If targetSheetName = "" Then
            Set targetWs = externalWb.Worksheets(1)
        Else
            On Error Resume Next
            Set targetWs = externalWb.Worksheets(targetSheetName)
            On Error GoTo 0
            If targetWs Is Nothing Then
                MsgBox fullWbName & "中无表" & targetSheetName & ",跳过行" & i, vbExclamation
                externalWb.Close False
                GoTo NextRow
            End If
        End If
        
        ' 查找外部表中匹配B列值的行,并按表2表头复制对应列
        lastRowExternal = targetWs.Cells(targetWs.Rows.Count, externalMatchCol).End(xlUp).Row
        For j = 2 To lastRowExternal ' 从第2行开始(跳过外部表表头)
            ' 匹配外部表的指定列(如B列)与表1B列的值
            If targetWs.Cells(j, externalMatchCol).Value = targetValue Then
                headerMatch = False ' 重置表头匹配标记
                ' 遍历表2的表头列,复制匹配的内容
                For k = 1 To lastCol2
                    ' 在外部表的表头行(第1行)查找与表2表头相同的列
                    Dim externalCol As Long
                    On Error Resume Next
                    externalCol = targetWs.Rows(1).Find( _
                        What:=ws2.Cells(1, k).Value, _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole).Column ' 精确匹配列名
                    On Error GoTo 0
                    
                    ' 若找到匹配列,复制数据到表2对应列
                    If externalCol > 0 Then
                        ws2.Cells(lastRow2, k).Value = targetWs.Cells(j, externalCol).Value
                        headerMatch = True ' 标记有匹配列
                    End If
                Next k
               
                ' 若有至少一个列匹配,记录行数并移动到表2下一行
                If headerMatch Then
                    lastRow2 = lastRow2 + 1
                    copyCount = copyCount + 1
                End If
            End If
        Next j
      
        ' 关闭外部工作簿
        externalWb.Close SaveChanges:=False
        Set externalWb = Nothing: Set targetWs = Nothing
        
NextRow:
    Next i
   
    MsgBox "所有操作完成!", vbInformation
End Sub

TA的精华主题

TA的得分主题

发表于 2025-11-5 08:09 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2025-12-16 05:12 , Processed in 0.038824 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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