mercredi 21 décembre 2016

VBA disable Checkbox click event from firing inside Textbox change event

I want to do something like this. Don't know if it is possible in Excel VBA. On a user form, I have a checkbox and a text box.

When I check the checkbox, the checkbox's caption will be inserted into the text box, when I uncheck the checkbox, the checkbox's caption will be removed from the same text box.

When i write into the text box the caption of the checkbox, inside the text box change event, the checkbox status will be updated. And at the same moment, the checkbox click event is fired and the caption text will be doubled. How can I stop the checkbox click event from firing twice?

Checkbox's click event code:

Private Sub cbSelect_Click()
    With TextBox1
        .Value = IIf(cbSelect.Value, _
        .Value & cbSelect.Caption, _
        Replace(.Value, cbSelect.Caption, vbNullString))
    End With
End Sub

Text box's change event code:

Private Sub TextBox1_Change()
    If InStr(TextBox1.Value, cbSelect.Caption) Then
        cbSelect.Value = 1
    Else
        cbSelect.Value = 0
    End If
End Sub

Any idea?




Aucun commentaire:

Enregistrer un commentaire