List box setting of initial value triggers a change event

  • Thread starter Thread starter dan
  • Start date Start date
D

dan

Why when I execute this code would a change event be
triggered for the listbox?

Private Sub lstBoxDateChg_Enter()

application.EnableEvents = False

lstBoxDateChg.Value = Range("'Print - View Selection'!
$AA$1").Text

application.EnableEvents = True

End Sub


This is the event that gets triggered:

Private Sub lstBoxDateChg_Change()

How can I load an initial value without this happening?
 
You changed the value of the listbox, so the change event fired.
Application.enableEvents is part of the Excel type library and only pertains
to Excel events. The listbox is part of the Msforms library - so
EnableEvents has no effect on it.

You can use a boolean variable to suppress the change event

At the top of the sheet module

Public bBlockEvents as boolean

Private Sub lstBoxDateChg_Enter()
bBlockEvents = True

lstBoxDateChg.Value = Range("'Print - View Selection'!> $AA$1").Text
bBlockEvents = False

End Sub


Private Sub lstBoxDateChg_Change()
if bBlockEvents then exit sub

' current code

End Sub
 
Or that should have said at the top of the Userform Module since it appears
these are on a userform.
 
Back
Top