PDA

View Full Version : Combobox dependencies



pominoz
05-16-2013, 09:19 PM
Hi there,

Can anyone tell me if it is possible to have the contents of a combobox dependent upon the selection in another combobox?

For example, if I have combobox1 contains

A
B
C
D

And Combobox2 contains

1
2
3
4
5
6
7
8

Then if the selection in combobox 1 was A, combobox2 would only allow me to select 1 or 2 for example? and would not show the additional options.

If this possible does anyone have any code to get me started please

Thanks

Pominoz

antlee
05-17-2013, 03:27 AM
You could use the change event of combobox 1 to populate combobox 2.

So in this example on changing the first combobox its change event clears combobox 2 and then depending on the value of 1 adds new items to combobox 2.

This following code is within the form with two comboboxes, obviously ComboBox1 and ComboxBox2


Private Sub UserForm_Initialize()
ComboBox1.AddItem "A"
ComboBox1.AddItem "B"
ComboBox1.AddItem "C"
ComboBox1.AddItem "D"
End Sub

Private Sub ComboBox1_Change()

ComboBox2.Clear

Select Case ComboBox1.Value
Case "A":
ComboBox2.AddItem "1"
ComboBox2.AddItem "2"
Case "B"
ComboBox2.AddItem "3"
ComboBox2.AddItem "4"
Case "C"
ComboBox2.AddItem "5"
ComboBox2.AddItem "6"
Case "D"
ComboBox2.AddItem "7"
ComboBox2.AddItem "8"
End Select

End Sub


This might give you a start,

Ant

pominoz
05-17-2013, 05:08 PM
Hi Ant,

Thanks for that I will give it a go today.

Pominoz