Sub InsérerGraphique()
Dim Wbk As Workbook, Wsh As Worksheet, Shp As Shape, Chrt As Chart
FichCSV = Application.GetOpenFilename("Relevé (*.csv), *.csv")
If FichCSV = False Then Exit Sub
'Ouvrir le fichier csv
Workbooks.OpenText Filename:=FichCSV, _
Origin:=65001, StartRow:=2, DataType:=xlDelimited, ConsecutiveDelimiter:=False, Comma:=True, _
DecimalSeparator:="."
Set Wsh = ActiveSheet
ActiveWindow.DisplayGridlines = False
'Données dans un tableau structuré
With Wsh.ListObjects.Add(xlSrcRange, Wsh.UsedRange, , xlYes)
.Name = "TS_Relevé"
.TableStyle = "TableStyleLight11"
.ShowTableStyleColumnStripes = True
End With
[TS_Relevé].EntireColumn.AutoFit
'Ajout d'un TS Heure Débit
With Wsh.ListObjects.Add(xlSrcRange, Wsh.ListObjects("TS_Relevé").Range.Offset(0, [TS_Relevé].Columns.Count + 1).Resize(, 2), , xlYes)
.Name = "TS_Débit"
.TableStyle = "TableStyleLight9"
.ShowTableStyleColumnStripes = True
End With
'Colonne des heures (formules puis ne conserver que les valeurs)
[TS_Débit].ListObject.Range.Cells(1).Value = "Heure"
With [TS_Débit[Heure]]
.Formula = "=DATE(TS_Relevé[@annee],TS_Relevé[@mois],TS_Relevé[@jour])+TIME(TS_Relevé[@Heure],TS_Relevé[@minutes],0)"
.Value = .Value
.NumberFormat = "hh:mm"
End With
'Colonne du débit (formules puis ne conserver que les valeurs)
[TS_Débit].ListObject.Range.Cells(1, 2).Value = "Débit m³/h"
With [TS_Débit[Débit m³/h]]
.Formula = "=TS_Relevé[@m3]*4"
.Value = .Value
End With
'S'écarter du TS pour éviter le graphique par défaut
Application.Goto Wsh.Cells(1).Offset(0, [TS_Relevé].Columns.Count + 5)
'Créer un graphique en X,Y
Set Shp = Wsh.Shapes.AddChart2(-1, xlXYScatter)
Set Chrt = Shp.Chart
'Position, dimensionnement
With Shp
.Top = Wsh.Cells(5, 3).Top
.Left = Wsh.Cells(5, 3).Left
.Width = Application.CentimetersToPoints(30)
.Height = Application.CentimetersToPoints(15)
End With
With Chrt
.ChartType = xlXYScatterLinesNoMarkers 'Pas de marqueurs sur la courbe
Set NS = .SeriesCollection.NewSeries 'Ajout de la série
End With
'Propriétés de la Serie
With NS
.Values = [TS_Débit[Débit m³/h]]
.XValues = [TS_Débit[Heure]]
.Name = "Relevé du " & Format(WorksheetFunction.Min([TS_Débit[Heure]]), "dd/mm/yyyy") & " au " & Format(WorksheetFunction.Max([TS_Débit[Heure]]), "dd/mm/yyyy")
End With
'Mise en forme du graphique
With Chrt
'Axe des X
.SetElement (msoElementPrimaryCategoryAxisTitleBelowAxis)
.Axes(xlCategory).AxisTitle.Caption = "heure"
'Axe des Y
.SetElement (msoElementPrimaryValueAxisTitleBelowAxis)
.Axes(xlValue).AxisTitle.Caption = "m³/h"
'Hauteur de la zone de tracé (on passe par selection car sinon bug sauf en pas à pas !)
.PlotArea.Select
Selection.Height = Application.CentimetersToPoints(12.5)
'Mise en forme de l'axe des X
With .Axes(xlCategory)
.TickLabels.Orientation = xlUpward
.TickLabels.NumberFormat = "hh:mm"
'Bornes sur des heures entières
.MinimumScale = WorksheetFunction.Floor(WorksheetFunction.Min([TS_Débit[Heure]]), 1 / 24)
.MaximumScale = WorksheetFunction.Ceiling(WorksheetFunction.Max([TS_Débit[Heure]]), 1 / 24) + 0.0000000001
'Unité principale 1 heure
.MajorUnit = 1 / 24
'Unité secondaire 14 d'heure
.MinorUnit = 1 / 24 / 4
.Format.Line.ForeColor.RGB = RGB(0, 0, 255)
.MajorTickMark = xlOutside
.MinorTickMark = xlOutside
.AxisTitle.Caption = "Heure"
.AxisTitle.Top = Application.CentimetersToPoints(14)
End With
End With
Application.Goto [TS_Relevé].Cells(1, 1)
End Sub