Plage variable
Bonjour je débute en VBA et j'ai réalisé la macro suivante :
Sub test()
'
' test Macro
'
'
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""TEST 1"""
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$A$1:$A$19"
ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$D$1:$D$19"
ActiveChart.Location Where:=xlLocationAsNewSheet
End Sub
J'aimerais savoir comment rendre les parties 'Sheet1'!$A$1:$A$19 et 'Sheet1'!$D$1:$D$19 variable ... c'est à dire que la macro continuerais à fonction quelque soit le nombre de ligne présentes dans la sheet.
Merci pour votre aide
Bonjour et bienvenu(e)
A tester
Sub test()
'
' test Macro
'
Dim NbLigne As Long
NbLigne = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""TEST 1"""
'ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$A$1:$A$19"
'ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$D$1:$D$19"
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$A$1:$A$" & NbLigne
ActiveChart.SeriesCollection(1).XValues = "='Sheet1'!$D$1:$D$" & NbLigne
ActiveChart.Location Where:=xlLocationAsNewSheet
End SubMerci ! J'en m'en vais de se pas essayer cette solution ! Un énorme merci pour cette rapide réponse !!
Bonne journée
Dans une folle envie d'aller plus loin j'ai testé avec un graphique avec 3 plages provenant de 3 sheets différentes mais j'ai un problème pour définir la variable au départ. Pourriez-vous m'aider ?
NbLigne = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
'
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""OBA IZT"""
'ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$B$4:$B$25"
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$B$4:$B$" & NbLigne
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=""TIN IZT"""
'ActiveChart.SeriesCollection(2).Values = "='Sheet2'!$B$4:$B$15"
ActiveChart.SeriesCollection(2).Values = "='Sheet2'!$B$4:$B$" & NbLign
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(3).Name = "=""Flow IZT"""
'ActiveChart.SeriesCollection(3).Values = "='Sheet3'!$B$4:$B$15"
ActiveChart.SeriesCollection(3).Values = "='Sheet3'!$B$4:$B$" & NbLign
'ActiveChart.SeriesCollection(3).XValues = "='Sheet1'!$A$4:$A$25"
ActiveChart.SeriesCollection(3).Values = "='Sheet1'!$A$4:$A$" & NbLign
ActiveChart.Location Where:=xlLocationAsNewSheet
End Sub
Je suis sûr qu'il manque pas grand chose mais je n'ai défini que nbligne par rapport à Sheet 1 donc normal que ça marche pas mais comment le résoudre telle est la question
Bonjour
Une solution : Dans la page "Sheet1" les colonnes A et B ont le même nombre de ligne
Sub test()
Dim Nbligne1 As Long, Nbligne2 As Long, Nbligne3 As Long
Nbligne1 = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
Nbligne2 = Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row
Nbligne3 = Sheets("Sheet3").Range("B" & Rows.Count).End(xlUp).Row
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""OBA IZT"""
'ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$B$4:$B$25"
ActiveChart.SeriesCollection(1).Values = "='Sheet1'!$B$4:$B$" & Nbligne1
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(2).Name = "=""TIN IZT"""
'ActiveChart.SeriesCollection(2).Values = "='Sheet2'!$B$4:$B$15"
ActiveChart.SeriesCollection(2).Values = "='Sheet2'!$B$4:$B$" & Nbligne2
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(3).Name = "=""Flow IZT"""
'ActiveChart.SeriesCollection(3).Values = "='Sheet3'!$B$4:$B$15"
ActiveChart.SeriesCollection(3).Values = "='Sheet3'!$B$4:$B$" & Nbligne3
'ActiveChart.SeriesCollection(3).XValues = "='Sheet1'!$A$4:$A$25"
ActiveChart.SeriesCollection(3).Values = "='Sheet1'!$A$4:$A$" & Nbligne1
ActiveChart.Location Where:=xlLocationAsNewSheet
End SubMerci Banzai !! Désormais je sais comment faire pour la prochaine fois
Sujets résolu !