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 81 82 83 84 85 86 87 88 89
| Sub CreateGraph()
Dim choice1 As String
Dim choice2 As String
With Worksheets("Feuil1")
choice1 = InputBox("Première plage ?" & vbLf & "(formalisme du type A1:B10 obligatoire)", "Plage de cellule", "A1:B10")
choice2 = InputBox("Seconde plage ?" & vbLf & "(formalisme du type C1:D10 obligatoire)", "Plage de cellule", "C1:D10")
'Delete all the graphs which are already on the worksheet
If .ChartObjects.Count <> 0 Then .ChartObjects.Delete
For k = 1 To 2
'Add a graph
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:="Feuil1"
'Delete all series when excel fills the graph automatically
With ActiveChart
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
End With
'Set the position of the graphe (here: on the range A1:P25)
With .ChartObjects(k)
.Left = Range("A1:P25").Offset(0, 26 * (k - 1)).Left
.Top = Range("A1:P25").Offset(0, 26 * (k - 1)).Top
.Width = Range("A1:P25").Offset(0, 26 * (k - 1)).Width
.Height = Range("A1:P25").Offset(0, 26 * (k - 1)).Height
End With
'In the graph we are creating...
With ActiveChart
'Set the title
.HasTitle = True
With .ChartTitle
.Characters.Text = "Title " & k
.Border.Weight = xlHairline
.Font.Size = 20
End With
'Set the Y axis title
With .Axes(xlValue)
.HasTitle = True
With .AxisTitle
.Caption = "Y axis"
.Font.Size = 13
.Font.Bold = True
End With
End With
'Set the X axis title
With .Axes(xlCategory)
.HasTitle = True
With .AxisTitle
.Caption = "X axis"
.Font.Size = 13
.Font.Bold = True
End With
End With
Select Case k
Case 1
.SetSourceData Source:=Worksheets("Feuil1").Range(choice1), PlotBy:=xlRows
Case 2
.SetSourceData Source:=Worksheets("Feuil1").Range(choice2), PlotBy:=xlRows
End Select
For i = 1 To .SeriesCollection.Count
With .SeriesCollection(i)
.ChartType = xlLine
Select Case k
Case 1
.Name = Worksheets("Feuil1").Range("H4").Offset(i - 1, 0)
Case 2
.Name = Worksheets("Feuil1").Range("H7").Offset(i - 1, 0)
End Select
.Border.Color = RGB(0, 0, 255 / i)
End With
Next i
.Legend.Position = xlBottom
End With
Next k
End With
End Sub |
Partager