Results 1 to 6 of 6

Thread: RESOLVED[2 Comboboxes]

Hybrid View

  1. #1

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Resolved RESOLVED[2 Comboboxes]

    I have Combobox1 with items 1, 2, and 3 and Combobox2 with items 11, 12, 21, 22, 23, 31, 32, 33 and 34.
    First.
    How can I first define Combobox1 with its items and the put the following condition:
    VB Code:
    1. If Combobox1.Vale="" Then
    2. Combobox2.Enabled= False
    3. Else
    4. Combobox2.Enabled= True
    5. End If
    Second.
    How can I introduce the condition that if Combobo1.Value=2 then the Combobox2 displays only items 21, 22 and 23, and so on for 1 and 3?
    Last edited by Fonty; May 19th, 2006 at 12:41 PM.

  2. #2
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: 2 Comboboxes

    You could use a select case which covers both your requirements.

    VB Code:
    1. Private Sub ComboBox1_Change()
    2.  
    3. Dim cbo1Selection As String
    4.  
    5.     cbo1Selection = Me.ComboBox1.Value
    6.  
    7.     With Me.ComboBox2
    8.         'First remove existing values
    9.         .Clear
    10.         Select Case cbo1Selection
    11.             Case ""
    12.                 'Disable ComboBox2 if selection is null
    13.                 .Enabled = False
    14.             Case "1"
    15.                 'Add the appropriate items for selection 1
    16.                 .Enabled = True
    17.                 .AddItem "11"
    18.                 .AddItem "12"
    19.             Case "2"
    20.                 'Add the appropriate items for selection 2
    21.                 .Enabled = True
    22.                 .AddItem "21"
    23.                 .AddItem "22"
    24.                 .AddItem "23"
    25.                 'Add the appropriate items for selection 3
    26.             Case "3"
    27.                 .Enabled = True
    28.                 .AddItem "31"
    29.                 .AddItem "32"
    30.                 .AddItem "33"
    31.                 .AddItem "34"
    32.             Case Else
    33.                 'Add all items
    34.                 .Enabled = True
    35.                 .AddItem "11"
    36.                 .AddItem "12"
    37.                 .AddItem "21"
    38.                 .AddItem "22"
    39.                 .AddItem "23"
    40.                 .AddItem "31"
    41.                 .AddItem "32"
    42.                 .AddItem "33"
    43.                 .AddItem "34"
    44.             End Select
    45.     End With
    46.    
    47. End Sub

    However, this will not fire until ComboBox1 has been changed, i.e. if the user does not change ComboBox1, ComboBox2 will not be disabled.

    Perhaps a better approach would be to disable ComboBox2 by default so when your document/userform whatever is activated, the ComboBox is disabled and will only be enabled when an appropriate choice is made from ComboBox1.

    Also, if you are going to allow selection of an item from your list only (by setting .matchrequired = true for the comboboxes), then a null entry will not be possible once the ComboBox has been changed once unless you add it as an item in the list.

    Does this help?
    Last edited by New2vba; May 19th, 2006 at 12:25 PM.
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  3. #3

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: 2 Comboboxes

    Thanks a lot.
    What does Me mean?
    Just another question. I introduced the code, however whenever I run the userform, the first combobox displays the three items repeated seven times. How could I avoid this?

  4. #4
    Lively Member New2vba's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    95

    Re: 2 Comboboxes

    The "Me" keyword refers to the current instance where the code is running.

    Regarding your problem with duplicated items in ComboBox1 - the above code does not affect these items, it simply acts based on the choice in CombBox1 so the cause lies elsewhere.

    How are you populating your CombBoxes?
    "Those things we must learn to do, we must learn by doing" (or hope somebody else will take pity and help out )

  5. #5

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: 2 Comboboxes

    I'm sorry. I already found the error.
    Thanks.

  6. #6

    Thread Starter
    Addicted Member Fonty's Avatar
    Join Date
    May 2006
    Location
    New York
    Posts
    173

    Re: RESOLVED[2 Comboboxes]

    I made a mistake. I just forgot to erease a For which I was originally using to fill the comboboxes.
    Regarding comboboxes. Do you know how can I introduce items of a particular field in some pivot table?
    For the option of the first combobox "1" there are two items (11 and 12) for the second combobox and for the items "2" there are three (21, 22 and 23)
    I could use something like this o like the code you showed me:
    VB Code:
    1. For i =1 to 3
    2. cbocombobox1.addItem Range("A" & i).Value
    3. Next i
    while using 2 for "1" and 3 for "2".
    However, suppose I have a pivot table called pivotable.
    When combobox1 has the value 2 I have
    VB Code:
    1. ActiveSheet.PivotTables("pivotable").PivotFields("combobox1").CurrentPage =1
    and the the field will show its 3 items and if I choose value "1" it will show 2 items. So the problem is that the field size change for every particular value of combobox1. Is there some way to specify that I want the combobox to be filled with the whole content of the field, no matter what size it has?

Posting Permissions

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



Click Here to Expand Forum to Full Width