简体   繁体   English

复制并粘贴到新工作表的最后一行

[英]Copy and paste to the last row in a new worksheet

I'm currently trying to copy from one worksheet and paste into a new worksheet using VBA. 我目前正在尝试使用VBA从一个工作表中复制并粘贴到新工作表中。 however i have the following variables 但是我有以下变量

  • The worksheet with data being copied is dynamic. 带有要复制数据的工作表是动态的。 the columns stay the same but the rows change every week. 列保持不变,但行每周更改。
  • The new worksheet which the data will be added to is dynamic. 数据将添加到的新工作表是动态的。 The Columns are the same but the new worksheet rows increase every week( eg row 700 one week row 800 the next). 列是相同的,但是新的工作表行每周增加(例如,第700行一个星期,下一个星期800行)。
  • The columns (A and BC) that are being copied and pasted are consistent. 被复制和粘贴的列(A和BC)是一致的。
  • The data should be pasted on columns A- BC on the next available row of the new worksheet 数据应粘贴在新工作表的下一个可用行的A-BC列上

I've currently managed to come up with the following code but i keep getting error's and don't know where i am going wrong as i am new to VBA. 我目前设法提出了以下代码,但是我不断收到错误消息,并且不知道我在哪里出错,因为我是VBA的新手。

 Sub CandP()
'
'

Dim Last_Row As Long

Application.ScreenUpdating = False

Last_Row = Range("A2:BC2" & Rows.Count).End(xlUp).Row
Range("A2:BC2").Copy
Windows("Newsheet.xlsm").Activate
Range("$A2:BC$" & last_row).FillDown

End Sub

All help is appreciated, Thank you 感谢所有帮助,谢谢

You could try this: 您可以尝试以下方法:

Option Explicit

    Sub CandP()

    Dim Last_Row1 As Long, Last_Row2 As Long
    Dim WB1 As Workbook, WB2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set WB1 = ThisWorkbook ' Workbook where you want to copy the data
    Set ws1 = WB1.Sheets("Sheet1") ' Change the name of your Sheet
    Set WB2 = Workbooks.Open("C:\Desktop\vba\Newsheet.xlsm") ' Enter the address of the Workbook you want to paste the data
    Set ws2 = WB2.Sheets("Sheet1") ' Change the name of your Sheet

    Last_Row1 = ws1.Range("A" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    Last_Row2 = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data

    ws1.Range("A2:BC" & Last_Row1).Copy ws2.Range("A" & Last_Row2)

    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM