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
|
Sub Test()
MsgBox Chercher("Le nom", "Le prénom")
End Sub
Function Chercher(Nom As String, Prenom As String) As String
Dim Plage As Range
Dim Cel As Range
Dim Adr As String
Dim I As Integer
'défini la plage de recherche en colonne "A" (colonne où se trouvent les noms de famille
With Worksheets("Feuil1")
Set Plage = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
End With
'recherche le nom
Set Cel = Plage.Find(Nom, , xlValues, xlWhole)
'si trouvé
If Not Cel Is Nothing Then
'mémorise l'adresse de la 1ère cellule
Adr = Cel.Address
'boucle à la recherche de tous les noms correspondant
Do
'si le prénom correspond, retourne le numéro de ligne et fin
If Cel.Offset(, 1) = Prenom Then
Chercher = "L'enregistrement se trouve à la ligne " & Cel.Row & " !"
Exit Function
'sinon, incrémente le nombre de fois où le nom est trouvé
Else
I = I + 1
End If
'continu la recherche
Set Cel = Plage.FindNext(Cel)
Loop While Adr <> Cel.Address
'si la variable à été incrémentée, le nom a bien été trouvé ma pas avec le prénom
If I > 0 Then
Chercher = "Il y a " & I & " enregistrement(s) au nom de '" & Nom & "' mais aucun avec le prénom '" & Prenom & "' !"
End If
'aucun enregistrement correspondant
Else
Chercher = "Il n'y a pas d'enregistrement du nom de " & Nom & " " & Prenom & " !"
End If
End Function |
Partager