Home » excel » excel – Removing items from multiple comboboxes

excel – Removing items from multiple comboboxes

Posted by: admin May 14, 2020 Leave a comment


I have 7 comboboxes. All these comboboxes have same source.

With Sheets("Data_Sheet")

Sheets("UI_Interface").ComboBox2.ListFillRange = "Data_Sheet!E2:E" & .Cells(Rows.Count, 5).End(xlUp).Row

End With

Same code has been written for other combobxes.

Now when a value from combobx1 is selected it should not be present in other comoboxes.
When i try to do this with following code but i’m getting error with this code.

j = ComboBox1.ListIndex

ComboBox2.RemoveItem (j)

I tried some different attributes too for removing the value but all gave some exception.

What is incorrect in this code?

How to&Answers:

The RemoveItem method works properly for me unless I use the .ListFillRange method to populate the combobox. If you use the .List method instead, it should work. To do that, you have to convert the range to an array.


Thanks to enderland for pointing out that you are working with form controls on a worksheet, not in a user form. So the approach should be similar but you won’t be able to use the ListFillRange method. Not a big deal, we can easily take that range, convert it to a variant/array, and use the List method.

Option Explicit
Private Sub Worksheet_Activate()
'## Sets default list for ComboBoxes on this sheet
    SetComboBoxLists ComboBox1
    SetComboBoxLists ComboBox2

End Sub
Private Sub ComboBox1_Change()
'## event handler for a combobox, repeat as needed
    '## Repopulate the list, otherwise you may get
    '    an Index out of Range error or Invalid Argument error,
    '    or the RemoveItem method will remove the wrong item
    SetComboBoxList ComboBox2
    '## Now, remove the item selected in ComboBox1
    ComboBox2.RemoveItem ComboBox1.ListIndex
End Sub

Private Sub SetComboBoxLists(cBox As MSForms.ComboBox)
'## Subroutine to fill the list in each combobox as needed
Dim lstRange As Variant

    '## Populate the array variable to use for the combobox.List method:
    '    double-check that I put the parentheses in the right place!
    With Sheets("Data_Sheet")
        lstRange = .Range("E2:E" & .Cells(Rows.Count, 5).End(xlUp).Row)
    End With

    '## Populate the combobox with the list
    cBox.List = lstRange

End Sub

Note that if any of your code manipulates (e.g., resizes, removes rows, etc) the range, you’ll need to re-apply the List method.