CHERCHER REMPLACER

Fermé
jeandchanel Messages postés 135 Date d'inscription vendredi 10 juin 2016 Statut Membre Dernière intervention 31 mai 2017 - Modifié le 31 mai 2017 à 18:08
Whismeril Messages postés 18279 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 mars 2023 - 31 mai 2017 à 18:44
Bonjour,
Je me propose d’écrire une macro qui fait la même chose que la fonction CHERCHER/REMPLACER d'excel. J'ai donc écris ce code qui ne marche pas. Au lieu de remplacer le mot "bonbons" par le mot "test" dans la phrase "bonbons rouge tomate" toute la phrase est remplacée par "test". Merci pour votre bonne compréhension.



Sub Recherche_remplace()

Dim derlig, NCI, lig, counter As Integer
Dim plage As Range, O As Worksheet

On Error GoTo suite 'sortie si erreur avec defige ecran
Application.ScreenUpdating = False 'fige ecran
Set O = Worksheets("Feuil10")
derlig = Columns("B").Find("*", , , , xlByRows, xlPrevious).Row 'derniere ligne
Set plage = O.Range("B2:B" & derlig)
NCI = Application.CountIf(plage, "*bonbons*") 'on compte le nombre de "bonbons"


If NCI > 0 Then
lig = 1
For I = 1 To NCI
'on recherche "bonbons"
lig = Columns("B").Find("*bonbons*", O.Cells(lig, 2), , xlWhole).Row

If lig > 0 And Cells(lig, 2) Like "*bonbons*" Then
counter = counter + 1

Cells(lig, 2) = "test"

End If
Next I
End If
MsgBox counter & " Mots trouves !", vbInformation, "Macro_Recherche_Remplace"

suite:
Application.ScreenUpdating = True 'defige ecran

End Sub




1 réponse

Whismeril Messages postés 18279 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 17 mars 2023 888
31 mai 2017 à 18:44
0