Option Explicit
Private dic As Object
Private Sub Worksheet_Activate()
Dim a, i As Long, ii As Long, w(), temp, myCols
Const Colref As Long = 3 '<-- Col reference for combo3
Set dic = CreateObject("Scripting.Dictionary")
dic.CompareMode = 1
Application.EnableEvents = False
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
a = [b8].CurrentRegion.Value
For i = 1 To UBound(a, 2)
.Item(a(1, i)) = i
Next
a = Sheets("data").Cells(1).CurrentRegion.Value
myCols = VBA.Array(1, 2, Colref)
For ii = 0 To UBound(myCols)
For i = 2 To UBound(a, 1)
If a(i, myCols(ii)) <> "" Then dic(a(i, myCols(ii))) = Empty
Next
Me.OLEObjects("ComboBox" & ii + 1).Object.List = dic.keys
dic.RemoveAll
Next
For i = 2 To UBound(a, 1)
If Not dic.exists(a(i, 1)) Then
ReDim w(1 To 2)
Set w(1) = _
CreateObject("Scripting.Dictionary")
w(1).CompareMode = 1
ReDim temp(1 To .Count, 1 To 1)
For ii = 1 To UBound(a, 2)
If .exists(a(1, ii)) Then temp(.Item(a(1, ii)), 1) = a(i, ii)
Next
w(2) = temp
dic(a(i, 1)) = w
Else
w = dic(a(i, 1))
temp = w(2)
ReDim Preserve temp(1 To .Count, 1 To UBound(temp, 2) + 1)
For ii = 1 To UBound(a, 2)
If .exists(a(1, ii)) Then temp(.Item(a(1, ii)), UBound(temp, 2)) = a(i, ii)
Next
w(2) = temp
dic(a(i, 1)) = w
End If
If Not dic(a(i, 1))(1).exists(a(i, 2)) Then
ReDim w(1 To 2)
Set w(1) = _
CreateObject("Scripting.Dictionary")
w(1).CompareMode = 1
ReDim temp(1 To .Count, 1 To 1)
For ii = 1 To UBound(a, 2)
If .exists(a(1, ii)) Then temp(.Item(a(1, ii)), 1) = a(i, ii)
Next
w(2) = temp
dic(a(i, 1))(1)(a(i, 2)) = w
Else
w = dic(a(i, 1))(1)(a(i, 2))
temp = w(2)
ReDim Preserve temp(1 To .Count, 1 To UBound(temp, 2) + 1)
For ii = 1 To UBound(a, 2)
If .exists(a(1, ii)) Then temp(.Item(a(1, ii)), UBound(temp, 2)) = a(i, ii)
Next
w(2) = temp
dic(a(i, 1))(1)(a(i, 2)) = w
End If
If Not dic(a(i, 1))(1)(a(i, 2))(1).exists(a(i, Colref)) Then
ReDim temp(1 To .Count, 1 To 1)
Else
temp = dic(a(i, 1))(1)(a(i, 2))(1)(a(i, Colref))
ReDim Preserve temp(1 To UBound(temp, 1), 1 To UBound(temp, 2) + 1)
End If
For ii = 1 To UBound(a, 2)
If .exists(a(1, ii)) Then temp(.Item(a(1, ii)), UBound(temp, 2)) = a(i, ii)
Next
dic(a(i, 1))(1)(a(i, 2))(1)(a(i, Colref)) = temp
Next
End With
Me.ComboBox1.List = dic.keys
Application.EnableEvents = True
End Sub
Private Sub ComboBox1_Change()
Dim w
Application.EnableEvents = False
With Me
.ComboBox2.Clear
.ComboBox3.Clear
[b8].CurrentRegion.Offset(1).ClearContents
If .ComboBox1.ListIndex <> -1 Then
.ComboBox2.List = dic(.ComboBox1.Value)(1).keys
w = dic(.ComboBox1.Value)(2)
[b9].Resize(UBound(w, 2), UBound(w, 1)).Value = Application.Transpose(w)
Else
Run Me.CodeName & ".worksheet_activate"
End If
End With
Application.EnableEvents = True
End Sub
Private Sub ComboBox2_Change()
Dim w
Application.EnableEvents = False
With Me
.ComboBox3.Clear
[b8].CurrentRegion.Offset(1).ClearContents
If .ComboBox2.ListIndex <> -1 Then
.ComboBox3.List = dic(Me.ComboBox1.Value)(1)(Me.ComboBox2.Value)(1).keys
w = dic(.ComboBox1.Value)(1)(.ComboBox2.Value)(2)
[b9].Resize(UBound(w, 2), UBound(w, 1)).Value = _
Application.Transpose(w)
End If
End With
Application.EnableEvents = True
End Sub
Private Sub ComboBox3_Change()
Dim w
Application.EnableEvents = False
With [b8:h8]
.CurrentRegion.Offset(1).ClearContents
If Me.ComboBox3.ListIndex <> -1 Then
w = dic(Me.ComboBox1.Value)(1)(Me.ComboBox2.Value)(1)(Me.ComboBox3.Value)
[b9].Resize(UBound(w, 2), UBound(w, 1)).Value = _
Application.Transpose(w)
End If
End With
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [b8].CurrentRegion.Rows(1)) Is Nothing Then
Run Me.CodeName & ".Worksheet_Activate"
End If
End Sub
Bookmarks