用VBA算是勉强实现了,后续有时间再优化吧Sub 汇总数据()
Sheets.Add
With ActiveSheet
.Name = "汇总表" & Format(Now, "hhmmss")
For Each s In ThisWorkbook.Sheets
s.UsedRange.Copy .Cells(.UsedRange.Rows.Count + 1, 1)
Next
End With
End Sub
Sub 合并EXCEL文件()
ActiveWorkbook.Save '先保存目标文件
Dim FilesToOpen '缺省情况被声明为 Variant
Dim X As Integer
Dim I As Integer
Dim Mname As String
Dim Oname As String
Dim Keyn As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
Mname = ActiveWorkbook.Name '目标文件名
FilesToOpen = Application.GetOpenFilename(FileFilter:="MicroSoft Excel文件(*.xls),*.xls", Title:="", MultiSelect:=True)
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "没有选中文件"
GoTo ExitHandler
End If
Keyn = InputBox("请输入需要合并的工作表名(包含关键字)" & _
Chr(10) & "后续所选文件的全部工作表导入,直接按确定或取消") ', , ActiveSheet.name)
X = 1
While X <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(X)
With ActiveWorkbook
Oname = .Name '源文件名
For I = 1 To .Sheets.Count
If InStr(Sheets(I).Name, Keyn) Then '如果Keyn为空则全部复制
.Sheets(I).Copy after:=Workbooks(Mname).Sheets(Workbooks(Mname).Sheets.Count) '复制到目标文件的最后一张表
Workbooks(Mname).Sheets(Workbooks(Mname).Sheets.Count).Name = Replace(Oname, ".xls", "-") & .Sheets(I).Name '重命名
End If
Next I
.Close '源文件关闭
X = X + 1
End With
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub |