【问题标题】:Copy & paste inadvertently triggers Worksheet_Change sub复制和粘贴无意中触发 Worksheet_Change sub
【发布时间】:2018-06-13 21:48:34
【问题描述】:

当列“P”取值“x”时,我遇到了“Worksheet_Change”子问题,该子将整行复制并粘贴到第二个工作表(“已完成”)中。内容如下:

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
    Application.EnableEvents = False
    'If Cell that is edited is in column P and the value is x then
    If Target.Column = 16 And Target.Value = "x" Then
        'Define last row on completed worksheet to know where to place the row of data
        LrowCompleted = Sheets("Completed").Cells(Rows.Count, "A").End(xlUp).Row
        'Copy and paste data
        Range("A" & Target.Row & ":P" & Target.Row).Copy Sheets("Completed").Range("A" & LrowCompleted + 1)
        'Delete Row from Project List
        Range("A" & Target.Row & ":P" & Target.Row).Delete xlShiftUp
    End If
    Application.EnableEvents = True
End Sub

子本身工作正常,但如果我在工作表中的任何位置复制和粘贴,子将被激活,并且我粘贴的行将发送到我的“已完成”工作表。

到目前为止,我一直在玩“if-clause”,但没有任何运气。例如:

    If Not Target.Column = 16 And Target.Value = "x" Is Nothing Then

我担心我错过了显而易见的事情,我很感激任何帮助。

感谢和问候

PMHD

【问题讨论】:

  • 您正在处理 Target 不仅仅是一个单元格的情况。也许在子的开头用if target.cont > 1 then exit sub 包扎它,或者当目标不止一个单元格时透露你想要发生的事情。'
  • 很棒的电话,@Jeeped!这使我找到了解决方案。我最终跳过了“退出子”部分,因此我仍然可以在第 16 列中使用“x”激活子。谢谢。

标签: vba excel excel-2013


【解决方案1】:

如果您关心多个目标,请处理它们;不要丢弃它们。

Private Sub Worksheet_Change(ByVal Target As Range)

  If not intersect(target, range("p:p")) is nothing then
        on error goto meh
        Application.EnableEvents = False
        dim t as range, lrc as long
        lrc = workSheets("Completed").Cells(Rows.Count, "A").End(xlUp).Row + 1
        for each t in intersect(target, range("p:p"))
            if lcase(t.Value2) = "x" Then
                intersect(columns("A:P"), t.rows(t.row)).Copy _
                    destination:=workSheets("Completed").cells(lrc , "A")
                lrc = lrc+1
                'Delete Row from Project List
                intersect(columns("A:P"), t.rows(t.row)).Delete xlShiftUp
             end if
        next t
    End if

meh:
    Application.EnableEvents = true

end sub

【讨论】:

    【解决方案2】:

    谢谢,吉普德。

    问题是由于 Target 引用了多个单元格而出现的。它已通过排除 Target.Count > 1 的情况得到修复。

    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    Application.EnableEvents = False
    'Exclude all cases where more than one cell is Target
    If Target.Count > 1 Then
    
    'If Cell that is edited is in column P and the value is x then
    ElseIf Target.Column = 16 And Target.Value = "x" Then
        'Define last row on completed worksheet to know where to place the row of data
        LrowCompleted = Sheets("Completed").Cells(Rows.Count, "A").End(xlUp).Row
        'Copy and paste data
        Range("A" & Target.Row & ":P" & Target.Row).Copy Sheets("Completed").Range("A" & LrowCompleted + 1)
        'Delete Row from Project List
        Range("A" & Target.Row & ":P" & Target.Row).Delete xlShiftUp
    End If
    Application.EnableEvents = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多