VB合并excel表格
时间: 2023-12-16 11:02:53 浏览: 140
vba excel 表格 合并
您可以使用以下VB代码来合并Excel表格:
```vb
Sub MergeExcelSheets()
Dim mainWorkbook As Workbook
Dim sourceWorkbook As Workbook
Dim mainWorksheet As Worksheet
Dim sourceWorksheet As Worksheet
Dim lastRow As Long
'打开主工作簿
Set mainWorkbook = ThisWorkbook
'循环遍历要合并的工作簿
For Each sourceWorkbook In Application.Workbooks
'排除已经是主工作簿的情况
If sourceWorkbook.Name <> mainWorkbook.Name Then
'循环遍历要合并的工作表
For Each sourceWorksheet In sourceWorkbook.Worksheets
'找到主工作簿的最后一行
Set mainWorksheet = mainWorkbook.Worksheets(sourceWorksheet.Name)
lastRow = mainWorksheet.Cells(mainWorksheet.Rows.Count, 1).End(xlUp).Row + 1
'复制要合并的工作表到主工作簿中
sourceWorksheet.UsedRange.Copy Destination:=mainWorksheet.Cells(lastRow, 1)
Next sourceWorksheet
End If
Next sourceWorkbook
End Sub
```
请确保您已经打开想要合并的Excel文件以及主工作簿,然后运行上述代码即可将所有工作簿中的工作表合并到主工作簿中。
阅读全文