在 Excel 中,可以通過編寫宏(VBA代碼)來實(shí)現(xiàn)將多個(gè)工作表中的數(shù)據(jù)合并到一個(gè)工作表中。具體操作步驟如下:
- 按下 Alt + F11 鍵打開 Visual Basic 編輯器;
- 在項(xiàng)目資源管理器窗口中右鍵點(diǎn)擊“VBAProject(Workbook名稱)”,選擇插入 - 模塊;
- 在新建的模塊中輸入以下代碼:
Sub mergeData()
'申明范圍
Dim sht As Worksheet, dest As Worksheet
Dim lastRow As Long, lastCol As Long
'定義目標(biāo)表格
Set dest = ThisWorkbook.Worksheets("Sheet0")
'合并行
For Each sht In Workbooks("要復(fù)制數(shù)據(jù)的工作表.xlsx").Worksheets
If?sht.Name?<> "Sheet0" Then
lastRow = dest.Cells(Rows.Count, 1).End(xlUp).Row + 1
lastCol = sht.Cells(1, Columns.Count).End(xlToLeft).Column
sht.Range(sht.Cells(1, 1), sht.Cells(lastRow, lastCol)).Copy dest.Range("A" & lastRow)
End If
Next
End Sub
- 保存文件,然后關(guān)閉 VBA 編輯器;
- 在 Excel 中打開要操作的工作簿,并按下 Alt + F8 打開宏對(duì)話框;
- 選擇剛剛編寫的“mergeData”宏并運(yùn)行即可。
通過上述步驟,就可以快速地將多個(gè)工作表中的數(shù)據(jù)合并到一個(gè)目標(biāo)工作表中了。