Code VBA pour MAJ graph avec données sur onglet différent

Bonjour à tous,

Je rencontre un problème d'écriture qui m'est insolvable alors que certainement très simple.

J'ai une feuille de données et je souhaite une feuille avec mon graphique. Je souhaite via un bouton MAJ le graphique en intégrant les étiquettes + un code couleur à mes points en fonction de ce que j'aurai surligné dans le tableau de données. Le truc, c'est que mes codes sont bons (normalement) mais seulement pour être sur la même feuille que les données, or c'est impossible dans mon cas il faut que je sépare les données du graph. Comment faire à partir de ca :

MACRO ETIQUETTES

Sub labelandcolor()

On Error Resume Next

Sheets(1).ChartObjects(1).Chart.SeriesCollection(1).ApplyDataLabels Type:=xlDataLabelsShowLabel

On Error GoTo 0

For i = 1 To Sheets(1).ChartObjects(1).Chart.SeriesCollection(1).Points.Count

Sheets(1).ChartObjects(1).Chart.SeriesCollection(1).Points(i).DataLabel.Text = Cells(i + 1, 1)

Sheets(1).ChartObjects(1).Chart.SeriesCollection(1).Points(i).DataLabel.Font.Size = 9

Next i

End Sub

MACRO COULEURS

Sub ModifCouleur2()

ActiveSheet.ChartObjects(1).Activate

For i = 1 To ActiveChart.SeriesCollection(1).Points.Count

ActiveChart.SeriesCollection(1).Points(i).MarkerBackgroundColorIndex = _

ActiveSheet.Cells(i + 1, 1).Interior.ColorIndex

Next i

End Sub

Tout est dans le fichier ci-joint si besoin.

Merci beaucoup pour ceux qui pourront m'aider !

20graphaide.xlsm (28.02 Ko)

Bonjour,

Une proposition à adapter.

Cdlt.

20graphaide.xlsm (29.81 Ko)
Option Explicit

Public Sub LabelAndColor()
Dim wb As Workbook
Dim wsData As Worksheet, wsChart As Worksheet
Dim objChart As ChartObject
Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Données")
    Set wsChart = wb.Worksheets("Graphique")
    Set objChart = wsChart.ChartObjects(1)

    With objChart.Chart.SeriesCollection(1)
        .ApplyDataLabels Type:=xlDataLabelsShowLabel
        For i = 1 To .Points.Count
            With .Points(i)
                .DataLabel.Text = wsData.Cells(i + 1, 1)
                .DataLabel.Font.Size = 9
                .MarkerBackgroundColorIndex = _
                wsData.Cells(i + 1, 1).Interior.ColorIndex
            End With

        Next i
    End With

    Set objChart = Nothing
    Set wsChart = Nothing: Set wsData = Nothing

End Sub

Jean-Eric, merci beaucoup c'est vraiment super.

J'avais deux questions supplémentaires à ce sujet :

1/ J'ai dupliqué le graph avec une autre colonne de chiffres dans mon tableau de données. Je voudrais appliquer la même mise à jour aux deux graphs en même temps. C'est possible ? Car quand je modifie la ligne suivante avec un "2", il applique la MAJ au graph N°2 seulement, or je voudrais une MAJ des deux :

Set objChart = wsChart.ChartObjects(1)

2/ Existe t-il une ligne simple à ajouter permettant un RAZ qui enlèverait les noms avec un autre bouton ?

Encore merci

J'ai réussi à intégrer un bouton de RAZ donc il ne me reste qu'un problème en suspend :

Appliquer la même chose à un second graphique. Ca peut se faire comment ?

Public Sub LabelAndColor()

Dim wb As Workbook

Dim wsData As Worksheet, wsChart As Worksheet

Dim objChart As ChartObject

Dim i As Long

Set wb = ActiveWorkbook

Set wsData = wb.Worksheets("Données")

Set wsChart = wb.Worksheets("Graphique")

Set objChart = wsChart.ChartObjects(1)

With objChart.Chart.SeriesCollection(1)

.ApplyDataLabels Type:=xlDataLabelsShowLabel

For i = 1 To .Points.Count

With .Points(i)

.DataLabel.Text = wsData.Cells(i + 1, 1)

.DataLabel.Font.Size = 9

.MarkerBackgroundColorIndex = _

wsData.Cells(i + 1, 1).Interior.ColorIndex

End With

Next i

End With

Set objChart = Nothing

Set wsChart = Nothing: Set wsData = Nothing

End Sub

Bonjour,

Ci-dessous la procédure modifiée pour prendre en compte un ensemble de graphiques dans une même feuille.

Cdlt.

Option Explicit

Public Sub LabelAndColor()
Dim wb As Workbook
Dim wsData As Worksheet, wsChart As Worksheet
Dim objChart As ChartObject
Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Données")
    Set wsChart = wb.Worksheets("Graphique")

    For Each objChart In wsChart.ChartObjects
        With objChart.Chart.SeriesCollection(1)
            .ApplyDataLabels Type:=xlDataLabelsShowLabel
            For i = 1 To .Points.Count
                With .Points(i)
                    .DataLabel.Text = wsData.Cells(i + 1, 1)
                    .DataLabel.Font.Size = 9
                    .MarkerBackgroundColorIndex = _
                    wsData.Cells(i + 1, 1).Interior.ColorIndex
                End With
            Next i
        End With
    Next objChart

    Set wsChart = Nothing: Set wsData = Nothing

End Sub

Jean Eric, merci beaucoup

Je me permets également de te demander une dernière variante : en cas de présence d'un troisième graphique n'ayant rien avoir avec les premiers et que je ne souhaite pas voir être modifié. Le For Each applique sur tout et me plante le graph 3/3 sur la même feuille. Comment n'en sélectionner que certains ?

Merci encore !

Bonjour,

Une dernière proposition à étudier.

Cdlt.

21graphaide.xlsm (36.44 Ko)
Option Explicit
' Tous les graphiques sauf le graphique 3 de la feuille
Public Sub LabelAndColor3()
Dim wb As Workbook
Dim wsData As Worksheet, wsChart As Worksheet
Dim objChart As ChartObject
Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Données")
    Set wsChart = wb.Worksheets("Graphique")

    For Each objChart In wsChart.ChartObjects
        If objChart.Index <> 3 Then
            With objChart.Chart.SeriesCollection(1)
                .ApplyDataLabels Type:=xlDataLabelsShowLabel
                For i = 1 To .Points.Count
                    With .Points(i)
                        .DataLabel.Text = wsData.Cells(i + 1, 1)
                        .DataLabel.Font.Size = 9
                        .MarkerBackgroundColorIndex = _
                        wsData.Cells(i + 1, 1).Interior.ColorIndex
                    End With
                Next i
            End With
        End If
    Next objChart

    Set wsChart = Nothing: Set wsData = Nothing

End Sub

Jean Eric, c'est parfait, maintenant j'ai le choix et je sais comment gérer les situations ! ! ! 8)

Rechercher des sujets similaires à "code vba maj graph donnees onglet different"