How to change cell value using Worksheet_change event without triggering a second call

Barranka

I'm working on a simple worksheet, and I need to change some data in the cells depending on the user input; those changes are made using the Worksheet_Change event. However, when I change another cell, the event is triggered again, so it becomes quite a headache (it's kind of a "chicken-and-egg" scenario).

Example:

private sub Worksheet_Change(ByVal Target as Range)
    with target ' Only cells in column C are unlocked and available for edition
        select case .row
            case 4
                if .value = 1 then
                    ActiveSheet.cells(5,3).value = 0
                else
                    ActiveSheet.cells(5,3).value = 1
                end if
            case 5
                if .value = 1 then
                    ActiveSheet.cells(4,3).value = 0
                else
                    ActiveSheet.cells(4,3) = 1
                end
       end select
    end with
end sub

As you can see, changes in row 4 trigger changes in row 5, which may trigger another change in row 4... and it becomes an "infinite call", which eventually crashes excel.

So, the question is: Is there a way to programmatically change the value of a cell without triggering the Worksheet_Change event?

Gary's Student

Disable interrupts while changing cells, then re-enable them when you are done:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim A As Range
    Set A = Range("A1")
    If Intersect(Target, A) Is Nothing Then Exit Sub
    Application.EnableEvents = False
        A.Value = ""
        A.Offset(0, 1).Value = "CLEARED"
    Application.EnableEvents = True
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

Worksheet_change event handler changes a cell; how to stop it triggering itself?

From Dev

How to manually modify a cell that is being edited in a worksheet_change event?

From Dev

Avoid Worksheet_Change event if user changes a certain value

From Dev

change event not triggering for first option value

From Dev

Triggering event "change" on select using bootstrap select plugin, only fires after second click

From Dev

JavaScript change event not triggering when using .trigger()

From Dev

Validate user entry on Worksheet_Change event

From Dev

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

From Dev

Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

From Dev

jquery-toggles setting state without triggering on change event

From Dev

How to fire an event when a value of a cell change in a RadGridView?

From Dev

How to execute Macro code inside a module from a worksheet_change event

From Dev

Change event triggering on momentjs object

From Dev

"onclick" event is not triggering on image to change it

From Dev

Cell Value Change Event and Running a Continuous Macro

From Dev

Triggering a function call on state change

From Dev

Exclude Cell Range from Worksheet_Change function

From Dev

Combining Select Case as Cell and Range in Worksheet_Change

From Dev

Multiple worksheet_change with same macro with different reference cell

From Dev

How to call a change event on a condition

From Dev

MS Excel worksheet change event - keeping record of old cell value against new value

From Dev

Change SelectedIndex without triggering SelectedIndexChanged?

From Dev

Excel hyperlink follow macro after worksheet_change event

From Dev

Excel hyperlink follow macro after worksheet_change event

From Dev

How to call render method of a cell view from the editor view upon change event?

From Dev

How do I change the value in an Excel table cell without specifying a cell reference in VBA

From Dev

How do I change the value in an Excel table cell without specifying a cell reference in VBA

From Dev

Event triggered by a cell change

Related Related

  1. 1

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  2. 2

    Worksheet_change event handler changes a cell; how to stop it triggering itself?

  3. 3

    How to manually modify a cell that is being edited in a worksheet_change event?

  4. 4

    Avoid Worksheet_Change event if user changes a certain value

  5. 5

    change event not triggering for first option value

  6. 6

    Triggering event "change" on select using bootstrap select plugin, only fires after second click

  7. 7

    JavaScript change event not triggering when using .trigger()

  8. 8

    Validate user entry on Worksheet_Change event

  9. 9

    Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

  10. 10

    Excel-VBA - How to identify Target range (more than 1 cell) is deleted in a Worksheet_Change function?

  11. 11

    jquery-toggles setting state without triggering on change event

  12. 12

    How to fire an event when a value of a cell change in a RadGridView?

  13. 13

    How to execute Macro code inside a module from a worksheet_change event

  14. 14

    Change event triggering on momentjs object

  15. 15

    "onclick" event is not triggering on image to change it

  16. 16

    Cell Value Change Event and Running a Continuous Macro

  17. 17

    Triggering a function call on state change

  18. 18

    Exclude Cell Range from Worksheet_Change function

  19. 19

    Combining Select Case as Cell and Range in Worksheet_Change

  20. 20

    Multiple worksheet_change with same macro with different reference cell

  21. 21

    How to call a change event on a condition

  22. 22

    MS Excel worksheet change event - keeping record of old cell value against new value

  23. 23

    Change SelectedIndex without triggering SelectedIndexChanged?

  24. 24

    Excel hyperlink follow macro after worksheet_change event

  25. 25

    Excel hyperlink follow macro after worksheet_change event

  26. 26

    How to call render method of a cell view from the editor view upon change event?

  27. 27

    How do I change the value in an Excel table cell without specifying a cell reference in VBA

  28. 28

    How do I change the value in an Excel table cell without specifying a cell reference in VBA

  29. 29

    Event triggered by a cell change

HotTag

Archive