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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
Option Explicit
Option Base 1
Dim Plage As Range, Cell As Range
Dim liste As New Collection
Dim Usf As Object
Dim X As Object
Dim i As Integer
Dim strList As String
Dim listecount As Integer
Dim replace() As Variant
Dim ObjListBox As Object
Dim ObjtextBox As Object
Dim ObjButonBox As Object
Dim j As Integer
Dim k As Integer
Dim listeitem As String
Sub listeUnique()
'Définit la plage de cellules pour la collection
Set Plage = Worksheets("Feuil1").Range(Worksheets("feuil1").Range("A1"), Worksheets("feuil1").Range("A1").End(xlDown))
' Attention si la première est vide il fait toute la colonne
On Error Resume Next
'Boucle sur les cellules de la plage cible
For Each Cell In Plage
'Création d'une collection de données uniques (sans doublons)
liste.Add Cell, CStr(Cell)
Next Cell
On Error GoTo 0
ReDim replace(liste.Count)
For i = 1 To liste.Count
Worksheets("Feuil1").Cells(i, 2) = liste(i)
Next i
strList = "Listedesnoms"
Set X = creationUserForm_Et_listBox_Dynamique(strList)
For i = 1 To liste.Count
X.Controls(strList & "text" & i).text = liste(i)
X.Controls(strList & i).Value = liste(i)
For k = 1 To liste.Count
X.Controls(strList & i).AddItem liste(k)
Next k
Next i
X.Show
For i = 1 To liste.Count
Worksheets("Feuil1").Cells(i, 3) = replace(i)
Next i
ThisWorkbook.VBProject.VBComponents.Remove Usf
Set Usf = Nothing
End Sub
Function creationUserForm_Et_listBox_Dynamique(nomListe As String) As Object
Set Usf = ThisWorkbook.VBProject.VBComponents.Add(3)
With Usf
.Properties("Caption") = "Mon UserForm"
.Properties("Width") = 400
.Properties("Height") = 10 + listecount * 20 + 80
End With
Set ObjButonBox = Usf.Designer.Controls.Add("Forms.CommandButton.1")
With ObjButonBox
.Left = 300: .Top = 30 + (liste.Count) * 20: .Width = 90: .Height = 20
.Name = "remplacer"
.Caption = "remplacer"
End With
For i = 1 To liste.Count
Set ObjListBox = Usf.Designer.Controls.Add("Forms.ComboBox.1")
Set ObjtextBox = Usf.Designer.Controls.Add("Forms.TextBox.1")
With ObjtextBox
.Left = 20: .Top = 10 + (i - 1) * 20: .Width = 180: .Height = 20
.Name = nomListe & "text" & i
End With
With ObjListBox
.Left = 200: .Top = 10 + (i - 1) * 20: .Width = 180: .Height = 20
.Name = nomListe & i
.Object.ColumnCount = 1
.Object.ColumnWidths = 70
End With
With Usf.CodeModule
j = .CountOfLines
.InsertLines j + 1, "Sub " & nomListe & i & "_change()"
.InsertLines j + 2 + liste.Count, "End Sub"
End With
Next i
With Usf.CodeModule
j = .CountOfLines
.InsertLines j + 3, "Sub remplacer_Click()"
For k = 1 To liste.Count
.InsertLines j + 4 + k, "If " & nomListe & k & ".ListIndex <> -1 Then replace(" & k & ")= " & Usf.Name & "." & nomListe & k
Next k
.InsertLines j + liste.Count + 5, "End Sub"
End With
VBA.UserForms.Add (Usf.Name)
Set creationUserForm_Et_listBox_Dynamique = UserForms(UserForms.Count - 1)
End Function |
Partager