运行-时间错误“1004”:对象“_Global”的方法 'Intersect' 失败

Run-time error '1004': Method 'Intersect' of object' _Global' failed

我在这方面还很陌生,正在努力寻找答案。也许它没有正确定义或根本没有定义。也许它没有指向正确的工作 sheet。我不太确定...任何帮助将不胜感激!谢谢!

此行出现错误:

Set Inte = Intersect(A, Target)

错误代码为:

Run-time error '1004': Method 'Intersect' of object'_Global' failed

完整代码:

Private Sub Worksheet_Change(ByVal Target As Range)
'Determine Target Colunm
 If Target.Column = 10 Then
'Check for "TD", Copy/Delete Row if found
  If Target = "TD" Then
    Application.EnableEvents = False
      nxtRow = Sheets("TD Locks").Range("J" & Rows.Count).End(xlUp).Row + 1
      Target.EntireRow.Copy _
        Destination:=Sheets("TD Locks").Range("A" & nxtRow)
      Target.EntireRow.Delete
      Application.EnableEvents = True
       Exit Sub
  End If

'Check for "Closed", Copy/Delete Row if found
  If Target = "Closed" Then
    Application.EnableEvents = False
      nxtRow = Sheets("Closed Locks").Range("J" & Rows.Count).End(xlUp).Row + 1
      Target.EntireRow.Copy _
        Destination:=Sheets("Closed Locks").Range("A" & nxtRow)
      Target.EntireRow.Delete
      Application.EnableEvents = True
  End If
 End If

'Adds date when borrower name is entered
    Dim A As Range, B As Range, Inte As Range, r As Range
    Set A = Range("C:C")
    Set Inte = Intersect(A, Target)
    If Inte Is Nothing Then Exit Sub
    Application.EnableEvents = False
        For Each r In Inte
            If r.Offset(0, 8).Value = "" Then
               r.Offset(0, 8).Value = Date
            End If
        Next r
    Application.EnableEvents = True
End Sub

如果你用这个改变问题行,它是否有效:

if not intersect(A, Target) is nothing then Set Inte = Intersect(A, Target)

您的代码中有一个 "devil touch",因为如果用户在 sheet 的 "J" 列中键入 "Closed",您将在其模块中放置此事件处理程序,它删除了 target 行 (Target.EntireRow.Delete),从而使 target 未被引用,并为在 target 的任何后续使用中抛出错误做好了准备,这恰好在 Set Inte = Intersect(A, Target)

但是如果我正确地阅读了你的代码,这甚至不应该发生,因为后一行完成应该只针对跨列 "C",如果它在列 "J" 中则不可能!.

如果以上内容正确,您可能需要使用如下代码

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
Dim nxtRow As Long
Dim Inte As Range, r As Range

Application.EnableEvents = False

With Target
    'Determine Target Colunm
    If .Column = 10 Then

        'Check for "Closed", Copy/Delete Row if found
        If .Value = "Closed" Then
            nxtRow = Sheets("Closed Locks").Range("J" & Rows.Count).End(xlUp).Row + 1
            .EntireRow.Copy _
            Destination:=Sheets("Closed Locks").Range("A" & nxtRow)
            .EntireRow.Delete

        ElseIf Target = "TD" Then
            'Check for "TD", Copy/Delete Row if found
            nxtRow = Sheets("TD Locks").Range("J" & Rows.Count).End(xlUp).Row + 1
            .EntireRow.Copy _
            Destination:=Sheets("TD Locks").Range("A" & nxtRow)
            .EntireRow.Delete
        End If

    Else

        'Adds date when borrower name is entered
        Set Inte = Intersect(.Cells, .Parent.Range("C:C"))
        If Not Inte Is Nothing Then
            For Each r In Inte
                If r.Offset(0, 8).Value = "" Then r.Offset(0, 8).Value = Date
            Next r
        End If

    End If
End With

Application.EnableEvents = True

End Sub