如何打开工作簿,复制数据并添加到另一工作簿的最后一行? [英] How can I open a workbook, copy data, and add to last line on another workbook?

查看:109
本文介绍了如何打开工作簿,复制数据并添加到另一工作簿的最后一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从当前工作簿中打开另一个工作簿,然后将数据复制到表列中并粘贴到当前工作簿中表的最后一行?并删除源数据.

Is it possible to open another workbook from the current one and copy the data on the table columns and paste on the last line of the table in the current workbook? And also delete the source data.

推荐答案

适应第5个变量,并尝试以下代码:

Adapt the 5th vars and try this code :

Option Explicit
Sub test()

Application.DisplayAlerts = False
Dim wb As Workbook
Set wb = ThisWorkbook

'*******************************************
'Adapt this vars

Dim path_wbToOpen As String
Dim myRange As String
Dim sheet_opened As String
Dim ws_final As Worksheet
Dim lastRow As Integer

path_wbToOpen = "C:\test\test.xlsx" 'path of workbook where is the data
myRange = "A1:D1" 'Range to cpy
sheet_opened = "sheet_opened" 'name in opened WB where is sheet data
Set ws_final = wb.Sheets("sh_test") 'Sheet in current WB to paste data
lastRow = ws_final.Range("A" & Rows.Count).End(xlUp).Row + 1 ' set the last row (adapt column to check last row)


'*******************************************

'open another workbook from current one
Dim wbToOpen As Workbook
Set wbToOpen = Workbooks.Open(path_wbToOpen)

'Copy Data from opened WB
wbToOpen.Sheets(sheet_opened).Range(myRange).Copy 'Copy the data

'current WB
ws_final.Range("a" & lastRow).PasteSpecial ' past to the last row

'Clear Data and close WB
wbToOpen.Sheets(sheet_opened).Range(myRange).Clear 'Clear Data
wbToOpen.Close 'close WB

Application.DisplayAlerts = True

End Sub

这篇关于如何打开工作簿,复制数据并添加到另一工作簿的最后一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆