2016-04-06 99 views
0

我对此仍然比较陌生,并试图找到答案。也许它没有正确定义或根本没有定义。也许它不是指向正确的工作表。我不太确定...任何帮助将不胜感激!谢谢!运行时错误'1004':对象'_Global'的方法'相交'失败

获取有关此行错误:

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

+0

使用'Application.Intersect'尝试 – PeterT

回答

2

有一个“魔鬼触摸”在你的代码,因为如果用户键入“关闭”列“J”的片,其模块你把这个事件处理程序,它会删除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 
0

请问如果你改变这个问题行它的工作:

if not intersect(A, Target) is nothing then Set Inte = Intersect(A, Target) 
相关问题