簡體   English   中英

如何復制最后一行數據的范圍並粘貼到它下面的行中

[英]How To Copy Range of Last Row of Data and Paste into Row below It

我正在嘗試創建一個宏來獲取工作表上的最后一行數據並將其復制/粘貼到它之前的行中。 我需要它來獲取列 BN 中的數據。 我只能使用下面的代碼為 B 列執行此操作,但我無法弄清楚讓它為 BN 列執行此操作的語法 - 有人可以幫忙嗎?

Sub copylastrow()

Worksheets("Sheet1").Activate

Range("B" & Rows.Count).End(xlUp).Copy
Range("B" & Rows.Count).End(xlUp).Offset(1).PasteSpecial

End Sub

代碼中的一些注釋:

  • 定義源工作表(無需激活)
  • 查找特定列中的最后一行
  • 根據列和最后一行設置源范圍
  • 傳輸值而不復制它們更快

假設:

  • B列是獲取最后一行的引用
  • 你只是在粘貼值

閱讀評論並調整代碼以滿足您的需求。

代碼

Public Sub CopyLastRow()
    
    ' Define the source sheet (no need to activate it)
    Dim sourceSheet As Worksheet
    Set sourceSheet = ThisWorkbook.Worksheets("Sheet1")
    
    ' Find the last row in an specific column
    Dim lastRow As Long
    lastRow = sourceSheet.Range("B" & sourceSheet.Rows.Count).End(xlUp).Row
    
    ' Set the source range according to columns and last row
    Dim sourceRange As Range
    Set sourceRange = sourceSheet.Range("B" & lastRow & ":N" & lastRow)
    
    ' Transfer values without copying them is faster
    sourceRange.Offset(1).Value = sourceRange.Value

End Sub

讓我知道它是否有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM