Sub Worksheet_Activate()
'
' GraphChangeCouleurSerie Macro
'
Dim SerieCourante As Series
Dim Ligne As Long
Dim CellTotal As Range 'Cellule contenant le nombre total de dossiers
Dim EspaceDeRecherche As Range
Dim Reponse As VbMsgBoxResult
Dim Protege As Boolean
On Error GoTo GestionErreurs
Protege = Me.ProtectContents
If Protege Then Me.Unprotect
'Recherche de la cellule contenant le nombre de dossiers
'+++Pour être plus fin pourquoi ne pas utiliser une cellule nommé "CellTotal" ?
Set EspaceDeRecherche = Range("C:C")
For Each CellTotal In EspaceDeRecherche
If CellTotal = "" Then Exit For
Next CellTotal
Set CellTotal = Cells(CellTotal.Row - 2, CellTotal.Column)
Application.EnableEvents = False
With ChartObjects("Graphique 1").Chart
'Applique au graphique les couleurs définies dans les cellules pour les états et masque les étiquettes des valeurs nulles
For Each SerieCourante In .SeriesCollection
Ligne = WorksheetFunction.Match(SerieCourante.Name, Range("B1:B20"), 0)
SerieCourante.Format.Fill.Solid
SerieCourante.Format.Fill.ForeColor.RGB = Cells(Ligne, 2).Interior.Color
If SerieCourante.Values(1) = 0 Then 'On masque l'étiquette car la valeur est 0
SerieCourante.HasDataLabels = False
Else 'Sinon l’étiquette doit apparaître dans la bonne couleur
SerieCourante.HasDataLabels = True
SerieCourante.DataLabels.ShowValue = True
SerieCourante.DataLabels.Font.Color = Cells(Ligne, 2).Font.Color
End If
Next SerieCourante
'Limite l'axe principal au nombre de données
.Axes(xlValue).MaximumScaleIsAuto = False
.Axes(xlValue).MaximumScale = CellTotal.Value
.Axes(xlValue).MinimumScaleIsAuto = False
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MajorUnit = (CellTotal.Value / 4)
End With
GoTo SortiePropre
GestionErreurs:
Err.Source = Err.Source & " dans la procédure ""Worksheet_Activate"" de " & ActiveSheet.Name 'Personalisation de la source de l'erreur
Reponse = MsgBox("Erreur : " & Err.Number & " - " & Err.Description & " dans " & Err.Source, vbCritical + vbSystemModal, "Une erreur s'est produite à l'exécution")
SortiePropre:
If Protege Then Me.Protect
Application.EnableEvents = True
End Sub