Home » excel » Why is a (Excel VBA) combobox change event triggering every time one of its properties is referenced?

Why is a (Excel VBA) combobox change event triggering every time one of its properties is referenced?

Posted by: admin April 23, 2020 Leave a comment


I’m a first time user on this forum. Here’s my scenario: On a userform, I have a combobox, two textboxes and an ‘OK’ button. When the user makes a makes a selection from the combobox’s dropdown list, the combobox’s change event is triggered and the event handling code fills in the textboxes with info from a worksheet based on the user selection. The user can then edit the information in one or both of the textboxes. The user then hits ‘OK’. The OK button’s click event then writes the modified info from the textboxes back to the cells in the worksheet. Appears to be fairly straight forward. Here’s my problem: the combobox’s change event appears to trigger every time its properties are referenced. Specifically, the three instances in the cb_CustomersUpdateOK_Click() subroutine below where the ListIndex property of the combobox is referenced. I placed a Msgbox in the Change event code to indicate when the event triggered. Using breakpoints at each of the three assign statements in the OK click event code, the combobox triggered (Msgbox displayed) at each of the three statements. When the trigger occurs, it overwrites the edited info in the textboxes with the initial data from the combobox selection. (1) Why is the combobox change event triggering the way it is? (2) What should I be doing to prevent this from happening?

I have been researching this for the past several hours and I haven’t found anything terribly useful. Any help would be greatly appreciated. Please let me know if more info is needed.

Private Sub combo_CustomersUpdateLastName_Change()

    MsgBox "combobox changed"       'For debug purposes

    With Sheets("Customers")
        tb_CustomersUpdateFirstName.Value = .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 2).Value
        tb_CustomersUpdatePhone.Value = .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 3).Value
    End With

End Sub


Private Sub cb_CustomersUpdateOK_Click()

    'Copy the updated customer data from the controls to the Customers sheet
    With Sheets("Customers")
        .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 1).Value = combo_CustomersUpdateLastName.Value
        .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 2).Value = tb_CustomersUpdateFirstName.Value
        .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 3).Value = tb_CustomersUpdatePhone.Value

        'Sort the customer data in case the last name or first name was updated
        Range("CustomerInfo").Sort Key1:=.Columns("A"), Key2:=.Columns("B")
    End With

    MsgBox "Customer data updated."

    Unload form_CustomersUpdate

End Sub
How to&Answers:

What is the source for the combobox items? If it is linked to a range then the change event is triggered because writing to the worksheet changes the linked source which in turn means the combobox refreshes itself.

If you do not want this to happen then you will need to ‘cache’ the values from the range into an array and populate the combox with them.