簡體   English   中英

復制並粘貼到新工作表的最后一行

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

我目前正在嘗試使用VBA從一個工作表中復制並粘貼到新工作表中。 但是我有以下變量

  • 帶有要復制數據的工作表是動態的。 列保持不變,但行每周更改。
  • 數據將添加到的新工作表是動態的。 列是相同的,但是新的工作表行每周增加(例如,第700行一個星期,下一個星期800行)。
  • 被復制和粘貼的列(A和BC)是一致的。
  • 數據應粘貼在新工作表的下一個可用行的A-BC列上

我目前設法提出了以下代碼,但是我不斷收到錯誤消息,並且不知道我在哪里出錯,因為我是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

感謝所有幫助,謝謝

您可以嘗試以下方法:

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