How do I create COMPLEX indirect data validation lists...

calumbus53

New Member
Joined
Feb 22, 2018
Messages
17
Hi guys, please help...

I am trying to create a series of indirect lists but only lists that link to their relative tree.

If say for example Parent "a" was selected, I would only have access to values directly linked with "a" (such as "a1.1, a1.2, a1.3, a1.4, a1.4", "a2.1, a2.2" and so on) and then on into child 2, repeating the same method. If "b" was selected, I only want to be able to select "b" related children, and then again onto the next level down.

I have managed to do this by creating lots of indirect lists but it is manual and just created over a "drag n drop" array! If the data is updated, I'm screwed!

Do you have any ideas? / Fancy help me doing it?

Below is an example of the format I am working to:

Parent Selection1 Child Selection2 Child SelectionParent1st Child2 Child
10.02.02aa2.1aa2.110a
10.03.03aa3.1aa3.1 a01a1.1
11.02.02bb2.2bb2.1 a a1.201aa1.1
a a1.3aa1.2
a a1.4aa1.3
a a1.5 aa1.4
a
a02a2.1
a a2.202aa2.1
a
a03a3.1
a a3.203aa3.1
a a3.3 aa3.2
a
a04a4.1
a a4.204aa4.1
11b
b01b1.1
b b1.201bb1.1
b
b02b2.1
bb2.202bb2.1
b b2.3 bb2.2
b
b03b3.1
b b3.203bb3.1

<tbody>
</tbody><colgroup><col><col><col span="2"><col><col><col><col><col><col><col></colgroup>
 
Cal
The best option for your piece of Code (index, Match) is to start a new thread. I should include an example of the related Data and your expected results, that way you get the full benefit of the Board.

Regarding my code:-
Q(1) I'm unable to rename the sheet or copy the code across to work in my form.
A(1) If your form has the same controls as Mine (comboboxes 1 to 3 and Text boxes 1 to 2) then you can do a straight copy and paste into your Userform Module.
The code is based on the active sheet so if your data is on another sheet from the Userform sheet you will need to alter the code as below:-[
Code:
Dim nStr As String
[B][COLOR=#b22222]With sheet("Mysheet")
Set Rng = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
end with[/COLOR][/B]
Set Dic = CreateObject("Scripting.Dictionary")

Q(2) I need to add in a 4th dependency combo box.
A(2) Combobox3.values are unique so "Results code" (4th Dependency) column are unique and Pasted in "Results code" Textbox. unless you wanted to include a "Blank" ,Thereby returning "Results code" like "20,01,00" and "20,02,00" ????

Q(3)When deleting one of the dependencies I need the full code box to reset or better still, say for example: (if it was) 11.01.01 and then the final combo box was cleared, (it should say) 11.01.00 Or if one of the dependencies was cleared, it should clear the code.
A(3) When you say "Delete" do you mean actually deleting the value rather than selecting from the Combobox????
I have altered the code (Below) so that any deleting should remove further down the line values.

Paste the whole of this code into your Userform Module. (at the top of code module)
Code:
Option Explicit
[COLOR=navy]Dim[/COLOR] Dic [COLOR=navy]As[/COLOR] Object
[COLOR=navy]Dim[/COLOR] K [COLOR=navy]As[/COLOR] Variant
Private [COLOR=navy]Sub[/COLOR] UserForm_Initialize()
'[COLOR=green][B]Trees/2[/B][/COLOR]
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] Ps [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Dim[/COLOR] Gs [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Dim[/COLOR] temp(1 To 3)
[COLOR=navy]Dim[/COLOR] nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range("A2", Range("A" & Rows.Count).End(xlUp))
[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.Value <> "" [COLOR=navy]Then[/COLOR]
    [COLOR=navy]If[/COLOR] Dn.Offset(, 1) <> "" [COLOR=navy]Then[/COLOR] temp(1) = Dn.Offset(, 1).Value
    [COLOR=navy]If[/COLOR] Dn.Offset(, 3) <> "" [COLOR=navy]Then[/COLOR] temp(2) = Dn.Offset(, 3).Value
    temp(3) = Dn.Offset(, 5).Value
    
    [COLOR=navy]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
            [COLOR=navy]Set[/COLOR] Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
        [COLOR=navy]End[/COLOR] If
            [COLOR=navy]If[/COLOR] Dn.Offset(, 2).Value <> "" [COLOR=navy]Then[/COLOR]
                [COLOR=navy]If[/COLOR] Not Dic(Dn.Value).exists(Dn.Offset(, 2).Value) [COLOR=navy]Then[/COLOR]
                    [COLOR=navy]Set[/COLOR] Dic(Dn.Value)(Dn.Offset(, 2).Value) = CreateObject("Scripting.Dictionary")
                    Dic(Dn.Value)(Dn.Offset(, 2).Value).CompareMode = 1
                [COLOR=navy]End[/COLOR] If
            [COLOR=navy]End[/COLOR] If
              [COLOR=navy]If[/COLOR] Dn.Offset(, 4).Value = "" And Dn.Offset(1, 4).Value <> "" [COLOR=navy]Then[/COLOR]
                    Dic(Dn.Value)(Dn.Offset(, 2).Value)(Dn.Offset(1, 4).Value) = Array(temp(1), temp(2), temp(3), Dn.Offset(1))
              [COLOR=navy]ElseIf[/COLOR] Dn.Offset(, 4).Value <> "" [COLOR=navy]Then[/COLOR]
                    Dic(Dn.Value)(Dn.Offset(, 2).Value)(Dn.Offset(, 4).Value) = Array(temp(1), temp(2), temp(3), Dn)
               [COLOR=navy]End[/COLOR] If
        [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] Dic.keys
    nStr = nStr & IIf(nStr = "", K, "," & K)
[COLOR=navy]Next[/COLOR] K
ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear
TextBox1.Text = ""
TextBox2.Text = ""
ComboBox1.List = Split(nStr, ",")
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Private [COLOR=navy]Sub[/COLOR] ComboBox1_Change()
[COLOR=navy]Dim[/COLOR] P [COLOR=navy]As[/COLOR] Variant, nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] G [COLOR=navy]As[/COLOR] Variant
    [COLOR=navy]If[/COLOR] Dic.exists(ComboBox1.Value) [COLOR=navy]Then[/COLOR]
    [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] P [COLOR=navy]In[/COLOR] Dic(ComboBox1.Value).keys
           nStr = nStr & IIf(nStr = "", P, "," & P)
    [COLOR=navy]Next[/COLOR] P
    [COLOR=navy]With[/COLOR] ComboBox2
        .Clear
        .Value = ""
        .List = Split(nStr, ",")
    [COLOR=navy]End[/COLOR] With
ComboBox3.Clear
[COLOR=navy]Else[/COLOR]
ComboBox2.Value = ""
ComboBox3.Value = ""
TextBox1.Text = ""
TextBox2.Text = ""
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Private [COLOR=navy]Sub[/COLOR] ComboBox2_Change()
[COLOR=navy]Dim[/COLOR] G [COLOR=navy]As[/COLOR] Variant, nStr [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
[COLOR=navy]If[/COLOR] ComboBox1.Value <> "" And ComboBox2.Value <> "" [COLOR=navy]Then[/COLOR]
[COLOR=navy]If[/COLOR] Dic(ComboBox1.Value).exists(ComboBox2.Value) [COLOR=navy]Then[/COLOR]
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] G [COLOR=navy]In[/COLOR] Dic(ComboBox1.Value)(ComboBox2.Value).keys
            nStr = nStr & IIf(nStr = "", G, "," & G)
        [COLOR=navy]Next[/COLOR] G
        
        [COLOR=navy]With[/COLOR] ComboBox3
            .Value = ""
            .Clear
            .List = Split(nStr, ",")
        [COLOR=navy]End[/COLOR] With
[COLOR=navy]Else[/COLOR]
'[COLOR=green][B]ComboBox1.Clear[/B][/COLOR]
'[COLOR=green][B]ComboBox2.Clear[/B][/COLOR]
TextBox1.Text = ""
TextBox2.Text = ""
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Private [COLOR=navy]Sub[/COLOR] ComboBox3_Change()
[COLOR=navy]If[/COLOR] ComboBox1.Value <> "" And ComboBox2.Value <> "" And ComboBox3 <> "" [COLOR=navy]Then[/COLOR]
[COLOR=navy]If[/COLOR] Dic(ComboBox1.Value)(ComboBox2.Value).exists(ComboBox3.Value) [COLOR=navy]Then[/COLOR]
TextBox1.Text = Format(Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(0) & "." & _
                Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(1) & "." & _
                Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(2), "hh.mm.ss")
TextBox2.Text = Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(3).Offset(, 6)
[COLOR=navy]End[/COLOR] If
[COLOR=navy]Else[/COLOR]
TextBox1.Text = ""
TextBox2.Text = ""
[COLOR=navy]End[/COLOR] If
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi Mick,

I am struggling. I have used the code you very kindly helped put together for me. However, I am unable to do the following:

1) Introduce a forth combo box, which is dependant on the 3rd (I have further detail - i.e. Tree1, Tree2, Tree3, Tree4)
2) The Trees have associated numeric values for the result code from the second Tree down to the forth (Tree1 has NO associated number for the result code.
3) If Tree1 was selected we can then select the dependant value from Tree2 and if it's resulting value was "11" then the result box would display "11.00.00" - if we then selected the 1st value in Tree3, we would have "11.01.00" and then in the forth tree (Tree4), we would select the 1st dependency value and see in the results box "11.01.01".

I have tried to edit your code but I am only introducing Errors.

Thank you in advance.
 
Upvote 0
There should be Tree1 data which leads to Tree2 > Tree3 > Tree4
as below:

The amount of data in the table I am working with is enormous. Tree1 data "Body and Safety" (after some rows) eventually becomes "Trim and Mechanisms" and then this has all its dependencies. Tree1 just does not have a associated code which is fine.

Everything you have helped with is great! - I just need to introduce the forth dependency.

Body and SafetySafety23Seatbelts000023.00.00
Body and SafetySafetySeatbelts010023.01.00
Body and SafetySafetySeatbeltsSeat Belt Assembly Front Row0123.01.01
Body and SafetySafetySeatbeltsSeat Belts - Second Row0223.01.02
Body and SafetySafetySeatbeltsSeat Belts - Additional Rows0323.01.03
Body and SafetySafetySeatbeltsChild Tethers0423.01.04
Body and SafetySafetyPassenger & Knee Airbag020023.02.00
Body and SafetySafetyPassenger & Knee AirbagPassenger Airbag0123.02.01
Body and SafetySafetyPassenger & Knee AirbagInflatable Knee Bolster0223.02.02
Body and SafetySafetySide Bags030023.03.00
Body and SafetySafetySide BagsFront Side Air Bag0123.03.01
Body and SafetySafetySide BagsCurtain Airbag System0223.03.02
Body and SafetySafetySide BagsDoor Mounted Inflatable Curtain0323.03.03
Body and SafetySafetySide BagsRear Side Air Bag0423.03.04
Body and SafetySafetySafety Electronics040023.04.00
Body and SafetySafetySafety ElectronicsRestraining Devices, Actuation Sensing0123.04.01
Body and SafetySafetySafety ElectronicsOccupant Classification System0223.04.02
Body and SafetySafetySafety ElectronicsBelt Minder Sensor0323.04.03
Body and SafetySafetySafety ElectronicsOccupants Restraints Controller0423.04.04
Body and SafetySafetySteering Wheel and Drivers Airbag050023.05.00
Body and SafetySafetySteering Wheel and Drivers AirbagSteering Wheel0123.05.01
Body and SafetySafetySteering Wheel and Drivers AirbagDrivers Air Bag0223.05.02
Body and SafetySafetySteering Wheel and Drivers AirbagDrivers Air Bag Bezel0323.05.03
Body and SafetySafetyActive Safety (ADAS)060023.06.00
Body and SafetySafetyActive Safety (ADAS)Front Safety Sensing 0123.06.01
Body and SafetySafetyActive Safety (ADAS)Rear Safety Sensing 0223.06.02
Body and SafetySafetyActive Safety (ADAS)Forward Facing Camera0323.06.03
Body and SafetySafetyActive Safety (ADAS)Central ADAS Controller0423.06.04
Body and SafetySafetyMiscellaneous Safety Components070023.07.00
Body and SafetySafetyMiscellaneous Safety ComponentsDeployable Roll Bar Systems0123.07.01
Body and SafetySafetyMiscellaneous Safety ComponentsPedestrian Protection Deployment0223.07.02
Body and SafetySafetyMiscellaneous Safety ComponentsSide Impact Protection - Door0323.07.03
Trim and MechanismsInterior Trim30000030.00.00
Trim and MechanismsInterior TrimSeating010030.01.00
Trim and MechanismsInterior TrimSeatingFront Seat Frame Assy0130.01.01
Trim and MechanismsInterior TrimSeatingFront Seat Trim0230.01.02
Trim and MechanismsInterior TrimSeatingFront Seat Comfort0330.01.03
Trim and MechanismsInterior TrimSeatingFront Seat Covers0430.01.04
Trim and MechanismsInterior TrimSeatingFront Seat Headrest0530.01.05
Trim and MechanismsInterior TrimSeatingSecond row Seat Frame Assy0630.01.06
Trim and MechanismsInterior TrimSeatingSecond row Seat Trim0730.01.07
Trim and MechanismsInterior TrimSeatingSecond row Seat Comfort0830.01.08
Trim and MechanismsInterior TrimSeatingSecond row Seat Covers0930.01.09
Trim and MechanismsInterior TrimSeatingSecond row Seat Headrest1030.01.10
Trim and MechanismsInterior TrimSeatingThird row Seat Frame Assy1130.01.11
Trim and MechanismsInterior TrimSeatingThird row Seat Trim1230.01.12
Trim and MechanismsInterior TrimSeatingThird row Seat Comfort1330.01.13
Trim and MechanismsInterior TrimSeatingThird row Seat Covers1430.01.14
Trim and MechanismsInterior TrimSeatingThird row Seat Headrest1530.01.15
Trim and MechanismsInterior TrimSeatingSeat ECU and Software1630.01.16
Trim and MechanismsInterior TrimNVH and Heat Protection020030.02.00
Trim and MechanismsInterior TrimNVH and Heat ProtectionTrim & Final Baffles0130.02.01
Trim and MechanismsInterior TrimNVH and Heat ProtectionEncapsulation NVH0230.02.02
Trim and MechanismsInterior TrimNVH and Heat ProtectionNVH Interior0330.02.03
Trim and MechanismsInterior TrimNVH and Heat ProtectionNVH Exterior0430.02.04
Trim and MechanismsInterior TrimNVH and Heat ProtectionFront Wheel Arch Liners0530.02.05
Trim and MechanismsInterior TrimNVH and Heat ProtectionRear Wheel Arch Liners0630.02.06
Trim and MechanismsInterior TrimLower Environment030030.03.00
Trim and MechanismsInterior TrimLower EnvironmentMain Floor Trim0130.03.01
Trim and MechanismsInterior TrimLower EnvironmentSill Trim and Name Plate0230.03.02

<tbody>
</tbody><colgroup><col><col><col><col><col><col><col><col><col></colgroup>

<tbody>
</tbody>
 
Upvote 0
Can I now assume from your latest Table, that the 4 Comboboxes are for "Tree1 to Tree4" and Textbox1 is for the Results "10:01:01" (Ex Cols 4,6 & 8) etc. and Textbox2 is for the "Results code" !!!!
 
Upvote 0
Is your latest table an accurate representation of your data because it now appears to have merged cells, which will need to removed for the code to work. The table also , now has no blank cells in column 3, which need to be accounted for.

It is very important that the data you sent accurately represents the true data !!!
 
Upvote 0
Mick,

Is there a way I can send you a file / screenshot? I am struggling to communicate directly through the forum.

I appreciate this is probably getting frustrating for you.

Cheers,

Cal
 
Upvote 0
Hi Mick,

I am stuck at the bottom of this code... I think I need to do something at the top to edit the .key or .lists?? - I am however not too sure? I have been trying to play with it but I am really struggling.

Code:
Option Explicit
Dim Dic As Object
Dim K As Variant
Private Sub UserForm_Initialize()
'Trees/2
Dim Rng As Range, Dn As Range
Dim c As Long
Dim Ps As String
Dim Gs As String
Dim temp(1 To 4)
Dim nStr As String
With Sheets("Sheet_CPSC")
Set Rng = .Range("A3", .Range("A" & Rows.Count).End(xlUp))
End With
Set Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
    For Each Dn In Rng
    If Dn.Value <> "" Then
    If Dn.Offset(, 1) <> "" Then temp(1) = Dn.Offset(, 1).Value
    If Dn.Offset(, 3) <> "" Then temp(2) = Dn.Offset(, 3).Value
    If Dn.Offset(, 5) <> "" Then temp(3) = Dn.Offset(, 5).Value
    temp(4) = Dn.Offset(, 7).Value
    If Not Dic.exists(Dn.Value) Then
            Set Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
        End If
            If Dn.Offset(, 2).Value <> "" Then
                If Not Dic(Dn.Value).exists(Dn.Offset(, 2).Value) Then
                    Set Dic(Dn.Value)(Dn.Offset(, 2).Value) = CreateObject("Scripting.Dictionary")
                    Dic(Dn.Value)(Dn.Offset(, 2).Value).CompareMode = 1
                End If
            End If
              If Dn.Offset(, 4).Value = "" And Dn.Offset(1, 4).Value <> "" Then
                    Dic(Dn.Value)(Dn.Offset(, 2).Value)(Dn.Offset(1, 4).Value) = Array(temp(1), temp(2), temp(3), temp(4), Dn.Offset(1))
              ElseIf Dn.Offset(, 4).Value <> "" Then
                    Dic(Dn.Value)(Dn.Offset(, 2).Value)(Dn.Offset(, 4).Value) = Array(temp(1), temp(2), temp(3), temp(4), Dn)
               End If
        End If
        Next Dn
For Each K In Dic.keys
    nStr = nStr & IIf(nStr = "", K, "," & K)
Next K
ComboBox1.Clear
ComboBox2.Clear
ComboBox3.Clear
ComboBox4.Clear
txtCPSC.Text = ""
'txtCPSC.Text = ""
ComboBox1.List = Split(nStr, ",")
End Sub
Private Sub ComboBox1_Change()
Dim P As Variant, nStr As String, G As Variant
    If Dic.exists(ComboBox1.Value) Then
    For Each P In Dic(ComboBox1.Value).keys
           nStr = nStr & IIf(nStr = "", P, "," & P)
    Next P
    With ComboBox2
        .Value = ""
        .Clear
        .List = Split(nStr, ",")
    End With
ComboBox3.Clear
Else
ComboBox2.Value = ""
ComboBox3.Value = ""
txtCPSC.Text = ""
'txtCPSC.Text = ""
End If
End Sub
Private Sub ComboBox2_Change()
Dim G As Variant, nStr As String
If ComboBox1.Value <> "" And ComboBox2.Value <> "" Then
        For Each G In Dic(ComboBox1.Value)(ComboBox2.Value).keys
            nStr = nStr & IIf(nStr = "", G, "," & G)
        Next G
        With ComboBox3
            .Value = ""
            .Clear
            .List = Split(nStr, ",")
        End With
Else
'ComboBox1.Clear
'ComboBox2.Clear
txtCPSC.Text = ""
'txtCPSC.Text = ""
End If
End Sub
Private Sub ComboBox3_Change()
Dim H As Variant, nStr As String
If ComboBox1.Value <> "" And ComboBox2.Value <> "" And ComboBox3 <> "" Then
        For Each H In Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value).keys
            nStr = nStr & IIf(nStr = "", H, "," & H)
        Next H
        With ComboBox4
            .Value = ""
            .Clear
            .List = Split(nStr, ",")
        End With
If Dic(ComboBox1.Value)(ComboBox2.Value).exists(ComboBox3.Value) Then
txtCPSC.Text = Format(Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(0) & "." & _
                Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(1) & "." & _
                Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(2), "hh.mm.ss")
'txtCPSC.Text = Dic(ComboBox1.Value)(ComboBox2.Value)(ComboBox3.Value)(3).Offset(, 6)
End If
Else
txtCPSC.Text = ""
'txtCPSC.Text = ""
End If
End Sub
Private Sub ComboBox4_Change()
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,520
Members
449,169
Latest member
mm424

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