Repérer et supprimer une valeur dans un commentaire

Bonsoir à toutes et tous,

J'aimerais savoir comment repérer une valeur dans un commentaire pour la supprimer. Exemple :

- La valeur recherché est : "Test1"

- Le commentaire contient : "Test0, Test1, Test2, test3"

J'aimerai qu'à la fin, il ne reste dans le commentaire plus que : "Test0, Test2, test3"

Si cela peut vous aider, j'ai déjà commencé à gratter quelques lignes me permettant d'identifier quelles cellules contiennent des commentaires.

Private Sub CommandButton1_Click()
Dim c, l As Long
Dim Commentaire As Comment
Dim Mavariable As String

    Mavariable = "Test1"

        For c = 5 To 9 'colonne
            For l = 3 To 38 'ligne

                Set Commentaire = ActiveSheet.Cells(l, c).Comment
                    If Not Commentaire Is Nothing Then 'test s'il y a un commentaire dans la cellule
                        'ici, le code pour vérifier si le commentaire contient Mavariable et supprimer cette valeur
                    End If
            Next l
        Next c

End Sub

Function NbComLines(r As Range) As Long
Application.Volatile

    Set c = r.Comment
        If Not c Is Nothing Then
            t = Split(c.Text, vbLf)
        End If

    NbComLines = UBound(t) + 1

End Function

Merci d'avance ;)

J'ai fini par trouver une solution fonctionnel. Je reste cependant à l'écoute de toutes propositions ;)

Private Sub CommandButton1_Click()
Dim c, l As Long
Dim Commentaire As Comment
Dim Mavariable, com, nom, nom1, nom2 As String

    Mavariable = "Test1"

        For c = 5 To 9 'colonne
            For l = 3 To 38 'ligne

                Set Commentaire = ActiveSheet.Cells(l, c).Comment
                    If Not Commentaire Is Nothing Then
                        com = ActiveSheet.Cells(l, c).Comment.Text
                            If com Like "*" & Mavariable & "*" Then
                                nom = Replace(com, Mavariable, " / ")
                                nom1 = Replace(nom, vbLf, "")
                                nom2 = Replace(nom1, " / ", vbLf)
                            End If
                    End If
            Next l
        Next c

End Sub

Bonsoir,

Une solution parmi d'autres, (je ne me suis pas occupé des divers séparateurs)

Private Sub CommandButton1_Click()
    Dim c, l As Long
    Dim Commentaire As String
    Dim Mavariable As String
    Mavariable = "Test1"
    For c = 5 To 9 'colonne
        For l = 3 To 38 'ligne
            On Error Resume Next
            Commentaire = Cells(l, c).Comment.Text
            Cells(l, c).Comment.Text Text:=Cells(l, c).Comment.Text Text:=Replace(Commentaire, Mavariable, "")
            On Error GoTo 0
        Next l
    Next c
End Sub

Cdlt

Bonsoir SOTIN ,

Une autre proposition :

Private Sub CommandButton1_Click()
Const Ref = "Test1"
Dim x, oldtext As String, newtext As String, s, i As Long

   For Each x In Range(Cells(3, 5), Cells(5, 9)).Cells
      If Not x.Comment Is Nothing Then
         If InStr(x.Comment.Text, Ref) > 0 Then
            oldtext = Application.Trim(Application.Clean(x.Comment.Text))
            newtext = "": s = Split(oldtext, ",")
            For i = 0 To UBound(s)
               If Trim(s(i)) <> Ref Then newtext = newtext & ", " & Trim(s(i))
            Next i
            x.Comment.Text Text:=Mid(newtext, 2)
         End If
      End If
   Next x
End Sub

bonjour le filt

@mafraise , je ne connaissais pas ce "application.clean"

Bonsoir BsAlv ,

C'est en testant (rapidement et succinctement) que j'ai employé Clean.

J'avais mis un Test1 en fin de note et par mégarde je l'avais fait suivre immédiatement d'un retour à la ligne. Mon code ne supprimait pas ce dernier Test1.

C'est pourquoi j'ai introduit ce "Clean" (en bon français Epurage()).

Ce qui montre encore une fois que le test d'un code aussi "modeste" soit-il est un vrai métier que je néglige bien souvent (tout comme les commentaires) .

Re,

Merci à tous pour vos propositions

Rechercher des sujets similaires à "reperer supprimer valeur commentaire"