[英]Update data from copy-paste in excel is located on bellow the old data. How can I change the location?
我在 excel 宏和這樣的代碼上遇到了一些問題:
Sub Backup_button1()
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
'set variable for copy and destination sheets
Set wsCopy = Workbooks("Form Input SAP.xlsm").Worksheets("7-9")
Set wsDest = Workbooks("File Backup.xlsx").Worksheets("7-9")
'1. Find last used row in the copy range based on data in column
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "H").End(xlUp).Row
'2. Find first blank row in the destination range based on data in colom Offset property move down 1 row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "H").End(xlUp).Offset(1).Row
'3. Copy & Paste Data
wsCopy.Range("H4:N" & lCopyLastRow).Copy
wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
End Sub
這些代碼用於復制excel個工作簿中的一些數據並粘貼到不同的工作簿中。 如果目標文件中有一些舊數據,它將在舊數據下方進行更新。 代碼工作正常,但我想更改更新位置,而不是在舊數據下方,但它將位於舊數據之后的單元格中。 有什么建議嗎?
更改您的代碼:
Const DestRow = 4
'2. Find first blank column in the destination range
lDestLastCol = wsDest.Cells(DestRow, wsDest.Columns.Count).End(xlToLeft).Column
'3. Copy & Paste Data
wsCopy.Range("H4:N" & lCopyLastRow).Copy
wsDest.cells(DestRow, lDestLastCol+1).PasteSpecial Paste:=xlPasteValues
第 2 步將為您提供正在使用的最后一列,第 3 步會將數據復制到下一個空閑列中。 我假設在目標工作表中您的數據也從第 4 行開始,否則您必須更改 const 定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.