简体   繁体   English

将一个工作表中的一行附加到不同工作簿中的工作表

[英]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 Range to Another Workbook将范围复制到另一个工作簿

  • When you need to copy values, it is more efficient to just write the values to the other worksheet than using 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.

相关问题 将工作表从一个工作簿复制到另一工作簿 - Copy worksheet from one Workbook to another Workbook 试图将一个工作表中的一个工作表复制到另一个预先存在的工作表中? - Trying to copy one worksheet from one workbook into another preexisting worksheet? 将数据从一个工作表复制到另一个工作簿中的指定工作表 - Copying data from one worksheet to specified worksheet in another workbook 将工作表从一个工作簿复制到另一个工作簿 - Copying worksheet from one workbook to another 根据不同工作表的 A 列将一个工作表的整行复制到另一个工作表 - Copy entire row of one worksheet to another based on column A of different worksheet 在工作簿中查找匹配的字符串,从不同的工作表中复制整行并替换原始行 - Excel VBA - Find matching string in workbook, copy entire row from different worksheet and replace original row - Excel VBA 将工作表从活动工作簿复制到其他位置的另一个工作簿 - Copy worksheet from active workbook to another workbook in a different location 将工作表从不同的工作簿复制到此工作簿参考问题 - Copy worksheet from different workbook to this workbook reference problems VBA将数据从一个工作簿复制到另一工作簿/工作表不起作用 - VBA to Copy data from one workbook to another workbook/worksheet not working 将工作表从一个Excel工作簿粘贴到另一工作簿时出错 - Error pasting a worksheet from one excel workbook to another workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM