excel vba 2 combobox's cascading

bahiapt

New Member
Joined
Aug 26, 2010
Messages
27
Hi all,

I have searched through the forum but couldn't find anything that would really do, i am trying to populate a combobox (first) with categorie data and after would like after a selection on this combobox the second one would give unique results for that selection which are customer type.

The raw data has repeated data (categorie) and columns are side by side for sources of the combobox.

The search results i have been finding don't suit me because they are normally for userforms or drop down lists.

Appreciate if you guys take in consideration that i am a complete noob in vba, need this for work :) and that's it, let me know if you need other information.

Best Regards,

Daniel
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi all,
i am trying to populate a combobox (first) with categorie data and after would like after a selection on this combobox the second one would give unique results for that selection which are customer type.

From what I am understanding of your post, you are in need of a way to populate ComboBox2 with data that is dependent on ComboBox1's selected data? If you know what the data is then you can hard-code it in (like the code below). The code listed below can also be modified to utilize an excel range/sheet/ etc.

To run the code please start a new userform with it two ComboBoxes and drop in this code. After that run the userform. You will see that when you select a number with ComboBox1 the same number will be spelled out in ComboBox2.

Code:
Private Sub UserForm_Initialize()
    'using "Private Sub UserForm_Initialize()" will make this code run everytime
    'the userform is opened
    
    
    With ComboBox1  'this adds the item to combobox1
        .AddItem ("1")
        .AddItem ("2")
        .AddItem ("3")
    End With
    
End Sub


Private Sub ComboBox1_Change()
    'this is used for the text change in combobox1
    
    ComboBox2.Clear     'this clears out the combobox incase anything is left over
    
    If ComboBox1.Text = "1" Then
        ComboBox2.AddItem ("One")   'tells combobox2 to add an item named "One"
        ComboBox2.Text = "One"      'tells combobox2 to have "One" as text
    ElseIf ComboBox1.Text = "2" Then
        ComboBox2.AddItem ("Two")
        ComboBox2.Text = "Two"
    ElseIf ComboBox1.Text = "3" Then
        ComboBox2.AddItem ("Three")
        ComboBox2.Text = "Three"
    Else
        ComboBox2.Clear
    End If
    
    Me.Repaint  'refreshes the userform with updated data incase it didnt update properly before
    
End Sub

Hope that helps!
~Frab
 
Last edited:
Upvote 0
Hi Frabulator many thanks for this code, if it wasn't asking too much is there a version of it without using userform?

Best Regards,
Daniel
 
Upvote 0
is there a version of it without using userform?

I personally only use userforms as they tend to be easier on me. However you should be able to use the same code in a module and assign the "ComboBox1_Change()" macro to the combobox on the worksheet.

A better explanation on how to do that can be found here.


---EDIT---
When you copy the code the 'UserForm Initialize' would not be needed as you are not opening up a userform.
 
Last edited:
Upvote 0
Many thanks Frabulator i will try it later in the day, dont have much time now.

Regards,

Daniel
 
Upvote 0
what would be the result of that query the string in combobox 1 or the customer_type (btw is not customer type is customer) data? Also will it bring unique results?

So sorry for not testing but only later can do it but would appreciate if you could take this doubts of my mind.

Best Regards,

Daniel
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,999
Members
448,541
Latest member
iparraguirre89

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