Userform VBA cascading unique combo boxes

cookie81

New Member
Joined
May 25, 2015
Messages
1
Dear all

I have a file with a userform.
there are 6 combo boxes which i need to fill up based on the table sheet "Listuni"
Sector Desc Segment Desc Function DescSupervisor Name NameRank
SECTOR1NetworkNetworkSEGmanger1emp1junior
SECTOR2businessbusinessSEGmanger2emp2NA
SECTOR3BAOBAOSEGmanager3emp3NA
SECTOR4tresortresorSEGmanger60emp4NA
SECTOR5aaaaSEGManager200emp5NA
SECTOR6bbbbSEGmanger1emp6NA
SECTOR7ccccSEGmanger2emp7NA
SECTOR8ddddSEGmanager3emp8NA
SECTOR9ffffSEGmanger60emp9NA
SECTOR10yyyySEGManager200emp10NA
SECTOR1wwwwSEGmanger1emp11NA
SECTOR2eeeeSEGmanger2emp12NA
SECTOR3rrrrSEGmanager3emp13NA
SECTOR4ttttSEGmanger60emp14NA
SECTOR5uuuuSEGManager200emp15directeur
SECTOR6iiiiSEGmanger1emp16directeur
SECTOR7ooooSEGmanger2emp17directeur
SECTOR8ppppSEGmanager3emp18directeur
SECTOR9ssssSEGmanger60emp19directeur
SECTOR10ddddSEGManager200emp20directeur
SECTOR1ffffSEGmanger3emp21directeur
SECTOR2ggggSEGmanger4emp22directeur
SECTOR3hhhhSEGmanager4emp23directeur
SECTOR4jjjjSEGmanger61emp24directeur
SECTOR5kkkkSEGManager201emp25directeur
SECTOR6llllSEGmanger5emp26directeur
SECTOR7zzzzSEGmanger6emp1directeur
SECTOR8xxxxSEGmanager5emp2directeur
SECTOR9vvvvSEGmanger62emp3directeur
SECTOR10bbbbSEGManager202emp1ancien

<tbody>
</tbody><colgroup><col><col span="2"><col><col><col></colgroup>


I need for each combobox to be filled with values depending on the preceding box
for instance if I select sector 1 in combobox CboSector, i should only be able to select ff, Network, ww in combo box CboSeg

i need unique values no repeat values as the list is a couple of thousand rows,
Attached is a copy of my user form and the code that I have inserted. but his code does not remove duplicate and is not able to provide cascade data from one combo to the other. please help!

sector Segment Desc Function DescSupervisor Name NameRank
SECTOR1ffffSEGmanger3emp21directeur
NetworkNetworkSEGmanger1emp1junior
wwwwSEGmanger1emp11NA

<tbody>
</tbody>



" Dim i As Long, j As Long
Dim ws As Worksheet
Set ws = Worksheets("Listuni")

With Data
For j = 1 To 5
For i = 1 To ws.Cells(65536, j).End(xlUp).Row
Select Case j
Case 1
.CboSector.AddItem ws.Cells(i, j)
Case 2
.CboSeg.AddItem ws.Cells(i, j)
Case 3
.CboDep.AddItem ws.Cells(i, j)
Case 4
.CboMan.AddItem ws.Cells(i, j)
Case 5
.CboNam.AddItem ws.Cells(i, j)
Case 6
.CboRank.AddItem ws.Cells(i, j)
End Select
Next
Next
End With



End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try this in Your Userform Module
Data assumed to be on Sheet("Listuni").
Code:
Option Explicit
Private [COLOR="Navy"]Sub[/COLOR] ComboBox1_Change()
Call cValues(ComboBox1.Value, ComboBox2, 2)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] ComboBox2_Change()
Call cValues(ComboBox2.Value, ComboBox3, 3)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] ComboBox3_Change()
Call cValues(ComboBox3.Value, ComboBox4, 4)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] ComboBox4_Change()
Call cValues(ComboBox4.Value, ComboBox5, 5)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] ComboBox5_Change()
Call cValues(ComboBox5.Value, ComboBox6, 6)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]


Private [COLOR="Navy"]Sub[/COLOR] UserForm_Initialize()
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dic         [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]With[/COLOR] Sheets("Listuni")
[COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
 [COLOR="Navy"]End[/COLOR] With
        [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
            Dic.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng: Dic(Dn.Value) = Empty: [COLOR="Navy"]Next[/COLOR]
    Me.ComboBox1.List = Application.Transpose(Dic.keys)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Sub[/COLOR] cValues(txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String,[/COLOR] Obj [COLOR="Navy"]As[/COLOR] Object, col [COLOR="Navy"]As[/COLOR] Integer)
[COLOR="Navy"]Dim[/COLOR] Dn              [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Rng             [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dic             [COLOR="Navy"]As[/COLOR] Object
[COLOR="Navy"]With[/COLOR] Sheets("Listuni")
[COLOR="Navy"]Set[/COLOR] Rng = .Range(.Cells(2, col), .Cells(Rows.Count, col).End(xlUp))
  [COLOR="Navy"]End[/COLOR] With
  [COLOR="Navy"]Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
   [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
        [COLOR="Navy"]If[/COLOR] Dn.Offset(, -1).Value = txt [COLOR="Navy"]Then[/COLOR]
            [COLOR="Navy"]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
                 Dic(Dn.Value) = Empty
            [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
   [COLOR="Navy"]Next[/COLOR] Dn
Obj.List = Application.Transpose(Dic.keys)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,225
Members
448,951
Latest member
jennlynn

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