如何使用VB宏代码合并两个Excel文件的内容。
Sub GetSheets()
Dim temp As String
Path = "C:\Users\....\ Desktop\Excel combine\"
Filename = Dir(Path & "*.xlsx")
Do While Filename "" Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
temp = ActiveWorkbook.Name ActiveSheet.Name = temp
ActiveWorkbook.Sheets(temp).Copy After:=ThisWorkbook.Sheets(1)
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
我上网的代码不符合要求。
答案 0 :(得分:0)
代码下方将复制WorkBook1的整张Sheet1,并在Sheet1
之后放置到WorkBook2Sub CopySheet_From_WB1_To_WB2()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Application.Workbooks.Open("C:\Users\...\Desktop\1.xlsx")
Set ws1 = wb1.Sheets("sheet1") 'replece 'Sheet1' with your sheet name
Set wb2 = Application.Workbooks.Open("C:\Users\...\Desktop\2.xlsx")
'Copying WorkBook1's Sheet1 to Workbook2 after Sheet1
ws1.Copy After:=wb2.Sheets("Sheet1") 'replece 'Sheet1' with your sheet name
wb1.Save
wb1.Close
wb2.Save
wb2.Close
End Sub
代码下方将复制Workbook1的Sheet1中使用过的单元格值,并在最后一次使用Workbook2的Sheet1行之后粘贴
Sub CopySheet1Data_to_Sheet2()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb1 = Application.Workbooks.Open("C:\Users\...\Desktop\1.xlsx")
Set ws1 = wb1.Sheets("sheet1") 'replece 'Sheet1' with your sheet name
ws1.Range(Cells(1, 1), ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Set wb2 = Application.Workbooks.Open("C:\Users\..\Desktop\2.xlsx")
Set ws2 = wb2.Sheets("Sheet1")
ws2.Activate
lastUsedRow = ActiveCell.SpecialCells(xlLastCell).Row
ws2.Cells(lastUsedRow + 1, 1).Select
ActiveSheet.Paste
wb1.Save
wb1.Close
wb2.Save
wb2.Close
End Sub