其他分享
首页 > 其他分享> > 2022-05-26 excel-VBA代码-快速合并多个工作表

2022-05-26 excel-VBA代码-快速合并多个工作表

作者:互联网

前提:

先新建一个excel工作表,命名为“合并表”。

被合并的多个工作表的表头字段相同,并且被合并的工作表与合并后的“合并表”文件要在同一目录下。

Excel软件与wps软件,都是点击<开发工具> --> <VB编辑器> --> <插入> --> <模块> ,将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 & "\" & "*.xlsx")
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("B65536").End(xlUp).Row + 2, 1) = Left(MyName, Len(MyName) - 4)
For G = 1 To Sheets.Count
Wb.Sheets(G).UsedRange.Copy .Cells(.Range("B65536").End(xlUp).Row + 1, 1)
Next
WbN = WbN & Chr(13) & Wb.Name
Wb.Close False
End With
End If
MyName = Dir
Loop
Range("B1").Select
Application.ScreenUpdating = True
MsgBox "共合并了" & Num & "个工作薄下的全部工作表。如下:" & Chr(13) & WbN, vbInformation, "提示"
End Sub

 

标签:Dim,26,End,Wb,05,合并,excel,MyName,Num
来源: https://www.cnblogs.com/crackerroot/p/16314559.html