永发信息网

怎么将多个Excel工作簿合并成一个新的工作簿

答案:2  悬赏:60  手机版
解决时间 2021-12-29 23:52
  • 提问者网友:太高姿态
  • 2021-12-28 23:50
怎么将多个Excel工作簿合并成一个新的工作簿
最佳答案
  • 五星知识达人网友:低音帝王
  • 2021-12-29 01:11
把要合并的文件复制到一个新建文件夹中,在该文件夹中新建一个execl,仅打开新建Excel,按Alt+F11,点菜单的插入,模块,粘贴如下代码:
Sub 合并当前目录下所有工作簿的全部工作表()
Dim MyPath, MyName, AWbName
Dim Wb As Workbook, WbN As String
Dim G As Long
Dim Num As Long
Dim BOX As String
Application.ScreenUpdating = False
MyPath = ActiveWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls")
AWbName = ActiveWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set Wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
With Workbooks(1).ActiveSheet
.Cells(.Range("A65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("A65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("A1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub
全部回答
  • 1楼网友:山君与见山
  • 2021-12-29 02:46
方法一:原始办法,复制粘贴,适合表不多。 方法二:批量办法,vba解决,适合表很多,代码如下: sub 合并当前目录下所有工作簿的全部工作表() dim mypath, myname, awbname dim wb as workbook, wbn as string dim g as long dim num as long dim box as string application.screenupdating = false mypath = activeworkbook.path myname = dir(mypath & "\" & "*.xls") awbname = activeworkbook.name num = 0 do while myname <> "" if myname <> awbname then set wb = workbooks.open(mypath & "\" & myname) num = num + 1 with workbooks(1).activesheet .cells(.range("a65536").end(xlup).row + 2, 1) = left(myname, len(myname) - 4) for g = 1 to sheets.count wb.sheets(g).usedrange.copy .cells(.range("a65536").end(xlup).row + 1, 1) next wbn = wbn & chr(13) & wb.name wb.close false end with end if myname = dir loop range("a1").select application.screenupdating = true msgbox "共合并了" & num & "个工作薄下的全部工作表。如下:" & chr(13) & wbn, vbinformation, "提示" end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯