1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| Private Sub UserForm_Initialize()
Me.ComboBox1.List = SansDoublonsTrié(Application.Transpose(Range("A2:A" & [A65000].End(xlUp).Row)))
End Sub
Private Sub ComboBox1_Change()
'Remplissage Combo2
Alim_Combo 2, ComboBox1.Value
End Sub
Private Sub ComboBox2_Change()
'Remplissage Combo3
Alim_Combo 3, ComboBox2.Value
End Sub
Function SansDoublonsTrié(a)
Set d = CreateObject("Scripting.Dictionary")
For Each c In a
d(c) = ""
Next c
b = d.keys
Call Tri(b, LBound(b), UBound(b))
SansDoublonsTrié = Application.Transpose(b)
End Function
Sub Tri(a, gauc, droi) ' Quick sort
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
temp = a(g): a(g) = a(d): a(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call Tri(a, g, droi)
If gauc < d Then Call Tri(a, gauc, d)
End Sub
Private Sub Alim_Combo(CbxIndex As Integer, Optional Cible As Variant)
Dim j As Integer
Dim Obj As Control
Set Obj = Me.Controls("ComboBox" & CbxIndex)
Obj.Clear
If CbxIndex = 1 Then
'Boucle sur les lignes de la colonne A (à partir de la 2eme ligne)
For j = 2 To NbLignes
Obj = Ws.Range("A" & j)
'Remplit le ComboBox sans doublons
If Obj.ListIndex = -1 Then Obj.AddItem Ws.Range("A" & j)
Next j
Else
For j = 2 To NbLignes
If Ws.Range("A" & j).Offset(0, CbxIndex - 2) = Cible Then
Obj = Ws.Range("A" & j).Offset(0, CbxIndex - 1)
If Obj.ListIndex = -1 Then Obj.AddItem Ws.Range("A" & j).Offset(0, CbxIndex - 1)
End If
Next j
End If
'Enlève la sélection dans le ComboBox
Obj.ListIndex = -1
End Sub |