Mise en forme conditionnelle sur graphique en VBA

Bonjour le forum,

Je gère mon graphique avec deux critères :

  • La moyenne des ecart
  • Les ecarts du client choisi

Et je voudrais que si les écarts du client sont inférieurs à la moyenne, encadrer la barre correspondante en vert si c'est inférieur à la moyenne et en rouge si c'est supérieur.

J'ai donc créé une macro avec la fonction IF qui correspond à ma demande mais parfois le résultat est rouge alors qu'il devrait être vert et je ne comprend pas pourquoi.

Je vous envoi une parti de la macro car il y en a une pour chaque critere :

Sub TEST_colorl1()

Dim sh As Worksheet

Set sh = Worksheets("ESSAI")

 If sh.Range("d8") <= sh.Range("b8") Then

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(0, 255, 0)
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

  Else

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With
End If

merci d'avance pour votre aide

Bonjour quentinmgt,

Je n'ai pas du tout essayé ton code ni ton fichier car je suis sur mon tel mais déjà ton code pourrai être simplifié comme ceci :

Sub TEST_colorl1()

Dim sh As Worksheet

Set sh = Worksheets("ESSAI") 

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
         If sh.Range("d8") <= sh.Range("b8") Then
            .ForeColor.RGB = RGB(0, 255, 0) 
          Else
            .ForeColor.RGB = RGB(255, 0, 0) 
           End if
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

Florain53,

Merci beaucoup, j'essaie en rentrant car je suis aussi sur mon tel !

Bonjour,

après essai, la macro fonction seulement si elle ne contient qu'un seul ordre :

Dim sh As Worksheet

Set sh = Worksheets("ESSAI") 

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
         If sh.Range("d8") <= sh.Range("b8") Then
            .ForeColor.RGB = RGB(0, 255, 0) 
          Else
            .ForeColor.RGB = RGB(255, 0, 0) 
           End if
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

A partir du moment ou j'en met plus d'un tout est détraqué et les je ne comprend pas pourquoi.. :

Dim sh As Worksheet

Set sh = Worksheets("ESSAI") 

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
         If sh.Range("d8") <= sh.Range("b8") Then
            .ForeColor.RGB = RGB(0, 255, 0) 
          Else
            .ForeColor.RGB = RGB(255, 0, 0) 
           End if
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(3).Select
    With Selection.Format.Line
        .Visible = msoTrue
         If sh.Range("d9") <= sh.Range("b9") Then
            .ForeColor.RGB = RGB(0, 255, 0) 
          Else
            .ForeColor.RGB = RGB(255, 0, 0) 
           End if
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

Essaye en convertissant les valeurs :

Dim sh As Worksheet

Set sh = Worksheets("ESSAI") 

    sh.ChartObjects("Graphique 1").Activate
    ActiveChart.FullSeriesCollection(2).Points(4).Select
    With Selection.Format.Line
        .Visible = msoTrue
         If sh.cint(Range("d8") ) <= sh.cint(Range("b8")) Then
            .ForeColor.RGB = RGB(0, 255, 0) 
          Else
            .ForeColor.RGB = RGB(255, 0, 0) 
           End if
        .Transparency = 0
    End With
    With Selection.Format.Line
        .Visible = msoTrue
        .Weight = 7
    End With

J'ai une erreur ("Membre de méthode ou de données introuvable") au lancement de la macro sur la ligne :

If sh.cint(Range("d8") ) <= sh.cint(Range("b8")) Then

Pardon autant pour moi essaye comme ça:

If Cint(sh.(Range("d8") ) <= Cint(sh.(Range("b8")) Then

J'ai modifier le code et c'est exactement comme au début, c'est comme ci la fonction IF ne fonctionnait pas du moins seulement certaines fois

Bonjour quentinmgt ,

Voici un fichier en retour avec une proposition qui me semble fonctionner.

Rechercher des sujets similaires à "mise forme conditionnelle graphique vba"