我在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工作簿中的某些数据并粘贴到不同的工作簿中。如果目标文件中有一些旧数据,它将更新旧数据。代码工作正常,但我想改变更新位置不是在下面的旧数据,而是它将定位在单元格后的旧数据。有什么建议吗?
发布于 2022-11-26 14:04:27
更改您的代码:
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定义。
https://stackoverflow.com/questions/74570217
复制相似问题