Consulting

Results 1 to 3 of 3

Thread: Combobox dependencies

  1. #1
    VBAX Regular
    Joined
    May 2012
    Location
    Singapore
    Posts
    19
    Location

    Combobox dependencies

    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

  2. #2
    VBAX Newbie
    Joined
    Dec 2012
    Posts
    4
    Location

    Use the combobox.change event

    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

    [vba]
    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
    [/vba]

    This might give you a start,

    Ant

  3. #3
    VBAX Regular
    Joined
    May 2012
    Location
    Singapore
    Posts
    19
    Location
    Hi Ant,

    Thanks for that I will give it a go today.

    Pominoz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •