Bonjour,

J'ai un problème lors de la création d'un graphique en VBA. Dans deux colonnes(Abscisse et Ordonnée), j'ai des valeurs et des "blancs". Je créé deux tableaux qui contiennent toutes les des colonnes sans les "blancs".

Ensuite je veux mettre en abscisse et en ordonnée de mon graphique ces deux tableau de valeurs (nombres à virgule ou string), mais cela génère une erreur 1004.

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Private Sub Workbook_Open()

Dim SC As Excel.ChartObject
Dim PositionTop, PositionLeft, i, j As Integer
Dim AxeY(), AxeX() 'As Variant

Dim chrt As ChartObject

For Each chrt In Sheets("SUIVI").ChartObjects

chrt.Delete

Next

PositionTop = Range("AM" & Cells(150, 2).End(xlUp).Row + 5).Top
PositionLeft = Range("AM" & Cells(150, 2).End(xlUp).Row + 5).Left

Set SC = ActiveSheet.ChartObjects.Add(PositionLeft, PositionTop, 1100, 600)

SC.Chart.SeriesCollection.NewSeries

j = 0

For i = 4 To Cells(150, 2).End(xlUp).Row - 1

    If Cells(i, 57).Value = "" Or Cells(i, 57).Value = "0" Then
    
    Else
    
        j = j + 1
        ReDim Preserve AxeY(j)
        ReDim Preserve AxeX(j)
        AxeY(j) = Range("BE" & i).Value 'Cells(i, 57).Value
        AxeX(j) = Cells(i, 2).Row '.Value
    
    End If

Next i

ReDim Preserve AxeY(j + 1)
ReDim Preserve AxeX(j + 1)

AxeY(j + 1) = ""
AxeX(j + 1) = ""

'Erreur 1004 pour l'un comme pour l'autre
SC.Chart.SeriesCollection(1).XValues = AxeX
SC.Chart.SeriesCollection(1).Values = AxeY

SC.Chart.ChartType = xlColumnClustered
SC.Chart.Axes(xlCategory, xlPrimary).TickLabels.Orientation = -45
SC.Chart.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
SC.Chart.ApplyDataLabels ShowValue:=True

SC.Chart.SeriesCollection(1).Name = "=""% Total heures"""

With SC.Chart
    .HasAxis(xlCategory, xlPrimary) = True
    .HasAxis(xlValue, xlPrimary) = True
    .Axes(xlCategory, xlPrimary).AxisBetweenCategories = False
End With

With SC.Chart.Axes(xlCategory, xlPrimary)
    .CrossesAt = 1
    .TickLabelSpacing = 1
    .TickMarkSpacing = 1
    .AxisBetweenCategories = False
    .ReversePlotOrder = False
End With

With SC.Chart.Axes(xlCategory, xlPrimary)
    .CrossesAt = 1
    .TickLabelSpacing = 1
    .TickMarkSpacing = 1
    .AxisBetweenCategories = False
    .ReversePlotOrder = False
End With

End Sub
Si quelqu'un pourrait me dire comment faire en sorte que mon tableau soit pris en compte. Ou bien me donner une méthode pour récupérer une plage de valeurs sans les cellules vides.

Merci d'avance.