Microsoft 365 supprimer caractères dans une chaine

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 !

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour à toutes et à tous,
Je vous souhaite une belle journée 🙂

J'avais récupéré ces codes qui fonctionnent très bien.
VB:
Option Explicit

Sub Detecter()
Dim r As Range, interdit$, d As Object, i%, x$
Application.DisplayAlerts = False
On Error Resume Next
Set r = Application.InputBox("Sélectionnez une plage :", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Cells.Interior.ColorIndex = xlNone 'RAZ
Set r = Intersect(r, ActiveSheet.UsedRange)
If r Is Nothing Then Exit Sub
interdit = "ABCDEFGHIJKLMNOPQSTUWVXYZÉÈabcdefghijklmqstwvxyzè1234567890: -"
Set d = CreateObject("Scripting.Dictionary")
For i = 1 To Len(interdit)
    d(Mid(interdit, i, 1)) = ""
Next
For Each r In r
    x = r
    For i = 1 To Len(x)
        If d.exists(Mid(x, i, 1)) Then r.Interior.ColorIndex = 3: Exit For
Next i, r
End Sub

Sub Supprimer()
Dim r As Range, interdit$, d As Object, i%, x$
Application.DisplayAlerts = False
On Error Resume Next
Set r = Application.InputBox("Sélectionnez une plage :", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set r = Intersect(r, ActiveSheet.UsedRange)
If r Is Nothing Then Exit Sub
r.Interior.ColorIndex = xlNone 'RAZ
interdit = "ACDEFGHIKLMNPQSTWVXYZÉÈacdeéfghiklmpqstwvxyzè1234567890: -"
Set d = CreateObject("Scripting.Dictionary")
For i = 1 To Len(interdit)
    d(Mid(interdit, i, 1)) = ""
Next
For Each r In r
    x = r
    For i = Len(x) To 1 Step -1
        If d.exists(Mid(x, i, 1)) Then x = Left(x, i - 1) & Mid(x, i + 1)
    Next i
    r = x
Next r
End Sub
Public Function Nb_Occurence(strInput As String, strFind As String) As Double
If strFind <> "" Then
    Nb_Occurence = (Len(strInput) - Len(Replace(strInput, strFind, ""))) / Len(strFind)
End If
End Function
je remercie encore son auteur 🙂

Dans l'exemple fichier joint, le code me laisse bien mon "Bonjour,"
Mais il me laisse aussi toutes les lettes identiques (contenues dans mon Bonjour,) "u,on,ouon,
u,onouonbnn, our urournbrn. nornrrn, uurrouonn,
"

je n'arrive à trouver à coder pour ne laisser que le "Bonjour,".

Auriez-vous le bon code ?
Fichier test joint.
Je vous remercie,
Amicalement,
lionel,
 

Pièces jointes

Strictement la même chose en VBA :
VB:
Sub Supprimer2()
Dim r As Range, interdit$, d As Object, i%, x$
Application.DisplayAlerts = False
On Error Resume Next
Set r = Application.InputBox("Sélectionnez une plage :", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set r = Intersect(r, ActiveSheet.UsedRange)
If r Is Nothing Then Exit Sub
r.Interior.ColorIndex = xlNone 'RAZ
If Application.CountIf(r, "*Bonjour*") > 0 Then r = "Bonjour,"
End Sub
 

Pièces jointes

Re Syvanu,
Merci et ça fonctionne super bien 🙂

On pourrait aller plus loin ? :
Dans les cellules, il pourra y avoir plusieurs "Bonjour," voir 3-4-5 ou plus.
Il faudrait qu'il me garde les "Bonjour," autant de fois qu'ils sont dans la cellule.
Je joins ton fichier retourné dans lequel j'ai modifié le contenu des cellules,
Merci à toi 🙂
 

Pièces jointes

Dernière édition:
Re Lionel,
Un essai en PJ avec :
VB:
Sub Supprimer()
Dim r As Range, interdit$, d As Object, i%, x$, tablo, Chaine$
Application.DisplayAlerts = False: On Error Resume Next
Set r = Application.InputBox("Sélectionnez une plage :", Type:=8)
On Error GoTo 0
If r Is Nothing Then Exit Sub
Application.ScreenUpdating = False
Set r = Intersect(r, ActiveSheet.UsedRange)
If r Is Nothing Then Exit Sub
r.Interior.ColorIndex = xlNone 'RAZ
For Each r In r
    If Application.CountIf(r, "*Bonjour*") > 0 Then
        Chaine = "": tablo = Split(r, " ")
        For i = 0 To UBound(tablo)
            If tablo(i) Like "*Bonjour*" Then Chaine = Chaine & "Bonjour, "
        Next i
        r = Chaine
    End If
Next r
End Sub
 

Pièces jointes

Moi non plus car je suis nul en vba ... juste un bricoleur lol 🙂
Ton code fonctionne super bien. Merci 🙂

J'ose en tenter une dernière.
J'ai, juste pour tester, voulu exécuter le code avec plusieurs mots et là, ça ne fonctionne plus.
Crois-tu que ce soit possible ?
Si trop compliqué, laisses tomber 😉 car je n'en ai pas besoin ... c'est pour savoir, en cas de besoin dans l'avenir.

Merci Sylvanu 🙂
lionel,
 
- 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

Discussions similaires

  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
247
Réponses
3
Affichages
598
Réponses
4
Affichages
360
  • Question Question
Microsoft 365 Export données
Réponses
4
Affichages
498
  • Question Question
XL 2021 VBA excel
Réponses
4
Affichages
76
Retour