VBA - Générer graphique dynamique avec des données sources disjointes

  • Initiateur de la discussion Initiateur de la discussion alexanbat
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

alexanbat

XLDnaute Junior
Hello,

Je souhaite créer un graphique dynamique avec VBA. Ce graphique a pour données source un tableau ayant 2 series.

Colonne A : Mois
Colonne B : Serie 1
Colonne C : Serie 2

J'ai fait une macro qui fonctionne très bien et mon graphique se met à jour de manière dynamique lorsque j'utilise les colonne A et B.

Par contre je n'ai pas réussi à faire tourner cette même macro lorsque les colonnes sont disjointes (Colonne A et C) et générer le "graphique 3" (Voir pièce jointe), sachant que la plage de traçage dynamique est commune aux 2 graphiques

Merci par avance pour vos réponses et pour votre aide.

AL
 

Pièces jointes

Re : VBA - Générer graphique dynamique avec des données sources disjointes

Bonjour,

Essayez le code suivant
Code:
Sub Plot_Dynamic_Graph_Range()
'
' Plot_Dynamic_Graph_Range Macro
'
'Set the type of parameters

Dim LastRow As Long     'Define the last row address
Dim StartDate As Date   'Define the start date for plotting purpose
Dim EndDate As Date     'Define the end date for plotting purpose
Dim FirstRow As Long    'Define the first non empty row of the chart
Dim EndRow As Long      'Define the last non empty row of the chart
Dim Duration As String  'Define the range of plotting
Dim S As Worksheet
'---
Set S = Sheets("Avec VBA")
LastRow = S.Range("A:A").SpecialCells(xlCellTypeLastCell).Row      'Define the last non empty row number
StartDate = S.Range("L2")
EndDate = DateAdd("m", S.Range("L4"), StartDate) - 1               'Calculate the end date as an addition of the plotting duration to the start date
For Row = 2 To LastRow
  If S.Cells(Row, 1) >= StartDate Then FirstRow = Row: Exit For    'Iterative check if the first encountered row is greater than or equal to the start date of the plotting window
Next
For Row = LastRow To 2 Step -1
  If S.Cells(Row, 1) <= EndDate Then EndRow = Row: Exit For        'Iterative check if the last encountered row is less than or equal to the end date of the plotting windows
Next
'/// Les graphiques ///
Duration = S.Range("A" & FirstRow & ":A" & EndRow & "," & "B" & FirstRow & ":B" & EndRow).Address
S.ChartObjects("Graphique 2").Chart.SetSourceData Source:=S.Range(Duration), PlotBy:=xlColumns
'---
Duration = S.Range("A" & FirstRow & ":A" & EndRow & "," & "C" & FirstRow & ":C" & EndRow).Address
S.ChartObjects("Graphique 3").Chart.SetSourceData Source:=S.Range(Duration), PlotBy:=xlColumns
End Sub
 

Pièces jointes

- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
Microsoft 365 création Graphique
Réponses
6
Affichages
594
Réponses
1
Affichages
455
Retour