在同一文件夹内利用VBA从当前己打开的工作簿中的工作表1的第4行A1:E4导出到另一个工作簿的最后一行的下面A:E
时间: 2024-05-08 17:15:44 浏览: 213
以下是实现此操作的VBA代码:
```vb
Sub ExportDataToAnotherWorkbook()
Dim sourceWorkbook As Workbook
Dim destinationWorkbook As Workbook
Dim sourceWorksheet As Worksheet
Dim destinationWorksheet As Worksheet
Dim lastRow As Long
'获取当前打开的工作簿和工作表
Set sourceWorkbook = ThisWorkbook
Set sourceWorksheet = sourceWorkbook.Worksheets(1)
'创建新的工作簿和工作表
Set destinationWorkbook = Workbooks.Add
Set destinationWorksheet = destinationWorkbook.Worksheets(1)
'获取目标工作表的最后一行
lastRow = destinationWorksheet.Cells(destinationWorksheet.Rows.Count, "A").End(xlUp).Row
'将数据复制到目标工作表的下一行
sourceWorksheet.Range("A4:E4").Copy Destination:=destinationWorksheet.Range("A" & lastRow + 1 & ":E" & lastRow + 4)
'保存并关闭目标工作簿
destinationWorkbook.SaveAs ThisWorkbook.Path & "\目标工作簿.xlsx"
destinationWorkbook.Close
MsgBox "数据已成功导出到目标工作簿中。"
End Sub
```
在此代码中,我们首先获取当前打开的工作簿和工作表,然后创建一个新的工作簿和工作表。接下来,我们获取目标工作表的最后一行,并将数据复制到目标工作表的下一行。最后,我们保存并关闭目标工作簿,并显示一个消息框来提示用户操作已成功完成。
请注意,此代码将目标工作簿保存在与当前工作簿相同的文件夹中,并将其命名为“目标工作簿.xlsx”。您可以根据需要修改此文件名和路径。
阅读全文
相关推荐

















