Simplifier Macro
Bonjour à tous,
Déjà un grand merci pour l'aide que vous allez m'apporter aujourd'hui.
Je travaille dans un syndicat d'assainissement.
Ma direction me demande des macros pour générer des courbes à partir de bases de données; ce que je suis parvenu à faire (non sans mal) avec succès.
J'ai appris à utiliser VBA en utilisant l'outil Enregistrer Macro, j'ai réussi à me retrouver dans cette langue si particulière et commence à bien progresser. Malheureusement à cause du boulot, je n'ai pas le temps d'être un pro de chez pro et c'est pourquoi je fais appel à vous pour m'aider à simplifier ma macro.
Donc j'ai une macro bien lourde qui répète pas mal d'action, je vous en livre un extrait pour que vous vous rendiez compte de l'étendu de mon problème:
Charts.Add
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
ActiveChart.SetSourceData Source:=Sheets("Suivi Gaz Matin").Range( _
"A:N"), PlotBy:=xlColumns
ActiveChart.SeriesCollection(1).Name = "='Suivi Gaz Matin'!R1C2"
ActiveChart.SeriesCollection(2).Name = "='Suivi Gaz Matin'!R1C4"
ActiveChart.SeriesCollection(3).Name = "='Suivi Gaz Matin'!R1C6"
ActiveChart.SeriesCollection(4).Name = "='Suivi Gaz Matin'!R1C8"
ActiveChart.SeriesCollection(5).Name = "='Suivi Gaz Matin'!R1C10"
ActiveChart.SeriesCollection(6).Name = "='Suivi Gaz Matin'!R1C12"
ActiveChart.SeriesCollection(7).Name = "='Suivi Gaz Matin'!R1C14"
ActiveChart.Location Where:=xlLocationAsNewSheet, Name:= "Courbes CH4"
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).AxisGroup = 2
With Selection.Border
.ColorIndex = 57
.Weight = xlThick
.LineStyle = xlContinuous
End With
With Selection
.MarkerBackgroundColorIndex = xlAutomatic
.MarkerForegroundColorIndex = xlAutomatic
.MarkerStyle = xlNone
.Smooth = False
.MarkerSize = 7
.Shadow = False
End With
ActiveChart.SeriesCollection(2).Select
With Selection.Border
.ColorIndex = 3
.Weight = xlMedium
End With
ActiveChart.SeriesCollection(3).Select
With Selection.Border
.ColorIndex = 46
.Weight = xlMedium
End With
ActiveChart.SeriesCollection(4).Select
With Selection.Border
.ColorIndex = 4
.Weight = xlMedium
End With
ActiveChart.SeriesCollection(5).Select
With Selection.Border
.ColorIndex = 28
.Weight = xlMedium
End With
ActiveChart.SeriesCollection(6).Select
With Selection.Border
.ColorIndex = 7
.Weight = xlThick
End With
ActiveChart.SeriesCollection(7).Select
With Selection.Border
.ColorIndex = 10
.Weight = xlMedium
End With
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Concentration de CH4 Cormailles "
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Concentration CH4 (%)"
.Axes(xlValue, xlSecondary).HasTitle = True
.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "Hauteur en mNGF"
End With
With ActiveChart.Axes(xlCategory)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
With ActiveChart.Axes(xlValue)
.HasMajorGridlines = True
.HasMinorGridlines = False
End With
ActiveChart.HasLegend = True
ActiveChart.Legend.Select
Selection.Position = xlBottom
With ActiveChart.Axes(xlValue)
.MinimumScale = 0
End With
ActiveChart.Axes(xlCategory).Select
With ActiveChart.Axes(xlCategory)
.MinimumScale = Range("A2").Value
.MaximumScale = Range("A65536").End(xlUp).Value
.MajorUnit = 0.25
End With
With Selection.TickLabels
.ReadingOrder = xlContext
.Orientation = 45
End With
ActiveChart.PlotArea.Select
With Selection.Border
.ColorIndex = 16
.Weight = xlThin
.LineStyle = xlContinuous
End With
Selection.Fill.TwoColorGradient Style:=msoGradientHorizontal, Variant:=1
With Selection
.Fill.Visible = True
.Fill.ForeColor.SchemeColor = 2
.Fill.BackColor.SchemeColor = 37
End With
Je suis convaincu que je peux simplifier cette macro et que je peux supprimer plusieurs parties ou en réduire d'autres.
Je crée plusieurs onglets, cette macro n'est qu"une partie de l'iceberg, mais en relisant une macro simplifiée, je pense être en mesure de comprendre le raisonnement et l'appliquer à tout mon fichier.
Donc si quelqu'un pouvait m'aider, ça me rendrait un grand service.
Bonne journée à tout ceux qui m'auront lu.
Jeremy
Bonjour,
A vu de nez, ma boule de cristal, me dit qu'on ne peut pas grand chose pour cette macro.
Par contre si tu as 30 macros presque identiques pour des données analogues on peut peut-être tout regrouper dans la même macro.
Par exemple si tes données sont tabulaires le graphiques Février se déduit surement de janvier à quelques lignes (ou colonnes) près.
Ou encore toujours dans le même tableau les données de Pierre se déduise probablement de celles de son voisin.
Dans ce cas un ou plusieurs paramètres peuvent parfois simplifier le problème.
Mais sans fichier joint inutile d'espérer un gain significatif.
A+