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

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

在这一行上得到错误:

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 

在你的代码中有一个“恶魔的触摸”,因为如果用户在放置这个事件处理程序的模块的工作表的“J”列中input“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 

它是否工作,如果你改变这个问题行:

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