Select a value in a combobox, remove same value in another combobox

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
HI ALL,

Let me explain the challenge i'm facing now.
I have 4 comboboxes on a userform. Each has the same set of values assigned to it when the form starts up.
Now...
Let's say the list of values are
1
2
3
4
5

When i select 1 in combobox 1, this should happen
list in combobox 1 - 1,2,3,4,5
list in combobox 2,3 and 4 - 2,3,4,5

Following from that, let's say i select 2 in combobox 2, this should happen
list in combobox 1 - 1,3,4,5
list in combobox 2 - 2,3,4,5
list in combobox 3 and 4 - 3,4,5

And then selecting 3 in combobox 3,
list in combobox 1 - 1,4,5
list in combobox 2 - 2,4,5
list in combobox 3 - 3,4,5
list in combobox 4 - 4,5

and so on.

Right now. I'm racking my brain over this and i can't seem to get the right solution. Feed me with ideas please!! :confused:

using xl2003

Thanks,
Shie Boon
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Here's one way it could be done...

Code:
Private Sub UserForm_Initialize()
    Dim i As Integer
    For i = 1 To 4
        Me.Controls("ComboBox" & i).List = Array("Apple", "Orange", "Pear", "Grape", "Strawberry")
    Next i
End Sub

Private Sub ComboBox1_Change()
    Updater ComboBox1
End Sub

Private Sub ComboBox2_Change()
    Updater ComboBox2
End Sub

Private Sub ComboBox3_Change()
    Updater ComboBox3
End Sub

Private Sub ComboBox4_Change()
    Updater ComboBox4
End Sub

Private Sub Updater(cb As ComboBox)

    Dim i As Integer, ii As Integer, cbox As ComboBox
    
    For i = 1 To 4
        If cb.Name <> "ComboBox" & i Then
            Set cbox = Me.Controls("ComboBox" & i)
            For ii = 0 To cbox.ListCount - 1
                If cbox.List(ii) = cb.Value Then
                    cbox.RemoveItem ii
                    Exit For
                End If
            Next ii
        End If
    Next i

End Sub
 
Upvote 0
Here's one way it could be done...

Code:
Private Sub UserForm_Initialize()
    Dim i As Integer
    For i = 1 To 4
        Me.Controls("ComboBox" & i).List = Array("Apple", "Orange", "Pear", "Grape", "Strawberry")
    Next i
End Sub
 
Private Sub ComboBox1_Change()
    Updater ComboBox1
End Sub
 
Private Sub ComboBox2_Change()
    Updater ComboBox2
End Sub
 
Private Sub ComboBox3_Change()
    Updater ComboBox3
End Sub
 
Private Sub ComboBox4_Change()
    Updater ComboBox4
End Sub
 
Private Sub Updater(cb As ComboBox)
 
    Dim i As Integer, ii As Integer, cbox As ComboBox
 
    For i = 1 To 4
        If cb.Name <> "ComboBox" & i Then
            Set cbox = Me.Controls("ComboBox" & i)
            For ii = 0 To cbox.ListCount - 1
                If cbox.List(ii) = cb.Value Then
                    cbox.RemoveItem ii
                    Exit For
                End If
            Next ii
        End If
    Next i
 
End Sub

Hi, is it possible for you to create a sample excel file using your code and share the link here? Will be very helpful as I encountered a similar problem but I have difficulty solving the problem with just the codes.

Thanks in advance.
 
Upvote 0
Create a userform with four comboboxes named ComboBox1, ComboBox2, ComboBox3, and ComboBox4.

Paste the code in the userform's module. That's it.
 
Upvote 0
It works for userform. However, I will need this to work on a worksheet and that is where I am encountering problems. Any advice?
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,551
Members
452,927
Latest member
rows and columns

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top