vbs 合并execl数据
时间: 2024-05-13 11:15:35 浏览: 114
合并选区内相同行.zip
你可以使用 VBA 代码来合并 Excel 数据。以下是一个简单的示例代码,可以将两个 Excel 文件中的数据合并到一个新的 Excel 文件中:
```
Sub mergeExcelFiles()
Dim wbk1 As Workbook, wbk2 As Workbook, wbkNew As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet, wsNew As Worksheet
Dim lRow1 As Long, lRow2 As Long, lRowNew As Long
'Open the first workbook
Set wbk1 = Workbooks.Open("C:\Path\To\File1.xlsx")
Set ws1 = wbk1.Worksheets("Sheet1")
lRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
'Open the second workbook
Set wbk2 = Workbooks.Open("C:\Path\To\File2.xlsx")
Set ws2 = wbk2.Worksheets("Sheet1")
lRow2 = ws2.Cells(Rows.Count, 1).End(xlUp).Row
'Create a new workbook to store the merged data
Set wbkNew = Workbooks.Add
Set wsNew = wbkNew.Worksheets("Sheet1")
'Copy the data from the first workbook to the new workbook
ws1.Range("A1:F" & lRow1).Copy wsNew.Range("A1")
lRowNew = lRow1
'Copy the data from the second workbook to the new workbook
ws2.Range("A2:F" & lRow2).Copy wsNew.Range("A" & lRowNew + 1)
lRowNew = lRowNew + lRow2 - 1
'Save and close the new workbook
wbkNew.SaveAs "C:\Path\To\MergedFile.xlsx"
wbkNew.Close
'Close the other workbooks
wbk1.Close False
wbk2.Close False
MsgBox "Files merged successfully!"
End Sub
```
你只需要修改代码中的文件路径和工作表名称以及要合并的单元格范围即可。如果你想要进一步学习 VBA 的话,可以参考 Microsoft 的官方文档。
阅读全文