Problem with chart

Sinem

Board Regular
Joined
Nov 8, 2015
Messages
63
Hello,

I have some datas on the sheet "Graphics" which I want to plot in the SAME Chart. But my code creates a new Chart for the new row by deletting the current Chart. How can I plot all my datas in the same Chart?


Function CreateLineCharts() As Boolean

Dim myChtObj As ChartObject
Dim rngChtData As Range
Dim rngChtXVal As Range
Dim iColumn As Long

Dim sheetName As String
sheetName = "DataSource"
Dim WSD As Worksheet
Set WSD = Worksheets("Graphics")

Dim chartSheet As String
chartSheet = "ChartOutput"
Dim CSD As Worksheet
Set CSD = Worksheets("Table3")

'get the current charts so proper overwriting can happen
Dim chtObjs As ChartObjects
Set chtObjs = CSD.ChartObjects
'turn off autofilter mode
WSD.AutoFilterMode = False
'Find the last row with data
Dim finalRow As Long
Dim i As Integer

finalRow = WSD.Cells(Application.Rows.Count, 2).End(xlUp).row


'add the chart
Charts.Add
With ActiveChart
'to determine how many values to loop over, find the last row in the data set
For i = 1 To finalRow
Dim chartName As String
chartName = WSD.Cells(i, 1).Value
'Delete Chart if it already exists, make a new one
Dim chtObj As ChartObject
For Each chtObj In chtObjs
If chtObj.Name = chartName Then
chtObj.Delete
End If
Next

'define chart data range for the row (record)
Dim dataString As String
dataString = "B" & i & ":P" & i
Set rngChtData = WSD.Range(dataString)

'define the x axis values
Set rngChtXVal = WSD.Range("$B$1:$P$1")


'make a Line chart
.ChartType = xlLineStacked

'remove extra series
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete

Loop

'add series from selected range, column by column
With .SeriesCollection.NewSeries
.Values = rngChtData
.XValues = rngChtXVal
.Name = "XX"
End With





Next i
End With

End Function
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,215,490
Messages
6,125,096
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top