此选择无效。确保复制和粘贴区域不重叠

时间:2019-06-14 21:20:22

标签: excel vba

请保持“插入”代码行标题中提到的错误。剪切和插入代码行看起来都一样大小。我已经盯着这个东西好几个小时了。我不知道自己在哪里。

Sub Worksheet_Change(ByVal Target As Range)
'convert communites by status

If Not Intersect(Target, Range("H1:H1000")) Is Nothing Then
If Cells(Target.Row, 8) = "Takedown" Then
Range(Target.EntireRow, Target.Offset(13, 0).EntireRow).Cut
Sheets("AIKEN.AUGUSTA-TAKEDOWN").Range(Range("A12").EntireRow, 
 Range("A25").EntireRow).Insert
 Range("B12:B25").Interior.ColorIndex = 3
 Range("C13").Select

End If
End If

End Sub

预期结果:行范围从工作表的一部分切出,并插入到工作表的不同区域。

实际结果:代码插入行出错。

1 个答案:

答案 0 :(得分:0)

尝试一下:

Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    If Target.CountLarge > 1 Then Exit Sub 
    Set rng = Application.Intersect(Target, Me.Range("H26:H1000"))

    If Not rng Is Nothing Then            
        If Cells(rng.Row, 8) = "Takedown" Then
            Application.EnableEvents = False '<< don't re-trigger on Cut
            Range(rng.EntireRow, rng.Offset(13, 0).EntireRow).Cut
            Me.Range("A12:A25").EntireRow.Insert
            Application.EnableEvents = True  '<< re-enable events
            Me.Range("B12:B25").Interior.ColorIndex = 3
            Me.Range("C13").Select
        End If
    End If

End Sub
相关问题