如何将几个工作表的数据合并到一个表格
发布网友
发布时间:2022-04-23 22:52
我来回答
共1个回答
热心网友
时间:2023-10-12 18:45
合并方法如下:
把需要合并的excel表格文档放到同一个文件夹里
新建一个“数据合并.xlsx“文档
打开“数据合并.xlsx“文档,在”Sheet1“工作表的地方右键→查看代码(快捷键:“Alt+F11”,尽量选用快捷方式)进入到Microsoft Visual Basic for Applications窗口
双击工程资源管理器里面的sheet1,在右侧的代码区粘贴如下代码:
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
运行→运行子过程/用户窗体(或者直接点击运行按钮,快捷键:F5),即可合并所有Excel表格到”数据合并.xlsx“文档的Sheet1工作表里面
完成Excel表格的合并。