[英]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
代碼中的一些注釋:
假設:
閱讀評論並調整代碼以滿足您的需求。
代碼
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.