2016-04-06 121 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) 
相關問題