Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

probleme avec la fonction find de VBA

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.
 
A

arnaud

Guest
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
 
P

Pascal76

Guest
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
 
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…