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
| Private Sub Userform_Initialize()
Dim i As Integer
rg = wsClients.Range("liste_clt").value
With Me.cb_client_existant
.ColumnCount = 8
.ColumnWidths = "150;40;25;150;150;150;150;30"
.ColumnHeads = False
.List = rg
End With
Private Sub cb_client_existant_Change()
Dim sSearch As String
Dim i, n, nbCol As Integer
' Si il n'y a pas de clé de recherche
' la source de données est rechargées
If cb_client_existant.value = "" Then
cb_client_existant.List = wsClients.Range("liste_clt").value
' Lance la recherche lorsqu'au moins 3 caractères ont été saisie
ElseIf Len(cb_client_existant.value) >= 3 And Me.cb_client_existant.ListIndex = -1 And IsError(Application.Match(Me.cb_client_existant, Application.Index(rg, , 1), 0)) Then
' Déclaration du tableau
Dim b()
' Création de la clé de recherche
sSearch = "*" & UCase(cb_client_existant) & "*"
n = 0
' Nombre de colonne à afficher dans la liste déroulante
nbCol = 8
'Boucle de recherche entre l'index le plus petit
' et le plus grand du tableau rg
For i = LBound(rg, 1) To UBound(rg, 1)
' Recherche de tmp dans le tableau rg
' rg(i, 1)
' i => index du tableau
' 1 => numéro de colonne où chercher
If UCase(rg(i, 1)) Like sSearch Then
n = n + 1: ReDim Preserve b(1 To nbCol, 1 To n)
' Pour ajouter une colonne à la liste affichée
' ajouter une valeur à b(1, n)
' doit correspondre au nombre définit dans nbCol
b(1, n) = rg(i, 1): _
b(2, n) = rg(i, 2): _
b(3, n) = rg(i, 3): _
b(4, n) = rg(i, 4): _
b(5, n) = rg(i, 5): _
b(6, n) = rg(i, 6): _
b(7, n) = rg(i, 7): _
b(8, n) = rg(i, 8)
End If
...
Next i
If n > 0 Then
ReDim Preserve b(1 To nbCol, 1 To n + 1)
Me.cb_client_existant.List = Application.Transpose(b)
Me.cb_client_existant.RemoveItem n
End If
Me.cb_client_existant.DropDown
Else
On Error Resume Next
End If
End Sub |
Partager