更正一个代码会导致另一个代码停止工作

时间:2018-05-11 13:51:04

标签: excel-vba runtime-error vba excel

我正在处理一个文档,其中可单击的单元格在工作表1和3的M列中放置不同的值。在工作表1上,当M列读取完成时,它将从工作表1中剪切并粘贴到工作表2中,当列M读取PARTIAL时保持它将从表1中删除并粘贴到表3中。我遇到了很多问题,但我在这里寻求帮助的问题是,在下面的代码中,移动将起作用,但我得到一个"运行 - 时间错误' 424'需要对象"并且不接受Time作为我的代码行中的对象Target.Offset(,4).Value = Time但是当我在可点击单元格的代码中解决问题时,行将不再剪切和粘贴。

第一个代码是允许行移动但却出错的代码

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDest As Range, rngDest2 As Range, rngDest3 As Range
          If UCase(Target.Value) = "PARTIAL HOLD" Then
            Set rngDest = Sheet3.Range("A5:Q5")
            If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
                Application.EnableEvents = False
                Target.EntireRow.Cut
                rngDest.Insert Shift:=xlDown
                Target.EntireRow.Delete
                Application.EnableEvents = True
            End If
        ElseIf UCase(Target.Value) = "PROGRESSING" Then
            Set rngDest3 = Sheet1.Range("A5:Q5")
            If Not Intersect(Sheet3.Cells(Target.Row, Target.Column), Sheet3.Range("M5:M290")) Is Nothing Then
                Application.EnableEvents = False
                Target.EntireRow.Cut
                rngDest3.Insert Shift:=xlDown
                Target.EntireRow.Delete
                Application.EnableEvents = True
            End If
        ElseIf UCase(Target.Value) = "COMPLETE" Then
            Set rngDest2 = Sheet2.Range("A5:Q5")
            If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
                 Application.EnableEvents = False
                 Target.EntireRow.Cut
                 rngDest2.Insert Shift:=xlDown
                 Target.EntireRow.Delete
                 Application.EnableEvents = True
            End If
        End If
    End Sub


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
       If Target.Column = 11 Then
          Cancel = True
          Target.Offset(, 2).Value = "IN PROGRESS"
          Target.Offset(, 4).Value = Time
       ElseIf Target.Column = 12 Then
          Cancel = True
          Target.Offset(, 1).Value = "COMPLETE"
          Target.Offset(, 4).Value = Time
       ElseIf Target.Column = 14 Then
          Cancel = True
          Target.Offset(, -1).Value = "PARTIAL HOLD"
       End If
    End Sub

下一个代码是我对可点击单元格进行的修正,但这会阻止行切割和粘贴

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngDest As Range, rngDest2 As Range, rngDest3 As Range
         If UCase(Target.Value) = "PARTIAL HOLD" Then
            Set rngDest = Sheet3.Range("A5:Q5")
            If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
                Application.EnableEvents = False
                Target.EntireRow.Cut
                rngDest.Insert Shift:=xlDown
                Target.EntireRow.Delete
                Application.EnableEvents = True
            End If
        ElseIf UCase(Target.Value) = "PROGRESSING" Then
            Set rngDest3 = Sheet1.Range("A5:Q5")
            If Not Intersect(Sheet3.Cells(Target.Row, Target.Column), 
      Sheet3.Range("M5:M290")) Is Nothing Then
                Application.EnableEvents = False
                Target.EntireRow.Cut
                rngDest3.Insert Shift:=xlDown
                Target.EntireRow.Delete
                Application.EnableEvents = True
            End If
        ElseIf UCase(Target.Value) = "COMPLETE" Then
            Set rngDest2 = Sheet2.Range("A5:Q5")
            If Not Intersect(Target, Sheet1.Range("M5:M290")) Is Nothing Then
                 Application.EnableEvents = False
                 Target.EntireRow.Cut
                 rngDest2.Insert Shift:=xlDown
                 Target.EntireRow.Delete
                 Application.EnableEvents = True
           End If
        End If
    End Sub


    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Application.EnableEvents = False
    On Error GoTo Xit:
       If Target.Column = 11 Then
          Cancel = True
          Target.Offset(, 2).Value = "IN PROGRESS"
          Target.Offset(, 4).Value = Time
       ElseIf Target.Column = 12 Then
          Cancel = True
          Target.Offset(, 1).Value = "COMPLETE"
          Target.Offset(, 4).Value = Time
       ElseIf Target.Column = 14 Then
          Cancel = True
          Target.Offset(, -1).Value = "PARTIAL HOLD"
       End If
    Xit:
    Application.EnableEvents = True
    End Sub

我该怎么做才能解决这个问题?

0 个答案:

没有答案