2014-05-19 41 views
1

我有單元格A1,單元格B1和單元格C1,我需要檢查用戶或VBA是否在A1中更改了值。蒙山出如果Target.Address A1 statment讓人無限循環Excel VBA Target.address worksheet.onchange無限循環

Private Sub Worksheet_Change(ByVal Target As range) 

If Target.Address = B1 or Target.Address = C1 Then 
    If IsEmpty(Cell("B1")) then 
     Cell("A1").value = "Enter value" 
      If IsEmpty(Cell("C1")) then 
       Cell("A1").value = "" 
      Else 
       Cell("A1").value = "=C1/B1" 
      End IF 
    End IF 
End IF 


If Target.Address = A1 
    IF "changed by user typing" Then 
     Cell("B1").value = "" 
     Cell("C1").value = "" 
    Else 
    End IF 
End IF 
End Sub 

如何確定什麼樣的目標已被用戶不能從VBA改變?

回答

3

我懷疑你想是這樣的:

Private Sub Worksheet_Change(ByVal Target As Range) 

    If Not Intersect(Target, Range("A1")) Is Nothing Then 
     Application.EnableEvents = False 
     Range("B1:C1").Value = "" 
     Application.EnableEvents = True 
    ElseIf Not Intersect(Target, Range("B1:C1")) Is Nothing Then 
     Application.EnableEvents = False 
     If IsEmpty(Range("B1")) Then 
      Range("A1").Value = "Enter value" 
     ElseIf IsEmpty(Range("C1")) Then 
      Range("A1").Value = "" 
     Else 
      Range("A1").Value = "=C1/B1" 
     End If 
     Application.EnableEvents = True 
    End If 

End Sub 
+0

是的,但我需要做的,只有當改變指定單元格使得如果'Target.Address = B1或Target.Address = C1 Then' '如果Target.Address = A1然後' – user3588043

+0

這沒有任何意義 - 目標地址不能同時存在。我發佈的代碼將清除B1:C1,如果用戶更改A1或者如果用戶更改B1或C1,請遵循原始邏輯。請測試它,然後_告訴我它是否不做你所需要的。 – Rory

+0

範圍錯誤(「B1:C1」)。Value =「」 對象的方法值範圍失敗 – user3588043