graphique avec des données sources variables

MINO

XLDnaute Junior
hallo ,
j`ai crée un macro pour la création d`un graphique Via VBA, ca fonctionne très b1 mais la probleme et mon intervale source et variable (D6:D70) ,il peut ce changer pour etre par exemple (D6:D55)
ya t`il une solution pour déclarée la source varaible
mon Code est la suivant:
Sub cree_graphxxxx()
Dim NbGraph As Byte
Dim wks As Worksheet
Dim Drang As String

For Each wks In ThisWorkbook.Worksheets
If wks.Name Like "Line ##" Then
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=wks.Range("D6:D70"), _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=wks.Name
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Temps de Cycle"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = ""
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Takt"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = False
.HasAxis(xlValue, xlPrimary) = True
End With
NbGraph = ActiveSheet.ChartObjects.Count 'compte le nombre de graphiques dans la feuille
'le nouveau graphique correspond à l'index le plus élevé
ActiveSheet.ChartObjects(NbGraph).Activate
ActiveChart.ChartTitle.Select
ActiveSheet.ChartObjects(NbGraph).Name = "Takt"
With ActiveSheet.Shapes("Takt")
.Left = Range("e15").Left
.Top = Range("e15").Top
End With
wks.Range("A1").Select
End If

Next wks
End Sub
 

Staple1600

XLDnaute Barbatruc
Re : graphique avec des données sources variables

Re

Si j'ai bien compris ce que tu souhaites
Modifies comme suit cette partie du code
Code:
'ici ton code initial
For Each wks In ThisWorkbook.Worksheets
If wks.Name Like "Line ##" Then
dv = wks.[D65536].End(xlUp).Row
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=wks.Range("D6:D" & dv), _
PlotBy:=xlColumns
'ici suite de ton code initial
Sans oublier de déclarer dv (en début de code)
Dim dv&
 

sourcier08

XLDnaute Occasionnel
Re : graphique avec des données sources variables

Salut,

Et comme ça, rien ne change ?


Code:
'ici ton code initial
For Each wks In ThisWorkbook.Worksheets
If wks.Name Like "Line ##" Then
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=wks.Range("d6:d" & Range(d & Rows.Count).End(xlUp).Row), _
PlotBy:=xlColumns
'ici suite de ton code initial
 

Staple1600

XLDnaute Barbatruc
Re : graphique avec des données sources variables

Re


Comme ceci, cela semble OK, non ?
Code:
Sub cree_graph2()
Dim NbGraph As Byte
Dim wks As Worksheet
Dim Drang As String
Dim p As Range
For Each wks In ThisWorkbook.Worksheets
If wks.Name Like "Line ##" Then
Set p = wks.Range("D5:D70").SpecialCells(xlCellTypeConstants, 1)
Charts.Add
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=p, _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAsObject, Name:=wks.Name
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = "Temps de Cycle"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = ""
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Takt"
End With
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = False
.HasAxis(xlValue, xlPrimary) = True
End With
  NbGraph = ActiveSheet.ChartObjects.Count 'compte le nombre de graphiques dans la feuille
'le nouveau graphique correspond à l'index le plus élevé
 ActiveSheet.ChartObjects(NbGraph).Activate
    ActiveChart.ChartTitle.Select
    ActiveSheet.ChartObjects(NbGraph).Name = "Takt"
    With ActiveSheet.Shapes("Takt")
        .Left = Range("e15").Left
        .Top = Range("e15").Top
    End With
    wks.Range("A1").Select
End If
Set p = Nothing
Next wks
End Sub
 

Staple1600

XLDnaute Barbatruc
Re : graphique avec des données sources variables

Re

La même mais à ma sauce (par pur désœuvrement) ;)
Code:
Sub cree_graph3()
Dim NbGraph As Byte, wks As Worksheet, Drang$, p As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each wks In ThisWorkbook.Worksheets
    If wks.Name Like "Line ##" Then
    Set p = wks.Range("D5:D70").SpecialCells(2, 1)
    Charts.Add: ActiveChart.ChartType = 4
    ActiveChart.SetSourceData Source:=p, PlotBy:=2: ActiveChart.Location Where:=2, Name:=wks.Name
    With ActiveChart
    .HasTitle = -1
    .ChartTitle.Characters.Text = "Temps de Cycle"
    .Axes(1, 1).HasTitle = -1: .Axes(1, 1).AxisTitle.Characters.Text = ""
    .Axes(2, 1).HasTitle = -1: .Axes(2, 1).AxisTitle.Characters.Text = "Takt"
    End With
    With ActiveChart
    .HasAxis(1, 1) = 0: .HasAxis(2, 1) = -1
    End With
      NbGraph = ActiveSheet.ChartObjects.Count 'compte le nombre de graphiques dans la feuille
    'le nouveau graphique correspond à l'index le plus élevé
     ActiveSheet.ChartObjects(NbGraph).Activate
        ActiveChart.ChartTitle.Select
        ActiveSheet.ChartObjects(NbGraph).Name = "Takt"
        With ActiveSheet.Shapes("Takt")
            .Left = [e15].Left: .Top = [e15].Top
        End With
        wks.Range("A1").Select
    End If
Set p = Nothing
Next wks
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

Discussions similaires

Réponses
1
Affichages
168
Réponses
0
Affichages
153

Statistiques des forums

Discussions
312 215
Messages
2 086 331
Membres
103 188
dernier inscrit
evebar