Bonjour,
Je réalise une base de donnée sur excel pour lequel je souhaiterais ajouter un moteur de recherche.
Ce dernier doit chercher dans la colonne A ou A et B en affichant tout ou partie du mot dans un command bouton.
J'ai trouvé une macro pour faire la recherche dans tout le classeur mais impossible de limiter ma recherche qu'a une voir deux colonnes .
Je vous transmet ci dessous ma macro:
Option Explicit
Sub CommandButton1_Click()
Dim countTot As Long
Dim counter As Long
Dim strSearchString As String
Dim Ws As Object
Dim foundCell As Variant
Dim loopAddr As Variant
Dim returnValue As String
strSearchString = InputBox(Prompt:="Saisir la pathologie à chercher. (mettre *patho recherché*)", Title:="Recherche")
If strSearchString = "" Then Exit Sub
For Each Ws In Worksheets
countTot = countTot + Application.CountIf(Ws.UsedRange, "=" & strSearchString)
Next Ws
If countTot = 0 Then
returnValue = MsgBox(" La valeur " & strSearchString & " n'est pas enregistrée ", vbOKOnly, " Message ")
Else
counter = 0
For Each Ws In Worksheets
With Ws
.Activate
Set foundCell = .Cells.Find(What:=strSearchString, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
loopAddr = foundCell.Address
Do
counter = counter + 1
foundCell.Activate
If countTot = 1 Then
returnValue = MsgBox(" La valeur " & strSearchString & " est enregistrée 1 seule fois ", vbOKOnly, " Message ")
Exit Sub
End If
If counter = countTot Then
returnValue = MsgBox(" La valeur " & strSearchString & " sélectionnée est la dernière !", vbOKOnly, "Message")
Exit Sub
Else
returnValue = MsgBox(" La valeur " & strSearchString & " sélectionnée est la " & counter & " sur " & countTot & " existantes. " & vbLf & _
" Voulez vous continuer la recherche ? ", vbYesNo, "Message")
If returnValue = vbNo Then Exit For
Set foundCell = .Cells.FindNext(after:=foundCell)
End If
Loop While Not foundCell Is Nothing And foundCell.Address <> loopAddr
End If
End With
Next Ws
End If
End Sub '
Je réalise une base de donnée sur excel pour lequel je souhaiterais ajouter un moteur de recherche.
Ce dernier doit chercher dans la colonne A ou A et B en affichant tout ou partie du mot dans un command bouton.
J'ai trouvé une macro pour faire la recherche dans tout le classeur mais impossible de limiter ma recherche qu'a une voir deux colonnes .
Je vous transmet ci dessous ma macro:
Option Explicit
Sub CommandButton1_Click()
Dim countTot As Long
Dim counter As Long
Dim strSearchString As String
Dim Ws As Object
Dim foundCell As Variant
Dim loopAddr As Variant
Dim returnValue As String
strSearchString = InputBox(Prompt:="Saisir la pathologie à chercher. (mettre *patho recherché*)", Title:="Recherche")
If strSearchString = "" Then Exit Sub
For Each Ws In Worksheets
countTot = countTot + Application.CountIf(Ws.UsedRange, "=" & strSearchString)
Next Ws
If countTot = 0 Then
returnValue = MsgBox(" La valeur " & strSearchString & " n'est pas enregistrée ", vbOKOnly, " Message ")
Else
counter = 0
For Each Ws In Worksheets
With Ws
.Activate
Set foundCell = .Cells.Find(What:=strSearchString, LookIn:=xlValues, LookAt:=xlPart)
If Not foundCell Is Nothing Then
loopAddr = foundCell.Address
Do
counter = counter + 1
foundCell.Activate
If countTot = 1 Then
returnValue = MsgBox(" La valeur " & strSearchString & " est enregistrée 1 seule fois ", vbOKOnly, " Message ")
Exit Sub
End If
If counter = countTot Then
returnValue = MsgBox(" La valeur " & strSearchString & " sélectionnée est la dernière !", vbOKOnly, "Message")
Exit Sub
Else
returnValue = MsgBox(" La valeur " & strSearchString & " sélectionnée est la " & counter & " sur " & countTot & " existantes. " & vbLf & _
" Voulez vous continuer la recherche ? ", vbYesNo, "Message")
If returnValue = vbNo Then Exit For
Set foundCell = .Cells.FindNext(after:=foundCell)
End If
Loop While Not foundCell Is Nothing And foundCell.Address <> loopAddr
End If
End With
Next Ws
End If
End Sub '