Tracer graph sur plage variable en VBA
Bonjour à tous,
J'ai un petit problème pour tracer un graph avec une macro.
Sur ma feuille 1, je dispose d'une plage de donnée qui se rempli en fonction du choix d'un menu déroulant (on choisi une commodité dans le menu déroulant et une macro va récupérer les valeurs dans un tableau en feuille 2 puis les copies sur la feuille 1).
Les valeurs sont des prix ou indices et sont donc associées à une date.
Jusqu'à maintenant je traçais le graphe du début à la fin de ma plage temporelle.
Or pour certaines commodités je n'ai pas de valeur soit au début de ma plage, soit à la fin. Et malheureusement Excel me trace les données sur toute la plage temporelle. Je me retrouve donc avec une courbe sur une plage plus grande que nécessaire.
Je souhaiterais donc, dans un soucis d’esthétique, afficher sur mon graph seulement les abscisses qui sont associées à une valeur.
Je ne pense pas que cela soit difficile mais je n'arrive pas à trouver comment coder ce graph sur plage variable.
Merci d'avance pour vos suggestions de code VBA.
Bonjour,
Je ne vois pas la macro qui trace le graphe de la zone entière dont tu cites dans ton message.
Voilà le bon fichier avec la macro
Bonjour,
Un exemple à adapter.
Cdlt.
Option Explicit
Public Sub DEMO()
Dim ws As Worksheet
Dim d As Long, f As Long
Dim rng_X As Range, rng_Y As Range
Set ws = ActiveSheet
With ws
d = IIf(.Cells(13, 2) = "", .Cells(13, 2).End(xlDown).Row, 13)
f = .Cells(.Rows.Count, 2).End(xlUp).Row
Set rng_X = .Cells(d, 1).Resize(f - d + 1)
Set rng_Y = .Cells(d, 2).Resize(f - d + 1)
End With
MsgBox rng_X.Address
MsgBox rng_Y.Address
Set rng_Y = Nothing
Set rng_X = Nothing
Set ws = Nothing
End SubMerci pour vos réponses.
Ton code fonctionne parfaitement Raja.
Je l'ai donc adapté à mon fichier de travail, mais je reçoit une erreur 1004 qui me surligne en jaune la ligne de sélection des valeurs du graphique.
Je n'arrive pas à déterminer d'où peut venir cette erreur ..
Voici le code :
Sub RECH()
Dim O1 As Worksheet 'déclare la variable O1 (Onglet 1)
Dim O2 As Worksheet 'déclare la variable O1 (Onglet 1)
Dim DEST As Range 'déclare la variable DEST (cellule de DESTination)
Dim DEST1 As Range
Dim F As Range 'déclare la variable F (Famille)
Dim C As Range 'déclare la variable C (Commodité)
Dim COL As Byte 'déclare la variable COL (COLonne)
Dim COL1 As Byte
Dim MONTHS As Range
Dim YEARS As Range
Dim Graph As ChartObject
Set O1 = Worksheets("Formulaire") 'définit l'onglet O1
Set O2 = Worksheets("Monthly Commdity Indexes") 'définit l'onglet O2
Set O3 = Worksheets("Yearly Commdity Indexes") 'définit l'onglet O2
Set DEST = O1.Range("B10") 'définit la cellule de destination DEST
Set DEST1 = O1.Range("E10") 'définit la cellule de destination DEST1
Set F = O1.Range("C2") 'définit la cellule F (Famille)
Set C = O1.Range("C4") 'définit la cellule C (Commodité)
Set MONTHS = O2.Range("A4:A245") 'définit la plage temporelle en mois
Set YEARS = O3.Range("A4:A25") 'définit la plage temporelle en année
O1.Range("A10:F260").Clear 'Effacer toute donnée existante sur la plage utilisée
If C = "EUR/USD" Or C = "EUR/GBP" Or C = "EUR/BRL" Or C = "EUR/CNY" Or C = "Oil Wti" Or C = "Oil Brent" Or C = "Electricity USA" Or C = "Natural Gas USA" Or C = "Benzene" Or C = "GPS EUR" Or C = "PP EUR" Or C = "K-resine" Or C = "GPS USA" Or C = "HIPS USA" Or C = "PP USA" Or C = "PC USA" Or C = "PA USA" Or C = "APET USA" Or C = "HDPE USA" Or C = "Rubber" Or C = "GPS ASIA" Or C = "HDPE ASIA" Or C = "PP ASIA" Or C = "Alu" Or C = "Copper" Or C = "Composite Stainless Steel" Or C = "Cobalt" Or C = "Soybeans" Or C = "Live Cattle" Or C = "Pork (Swine)" Or C = "Rennet Casein" Or C = "Intelectual Services" Or C = "Wood pulp" Or C = "White top (kraft)" Or C = "Wellenstoff" Or C = "Baltic Dry Index" Then
COL = O2.Range("B1:AL1").Find(C.Value, , xlValues, xlWhole).Column
COL1 = O3.Range("B1:CO1").Find(C.Value, , xlValues, xlWhole).Column
O2.Range(O2.Cells(4, COL), O2.Cells(245, COL)).Copy Destination:=DEST 'copie la plage correspondante et colle ses valeurs dans DEST
O3.Range(O3.Cells(4, COL1), O3.Cells(26, COL1 + 1)).Copy Destination:=DEST1 'copie la plage correspondante et colle ses valeurs dans DEST1
MONTHS.Copy Destination:=O1.Range("A10")
YEARS.Copy Destination:=O1.Range("D10")
For Each Graph In O1.ChartObjects
If Graph.Name = "graph" Then
Graph.Delete
Exit For
End If
Next Graph
For i = 12 To 251
If Cells(i, "B") <> "" Then
deb = i
Exit For
End If
Next i
For i = deb To 251
If Cells(i, "B") = "" Then
fin = i - 1
Exit For
End If
Next i
Set Graph = O1.ChartObjects.Add(485, 135, 500, 300)
With Graph.Chart
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = O1.Range(O1.Cells(i, 2), O1.Cells(fin, 2))
.SeriesCollection(1).XValues = O1.Range(O1.Cells(deb, 1), O1.Cells(fin, 1))
.SeriesCollection(1).Name = "=Formulaire!$B$10"
End With
Graph.Name = "graph"
End If
If C = "Natural Gas EUR" Or C = "Electricity EU" Or C = "Electricity France" Or C = "Electricity Italy" Or C = "Electricity Spain" Or C = "Inflation Eurozone" Or C = "Inflation France" Or C = "Inflation Italy" Or C = "Inflation USA" Or C = "Inflation China" Then
COL1 = O3.Range("B1:CO1").Find(C.Value, , xlValues, xlWhole).Column
O3.Range(O3.Cells(4, COL1), O3.Cells(26, COL1 + 1)).Copy Destination:=DEST1 'copie la plage correspondante et colle ses valeurs dans DEST1
YEARS.Copy Destination:=O1.Range("D10")
For Each Graph In O1.ChartObjects
If Graph.Name = "graph" Then
Graph.Delete
Exit For
End If
Next Graph
For i = 12 To 31
If Cells(i, "E") <> "" Then
deb = i
Exit For
End If
Next i
For i = deb To 31
If Cells(i, "E") = "" Then
fin = i - 1
Exit For
End If
Next i
Set Graph = O1.ChartObjects.Add(485, 135, 500, 300)
With Graph.Chart
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = O1.Range(O1.Cells(deb, 5), O1.Cells(fin, 5))
.SeriesCollection(1).XValues = O1.Range(O1.Cells(deb, 4), O1.Cells(fin, 4))
.SeriesCollection(1).Name = "=Formulaire!$E$10"
End With
Graph.Name = "graph"
End If
If C = "" Or C = "select" Then
For Each Graph In O1.ChartObjects
If Graph.Name = "graph" Then
Graph.Delete
Exit For
End If
Next Graph
MsgBox ("select a commodity")
End If
O1.Range("C2").Value = ("select")
O1.Range("C4").Value = ("select")
End Sub Merci d'avance
PS : Je ne peut vous fournir le fichier excel celui-ci étant confidentiel.