Share via

how to avoid continually triggering a selection change when writing to a cell in the selection

Anonymous
2011-11-18T20:32:19+00:00

I have a rage of cells $C$9:$K$9.  the user can mark only one of these cells with an x.  I have a selection change sub looking at this range and want to clear the contents of the range when the users marks a different cell with an "x" leaving only the newly selected cell with the "x".  Problem is when I clear the contents in vba it triggers the selection change event again and causes an infinite loop.  What is the fastest way to accomplish what I want?

Thanks

Dan

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-11-18T20:38:33+00:00

... Problem is when I clear the contents in vba it triggers the selection change event again and causes an infinite loop ...

Use EnableEvents to halt processing events while you run your code routine and reenable the event trigger before exiting the function.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Not Intersect(Target, Range("A:A")) Is Nothing Then

        Application.EnableEvents = False

        ' your code here

        Application.EnableEvents = True

    End If

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-11-22T14:05:32+00:00

    Thanks.  Totally slipped my mind to use enableEvents.

    Was this answer helpful?

    0 comments No comments