如何将一个 excel中的多个sheet的内容合并到一个sheet中?急求!!!
发布网友
发布时间:2023-06-20 04:04
我来回答
共1个回答
热心网友
时间:2023-10-06 03:46
这个用代码比较方便:
请参考我另一个回答:
http://zhidao.baidu.com/question/492504093231603692
在工作表最前面插入一个空白sheet——打开宏(按alt+F11)——插入模块——复制粘贴以下代码——F5运行
'首先在所有工作表最前面插入一个新的工作表,然后运行此程序即可。
Sub B5合并工作表1()
Cells.ClearContents
Dim i As Long, j As Long, j1 As Long, j2 As Long, k As Long, arr
Sheets(2).Range("a1").CurrentRegion.Copy Sheets(1).Range("a1")
j1 = Range("a1").End(xlToRight).Column + 1
Cells(1, j1) = "来自"
j2 = Cells(Rows.Count, 1).End(xlUp).Row - 1
ReDim arr(1 To j2)
For i = 1 To j2
arr(i) = Sheets(2).Name
Next
Cells(2, j1).Resize(j2) = arr
k = j2 + 2
For i = 3 To Sheets.Count
j = Cells(Rows.Count, 1).End(xlUp).Row + 1
Sheets(i).Range("a1").CurrentRegion.Offset(1, 0).Copy Sheets(1).Range("a" & j)
j2 = Sheets(i).Cells(Rows.Count, 1).End(xlUp).Row - 1
ReDim arr(1 To j2)
For j = 1 To j2
arr(j) = Sheets(i).Name
Next
Cells(k, j1).Resize(j2) = arr
k = k + j2
Next
End Sub