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
| Dim Résultats() 'on déclare une variable de type array que l'on ne dimensionne pas encore, tout en haut du code du formulaire
Private Sub CommandButton_cherche_Click()
Dim Cell As Range
Dim NbRésultats As Integer
Dim IndexRésultat As Byte
Dim Recherche As String
Recherche = "*" & TextBox_Nom.Text & "*"
IndexRésultat = 0 'ira jusqu'à NbRésultats
ChoixRésultat.Clear 'ComboBox à ajouter
NbRésultats = Application.WorksheetFunction.CountIf(Columns(2), Recherche) 'on scanne le nombre de résultats que l'on va trouver
If NbRésultats = 0 Then MsgBox "Aucun résultat", vbInformation: Exit Sub 'si on trouve 0, on l'indique et on quitte la macro
ReDim NbRésultats(1 To NbRésultats, 0 To 17) 'on redimensionne le tableau, le 0 to 17 est pour les offsets vers la droite par rapport à la cellule trouvée
For Each Cell In Columns(2).SpecialCells(xlCellTypeConstants).Cells 'on scanne les cellules de la colonne 2 qui sont des constantes (on peut adapter pour les formules également)
If Cell.Text Like Recherche Then 'si on trouve une correspondance
IndexRésultat = IndexRésultat + 1 'on incrémente l'index
ChoixRésultat.AddItem IndexRésultat 'on l'ajoute dans la combobox pour pouvoir le sélectionner plus tard
For i = 0 To 17 'on ajoute tous les offsets que l'on veut utiliser (nom, prénom, conjoint...)
Résultats(IndexRésultat, i) = Cell.Offset(0, i) 'dans la seconde dimension du tableau des Résultats
Next i
End If
End If
End Sub
Private Sub ChoixRésultat_Change() 'ceci serait par exemple une ComboBox
TextBox_Nom.Value = Résultats(ChoixRésultat.Value, 0)
TextBox_Prenom.Value = Résultats(ChoixRésultat.Value, 1)
TextBox_conjoint.Value = Résultats(ChoixRésultat.Value, 2)
TextBox_Adresse.Value = Résultats(ChoixRésultat.Value, 3)
TextBox_Ville.Value = Résultats(ChoixRésultat.Value, 4)
TextBox_Province.Value = Résultats(ChoixRésultat.Value, 6)
TextBox_CP.Value = Résultats(ChoixRésultat.Value, 6)
TextBox_Tel1.Value = Résultats(ChoixRésultat.Value, 7)
TextBox_Tel2.Value = Résultats(ChoixRésultat.Value, 8)
TextBox_Adressecourriel.Value = Résultats(ChoixRésultat.Value, 9)
CheckBox_CHEL.Value = Résultats(ChoixRésultat.Value, 10)
CheckBox_CHJAP.Value = Résultats(ChoixRésultat.Value, 11)
CheckBox_CHT.Value = Résultats(ChoixRésultat.Value, 12)
CheckBox_Rdvmed.Value = Résultats(ChoixRésultat.Value, 16)
CheckBox_Myosotis.Value = Résultats(ChoixRésultat.Value, 13)
CheckBox_Finvie.Value = Résultats(ChoixRésultat.Value, 15)
CheckBox_Pastorale.Value = Résultats(ChoixRésultat.Value, 14)
CheckBox_comres.Value = Résultats(ChoixRésultat.Value, 17)
End Sub |
Partager