Excel VBA Target.address worksheet.onchange无限循环

时间:2014-05-19 06:53:27

标签: excel excel-vba onchange vba

我有单元格A1,单元格B1和单元格C1,我需要检查用户或VBA在A1中是否更改了值。 Whith out如果Target.Address A1上的语句产生无限循环

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更改了哪些Target?

1 个答案:

答案 0 :(得分: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