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

时间:2016-04-06 13:47:12

标签: excel vba excel-vba runtime-error

我仍然相当新,并试图找到答案。也许它没有正确定义或根本没有定义。也许它没有指向正确的工作表。我不太确定......任何帮助都将不胜感激!谢谢!

在此行中收到错误:

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

2 个答案:

答案 0 :(得分:2)

您的代码中存在“魔鬼触摸”,因为如果用户在您放置此事件处理程序的模块中的工作表的“J”列中键入“已关闭”,则会删除target行({ {1}}),因此Target.EntireRow.Delete未被引用,并为随后使用target时出现错误做准备,这恰好位于target

但是如果我正确阅读了你的代码,那么这甚至不应该发生,因为后一行只是应该以十字列“C”为目标,如果它在“J”列中就不可能!

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

Set Inte = Intersect(A, Target)

答案 1 :(得分:0)

如果您使用以下方法更改问题行,是否有效:

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