prevent event procedure under condition

  • Thread starter Thread starter short_n_curly
  • Start date Start date
S

short_n_curly

is it possible to stop the selection change event procedure from being
triggered from within a worksheet change event code
i.e i have an event by slection that will begin the start of event by
change but keeps looping when triggered
if the change would stop the selection code once activated it will
prevent any looping

hope this makes sense i would imagine it be something similar to
either
exit private sub worksheet_selectionchange
or
end provate sub worksheet_selectionchange
however neither of these codes work

please help me thanks
 
I am assuming that your selection change event code triggers a selection
change and that is resulting in a recursive call. To fix that you can set
application.enableEvents to ignore the events something like this

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo ErrorHandler
Application.EnableEvents = False

ErrorHandler:
Application.EnableEvents = True
End Sub

As always when switching application settings it is safest to have an error
handler to reset the applicaton in case your code crashes...
 
Just some added information:

Jim, either you mistyped or misread. He said the change event is causing a
selectionchange to fire.

In any event, while the solution is certainly applicable, a better all
around solution might be to avoid making a selection in the change event.
In other words, if the OP is using recorder style code like

Range("A1").Select
Selection.Value = 21
Range("B9").Select

then this should be avoided with just

range("A1").Value = 21

which doesn't trigger a selectionchange.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top