[英]Appending a row from one worksheet to a worksheet in a different workbook
I have a workbook that produces a single row of data for each month.我有一个工作簿,每个月都会生成一行数据。 I want to append that data into a running list of previous months in a different workbook.我想将 append 该数据放入不同工作簿中前几个月的运行列表中。
The code copies the data from the worksheet then pastes the data into the other worksheet.代码从工作表中复制数据,然后将数据粘贴到另一个工作表中。
My problem is it will just paste in the first row.我的问题是它只会粘贴在第一行。 I want to paste at the bottom of any data that's already there.我想粘贴到任何已经存在的数据的底部。
Private Sub CommandButton1_Click()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim shSource As Worksheet
Dim shTarget As Worksheet
Dim LastRow As Long
Set wbSource = ThisWorkbook
Set wbTarget = Workbooks.Open(Filename:="Y:\DEVTEST.xlsx")
Set shSource = wbSource.Worksheets("Append")
shSource.Range("A2:U2").Copy
' Reference to sheet to copy to
Set shTarget = wbTarget.Worksheets("Sheet1")
LastRow = WorksheetFunction.Max(Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)
shTarget.Cells(LastRow, "A").PasteSpecial xlPasteValues
End Sub
Copy/PasteSpecial
.当您需要复制值时,将值写入另一个工作表比使用Copy/PasteSpecial
更有效。Option Explicit
Private Sub CommandButton1_Click()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim shSource As Worksheet
Dim shTarget As Worksheet
Dim rg As Range
Dim cel As Range
Set wbSource = ThisWorkbook
Set shSource = wbSource.Worksheets("Append")
Set rg = shSource.Range("A2:U2")
Set wbTarget = Workbooks.Open(Filename:="Y:\DEVTEST.xlsx")
Set shTarget = wbTarget.Worksheets("Sheet1")
Set cel = shTarget.Cells(shTarget.Rows.Count, "A").End(xlUp).Offset(1)
cel.Resize(, rg.Columns.Count).Value = rg.Value
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.