复制活动单元格行并直接在下面插入复制的行

时间:2015-09-21 11:29:45

标签: excel vba excel-vba

我正在尝试复制活动单元格行,并希望将复制的行直接插入该行下方。像这样:

Row 1   < User clicks relevant cell to trigger macro (Active Row)
Row 2   < Copied Cell gets inserted directly beneath Active Row

即使我的Active Row下面有一行,即当用户点击复制Active Row时,我希望这种情况发生:

Row 1 < Active Row
Row 2 < Other Content 

会变成这样:

Row 1 < Active Row
Row 2   < Copied Cell gets inserted directly beneath Active Row
Row 3 < Other Content 

这是我的代码,我试图把它放在一起,没有运气:

'Add Tender Type Row
Dim NextRow As Range
Set NextRow = Range("B" & Sheets("Home").ActiveCell.Rows.Count + 1)
If Not Intersect(Target, Range("AF" & ActiveCell.Row)) Is Nothing And Range("AF" & ActiveCell.Row).Value = "+" Then
Sheet1.Range("B" & ActiveCell.Row & ":AF" & ActiveCell.Row).Copy
Sheet1.Activate
NextRow.PasteSpecial Paste:=1, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If

请有人告诉我正确的方法吗?

1 个答案:

答案 0 :(得分:0)

您对NextRow的定义错误,您使用Sheets("Home").ActiveCell.Rows.Count + 1代替ActiveCell.Row + 1

尝试一下:

Sub Mark_Harris()

Dim Ws As Worksheet, _
    NextRow As Range

If Target.Cells.Count > 1 Then Exit Sub
Set Ws = ActiveWorkbook.Sheets("Home")


With Ws
    If Not Intersect(Target, .Range("AF" & ActiveCell.Row)) Is Nothing And .Range("AF" & ActiveCell.Row).Value = "+" Then
        .Rows(ActiveCell.Row + 1).Insert Shift:=xlDown
        Set NextRow = .Range("B" & ActiveCell.Row + 1)
        .Range("B" & ActiveCell.Row & ":AF" & ActiveCell.Row).Copy
        NextRow.PasteSpecial Paste:=1, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    End If
End With
Application.CutCopyMode = False

End Sub