|
-
May 19th, 2006, 10:35 AM
#1
Thread Starter
Addicted Member
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:
If Combobox1.Vale="" Then
Combobox2.Enabled= False
Else
Combobox2.Enabled= True
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.
-
May 19th, 2006, 12:10 PM
#2
Lively Member
Re: 2 Comboboxes
You could use a select case which covers both your requirements.
VB Code:
Private Sub ComboBox1_Change()
Dim cbo1Selection As String
cbo1Selection = Me.ComboBox1.Value
With Me.ComboBox2
'First remove existing values
.Clear
Select Case cbo1Selection
Case ""
'Disable ComboBox2 if selection is null
.Enabled = False
Case "1"
'Add the appropriate items for selection 1
.Enabled = True
.AddItem "11"
.AddItem "12"
Case "2"
'Add the appropriate items for selection 2
.Enabled = True
.AddItem "21"
.AddItem "22"
.AddItem "23"
'Add the appropriate items for selection 3
Case "3"
.Enabled = True
.AddItem "31"
.AddItem "32"
.AddItem "33"
.AddItem "34"
Case Else
'Add all items
.Enabled = True
.AddItem "11"
.AddItem "12"
.AddItem "21"
.AddItem "22"
.AddItem "23"
.AddItem "31"
.AddItem "32"
.AddItem "33"
.AddItem "34"
End Select
End With
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 )
-
May 19th, 2006, 12:29 PM
#3
Thread Starter
Addicted Member
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?
-
May 19th, 2006, 12:41 PM
#4
Thread Starter
Addicted Member
Re: 2 Comboboxes
I'm sorry. I already found the error.
Thanks.
-
May 19th, 2006, 12:49 PM
#5
Lively Member
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 )
-
May 19th, 2006, 01:07 PM
#6
Thread Starter
Addicted Member
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:
For i =1 to 3
cbocombobox1.addItem Range("A" & i).Value
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|