VBA - Bouton mettre le texte entre parenthèses

Bonjour,

J'ai ce code qui fonctionne très bien pour pouvoir mettre le contenu d'une seule cellule (A1) entre parenthèses cependant je souhaiterais l'adapter pour pouvoir effectuer la même opération sur la ou les cellule(s) sélectionnées dans la zone A5: D54.

Sub BoutonMettreEntreParenthèses()
ActiveSheet.Unprotect Password:="."
If Left(Range("A1"),1)="(" and Right(Range("A1"),1)=")" Then
    Range("A1")=Mid(Range("A1"),2,Len(Range("A1"))-2)
Else
    Range("A1")="(" & Range("A1") & ")"
End if
ActiveSheet.Protect Password:="."
End Sub

Pouvez-vous m'aider?

Meilleures salutations,

Thierry

Bonjour,

Essayez ceci:

Sub BoutonMettreEntreParenthèses()
    ActiveSheet.Unprotect Password:="."
    For Each cell In Selection
        If Left(cell, 1) = "(" And Right(cell, 1) = ")" Then
            cell.Value = Mid(cell, 2, Len(cell) - 2)
        Else
            cell.Value = "(" & cell & ")"
        End If
    Next
    ActiveSheet.Protect Password:="."
End Sub

CDlt

Bonjour Arturo,

Merci beaucoup pour votre proposition qui fonctionne parfaitement bien cependant il fallait juste ne pas oublier de définir la variable:

Sub BoutonMettreEntreParenthèses()
Dim cell As Range
    ActiveSheet.Unprotect Password:="."
    For Each cell In Selection
        If Left(cell, 1) = "(" And Right(cell, 1) = ")" Then
            cell.Value = Mid(cell, 2, Len(cell) - 2)
        Else
            cell.Value = "(" & cell & ")"
        End If
    Next
    ActiveSheet.Protect Password:="."
End Sub

Bonne journée et encore merci

Rechercher des sujets similaires à "vba bouton mettre texte entre parentheses"