Microsoft 365 Modifier le texte dans une forme

pat66

XLDnaute Impliqué
Bonjour le forum,

Je recherche la meilleure façon d'écrire cette action, qu'en pensez vous ? pensez vous que l'on peut écrire cette macro d'une meilleure manière ?

Un grand merci à tous

VB:
If ActiveSheet.Shapes("Rectangle : coins arrondis 135").DrawingObject.Text = "Masquer montants" Then
ActiveSheet.Shapes("Rectangle : coins arrondis 135").Fill.ForeColor.RGB = RGB(0, 32, 96)
ActiveSheet.Shapes("Rectangle : coins arrondis 135").DrawingObject.Text = "Afficher montants"
Sheets("simulateur").DrawingObjects("Rectangle : coins arrondis 135").Font.Color = RGB(255, 0, 0)
Sheets("simulateur").Shapes("Rectangle : coins arrondis 135").OLEFormat.Object.Border.Color = RGB(255, 160, 70)
With ActiveSheet.Shapes("Rectangle : coins arrondis 135").TextEffect
  .FontSize = 12
  .FontName = "Roboto"
End With
Else
ActiveSheet.Shapes("Rectangle : coins arrondis 135").Fill.ForeColor.RGB = RGB(255, 32, 96)
ActiveSheet.Shapes("Rectangle : coins arrondis 135").DrawingObject.Text = "Masquer montants"
Sheets("simulateur").DrawingObjects("Rectangle : coins arrondis 135").Font.Color = RGB(255, 255, 255)
Sheets("simulateur").Shapes("Rectangle : coins arrondis 135").OLEFormat.Object.Border.Color = RGB(255, 0, 0)
With ActiveSheet.Shapes("Rectangle : coins arrondis 135").TextEffect
  .FontSize = 14
  .FontName = "Arial"
End With
End If

NB : on peut aussi mettre une autre condition, selon que la forme "Groupe1" soit affichée ou pas
 
Dernière édition:

Franc58

XLDnaute Occasionnel
Bonjour, en utilisant une variable pour stocker l'objet on allège le code comme ceci:


VB:
Dim myShape As Shape
Set myShape = ActiveSheet.Shapes("Rectangle : coins arrondis 135")

If myShape.DrawingObject.Text = "Masquer montants" Then
    myShape.Fill.ForeColor.RGB = RGB(0, 32, 96)
    myShape.DrawingObject.Text = "Afficher montants"
    Sheets("simulateur").DrawingObjects("Rectangle : coins arrondis 135").Font.Color = RGB(255, 0, 0)
    Sheets("simulateur").Shapes("Rectangle : coins arrondis 135").OLEFormat.Object.Border.Color = RGB(255, 160, 70)
    With myShape.TextEffect
        .FontSize = 12
        .FontName = "Roboto"
    End With
Else
    myShape.Fill.ForeColor.RGB = RGB(255, 32, 96)
    myShape.DrawingObject.Text = "Masquer montants"
    Sheets("simulateur").DrawingObjects("Rectangle : coins arrondis 135").Font.Color = RGB(255, 255, 255)
    Sheets("simulateur").Shapes("Rectangle : coins arrondis 135").OLEFormat.Object.Border.Color = RGB(255, 0, 0)
    With myShape.TextEffect
        .FontSize = 14
        .FontName = "Arial"
    End With
End If
 

patricktoulon

XLDnaute Barbatruc
Bonjour
avant d'essayer d'eclaircir il faudrait déjà te decider par quelle méthode et collection tu veux travailler ta shape
collection shapes ou drawingobjects?? ;)
allez bonne route
VB:
With ActiveSheet.DrawingObjects("Rectangle : coins arrondis 135")
        If .Text = "Masquer montants" Then
            .Interior.Color = RGB(0, 32, 96)
            .Text = "Afficher montants"
            .Font.Color = RGB(255, 0, 0)
            .Border.Color = RGB(255, 160, 70)
            .Font.Size = 12
            .Font.Name = "Roboto"
        Else
            .Interior.Color = RGB(255, 32, 96)
            .Text = "Masquer montants"
            .Font.Color = RGB(255, 255, 255)
            .Border.Color = RGB(255, 0, 0)
            .Font.Size = 14
            .Font.Name = "Arial"
        End If
    End With
 

ChTi160

XLDnaute Barbatruc
Bonjour le Fil
Sans Fichier pas évident !
j'ai pensé que ceux-ci pourrait être sous cette Forme
VB:
With myShape
    With .DrawingObject
           .Text = IIf(.Text = "Masquer montants", "Afficher montants", "Masquer montants")
           .Fill.ForeColor.RGB = IIf(.Text = "Masquer montants", RGB(0, 32, 96), RGB(255, 32, 96))
           .OLEFormat.Object.Border.Color = IIf(.Text = "", RGB(255, 160, 70), RGB(255, 0, 0))
    End With
    With .TextEffect
            .FontSize = IIf(.Text, 12, 14)
            .FontName = IIf(.Text, "Roboto", "Arial")
    End With
End With
Non testé pas de Fichier!
Jean marie
 

patricktoulon

XLDnaute Barbatruc
re
Bonjour jean-marie
.OLEFormat.Object. ??????
c'est pas la peine drawingobject est deja un object !!!!
With .TextEffect???????
le drawingobject n'a pas cette propriété


allez pour la route
VB:
Sub test()
    With ActiveSheet.DrawingObjects("Rectangle : coins arrondis 135")
        If .Text = "Masquer montants" Then
            .Interior.Color = RGB(0, 32, 96)        'couleur de la shape
            .Text = "Afficher montants"             'texte de la shape
            .Font.Color = RGB(255, 0, 0)            'couleur du texte
            .Border.Color = RGB(255, 160, 70)       'couleur de la bordure
            .ShapeRange.Line.Weight = 5             'epaisseur de la bordure(obligé de passer par shaperange car drawingobject ne gère pas cette propriété)
            .Font.Size = 12                         'font size du texte
            .Font.Name = "Roboto"                   'police de caractere
            .Font.Bold = False                      'texte en gras ou pas
            .Font.Italic = True                     'texte en italic ou pas
            .HorizontalAlignment = xlHAlignCenter   'alignement horizontal du texte(à droite =xlHAlignRight / a gauche =xlHAlignLeft)
            .VerticalAlignment = xlVAlignCenter     'alignement vertical du texte( en bas = xlVAlignBottom / en haut =xlVAlignTop )
        Else
            .Interior.Color = RGB(255, 32, 96)          'couleur de la shape
            .Text = "Masquer montants"                  'texte de la shape
            .Font.Color = RGB(255, 255, 255)            'couleur du texte
            .Border.Color = RGB(255, 0, 0)              'couleur de la bordure
            .ShapeRange.Line.Weight = 10                'epaisseur de la bordure(obligé de passer par shaperange car drawingobject ne gère pas cette propriété)
            .Font.Size = 14                             'font size du texte
            .Font.Name = "Arial"                        'police de caractere
            .Font.Bold = True                           'texte en gras ou pas
            .Font.Italic = False                        'texte en italic ou pas
            .HorizontalAlignment = xlHAlignCenter       'alignement horizontal du texte(à droite =xlHAlignRight / a gauche =xlHAlignLeft)
            .VerticalAlignment = xlVAlignCenter         'alignement vertical du texte( en bas = xlVAlignBottom / en haut =xlVAlignTop )
        End If
    End With
End Sub

c'est simple tout ce qui est propriété de base c'est casiment comme les cellules
sauf l'epaisseur de trait etdonc là on passe simplement par la classe .shaperange
 

Discussions similaires

Statistiques des forums

Discussions
312 206
Messages
2 086 226
Membres
103 159
dernier inscrit
FBallea