How to end infinite “change” loop in VBA

前端 未结 3 1034
名媛妹妹
名媛妹妹 2020-12-20 17:57

I have a problem with visual basic. I want to make a macro/function that will multiply a number I enter by 3 and give a result in the same cell. I tried something like this:

3条回答
  •  星月不相逢
    2020-12-20 18:21

    With error handling to ensure .EnableEvents goes back to True:

    Sub Worksheet_Change(ByVal Target As Range)
        On Error GoTo CleanExit
        If Target.Address = "$Q$21" Then
            Application.EnableEvents = False
            Target.Value = Target.Value * 3
        End If
    CleanExit:
        Application.EnableEvents = True
        On Error GoTo 0
    End Sub
    

提交回复
热议问题