XL 2010 VBA - Bouton retrait gauche du texte

tchi456

XLDnaute Occasionnel
Bonjour,

J'ai ce code qui fonctionne très bien pour pouvoir faire un retrait gauche du texte.

VB:
'Bouton pour faire un retrait gauche du texte de 2

Sub BoutonTexteRetrait()
    ActiveSheet.Unprotect Password:="."
    Dim Cel As Range
    For Each Cel In Selection
    Cel.IndentLevel = Cel.IndentLevel + IIf(Cel.IndentLevel = 0, 2, -2)
    Next Cel
    ActiveSheet.Protect Password:="."
End Sub

Je souhaiterais l'adapter pour pouvoir en même temps ajouter un tiret devant le texte.

Exemple:

Texte
Je clique sur le bouton:
- Texte
Je reclique sur le bouton:
Texte

Pouvez-vous m'aider?

Meilleures salutations,

Thierry
 
Solution
Bonjour Job75 et Phil69970,

Merci pour vos propositions. Ca fonctionne à merveille.

VB:
Sub BoutonTexteRetrait()
    ActiveSheet.Unprotect Password:="."
    Dim Cel As Range
    For Each Cel In Selection
    Cel = IIf(Left(Cel, 1) = "-", Trim(Mid(Cel, 2)), "-" & Trim(Cel))
    Cel.IndentLevel = Cel.IndentLevel + IIf(Cel.IndentLevel = 0, 1, -1)
    Next Cel
    ActiveSheet.Protect Password:="."
End Sub

Bonne journée!

Thierry

job75

XLDnaute Barbatruc
Bonjour tchi456,

Avec un tiret au début plus question de retrait :
VB:
Sub BoutonTexteRetrait()
    Dim Cel As Range
    ActiveSheet.Protect Password:=".", UserInterfaceOnly:=True
    For Each Cel In Selection
        Cel = IIf(Left(Cel, 1) = "-", Trim(Mid(Cel, 2)), "- " & Trim(Cel))
    Next Cel
End Sub
A+
 

Phil69970

XLDnaute Barbatruc
Bonjour @tchi456

Je te propose :

VB:
Sub BoutonTexteRetrait()
ActiveSheet.Unprotect Password:="."
Dim Cel As Range
For Each Cel In Selection
    If Cel.IndentLevel <> 0 Then
        Cel.IndentLevel = 0
    Else
        Cel.IndentLevel = Cel.IndentLevel + IIf(Cel.IndentLevel = 0, 2, -2)
    End If
   
Next Cel
ActiveSheet.Protect Password:="."
End Sub

*Merci de ton retour

@Phil69970
 

tchi456

XLDnaute Occasionnel
Bonjour Job75 et Phil69970,

Merci pour vos propositions. Ca fonctionne à merveille.

VB:
Sub BoutonTexteRetrait()
    ActiveSheet.Unprotect Password:="."
    Dim Cel As Range
    For Each Cel In Selection
    Cel = IIf(Left(Cel, 1) = "-", Trim(Mid(Cel, 2)), "-" & Trim(Cel))
    Cel.IndentLevel = Cel.IndentLevel + IIf(Cel.IndentLevel = 0, 1, -1)
    Next Cel
    ActiveSheet.Protect Password:="."
End Sub

Bonne journée!

Thierry
 

Discussions similaires

Statistiques des forums

Discussions
312 074
Messages
2 085 071
Membres
102 770
dernier inscrit
mathieu.lemaitre