probleme avec la fonction find de VBA

  • Initiateur de la discussion Initiateur de la discussion Armel
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

A

Armel

Guest
Bonjour le Forum,

Voila, le programme ci-dessous recherche le mot que je tape dans la cellule C13, dans une feuille d'excel. Puis il fait clignoter la cellule trouvée.

Sub Recherche()


ActiveSheet.Cells.Find(What:=Worksheets(2).[c13], After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select




mémo1 = ActiveCell.Interior.ColorIndex
mémo2 = ActiveCell.Font.Size
mémo3 = ActiveCell.Font.Color
mémo4 = ActiveCell.Font.Bold
For i = 1 To 1
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True

Next i
ActiveCell.Interior.ColorIndex = mémo1
ActiveCell.Font.Size = mémo2
ActiveCell.Font.Color = mémo3
ActiveCell.Font.Bold = mémo4

End Sub

Le probleme est: si je tape un mot qui n'existe pas dans la feuille il m'ouvre la fenetre de débogage de VB.

Est que quelqu'un à une solution???

Merci à tous.
 
salut 🙂,

il faut juste vérifier si la fonction a trouver quelque chose ou non

Sub Recherche()
dim trouve As Variant

set trouve = ActiveSheet.Cells.Find(What:=Worksheets(2).[c13], After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select



If Not trouve Is Nothing Then

mémo1 = ActiveCell.Interior.ColorIndex
mémo2 = ActiveCell.Font.Size
mémo3 = ActiveCell.Font.Color
mémo4 = ActiveCell.Font.Bold
For i = 1 To 1
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True

Next i
ActiveCell.Interior.ColorIndex = mémo1
ActiveCell.Font.Size = mémo2
ActiveCell.Font.Color = mémo3
ActiveCell.Font.Bold = mémo4

end if

End Sub
 
Bonjour

Voici ton code modifiée

Sub Recherche()


On Error GoTo erreur '<==Ajout pour envoyer au message en cas d'erreur

Cells.Find(What:=Worksheets(2).[c13], After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _
.Select


mémo1 = ActiveCell.Interior.ColorIndex
mémo2 = ActiveCell.Font.Size
mémo3 = ActiveCell.Font.Color
mémo4 = ActiveCell.Font.Bold
For i = 1 To 1
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True
Application.Wait Now + TimeValue("00:00:01")
ActiveCell.Interior.Color = RGB(27, 27, 255)
ActiveCell.Font.Color = RGB(255, 255, 255)
ActiveCell.Font.Bold = True

Next i
ActiveCell.Interior.ColorIndex = mémo1
ActiveCell.Font.Size = mémo2
ActiveCell.Font.Color = mémo3
ActiveCell.Font.Bold = mémo4

Exit Sub '<=== ajouter pour sortir si pas d'erreur

erreur: '<=== ajout pour message d'erreur
MsgBox "La valeur cherchée n'existe pas"

End Sub


Bonne journée

Pascal
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD
Assurez vous de marquer un message comme solution pour une meilleure transparence.

Discussions similaires

Réponses
12
Affichages
1 K
A
Réponses
1
Affichages
921
Munchkin
M
Retour