我希望在按下按钮时将选定的数据行移动到新工作表。我有以下代码工作,但它覆盖目标表中最后一行上方的现有行,我希望它在目标表中插入一个新行。提前致谢
Sub move_row()
'Declare variables
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Dim lastRow As Long
'Set variables
Set sht1 = Sheets("Incremental Opps")
Set sht2 = Sheets("Forecast Data")
'Select Entire Row
Selection.EntireRow.Select
'Move row to destination sheet & Delete source row
lastRow = sht2.Range("A" & sht2.Rows.Count).End(xlUp).Row
With Selection
.Copy Destination:=sht2.Range("A" & lastRow - 1)
.EntireRow.Delete
End With
End Sub
答案 0 :(得分:0)
只需将此行更改为以下内容:
.Copy Destination:=sht2.Range("A" & lastRow + 1)
答案 1 :(得分:0)
在没有新行的情况下尝试此操作
Sub move_row()
ActiveCell.EntireRow.Copy Worksheets("Forecast Data").Range("A" & Worksheets("Forecast Data").Range("A" & Rows.Count).End(xlUp).Row - 1)
ActiveCell.EntireRow.Delete
End Sub
使用新行(插入)
Sub move_row()
Worksheets("Forecast Data").Range("A" & Worksheets("Forecast Data").Range("A" & Rows.Count).End(xlUp).Row).Insert Shift:=xlDown
ActiveCell.EntireRow.Copy Worksheets("Forecast Data").Range("A" & Worksheets("Forecast Data").Range("A" & Rows.Count).End(xlUp).Row - 1)
ActiveCell.EntireRow.Delete
End Sub