Adding a Series to a Chart

dcanham

Active Member
Joined
Jun 7, 2006
Messages
306
Below is a code chunk that is supposed to add a new chart then add a chart series as the sales numbers for that product are done totalling. It gets past the chart creation in the beginning. However, when I hit the

'Set cNewSeries = cNewChart.SeriesCollection.NewSeries'

line when adding the chart series it gives me a

'Runtime Error 438 Objectdoesn't support this property or method'

What am I doing wrong? I know what that error is supposed to mean, but I see nothing wrong with the way I'm using the object variables. This chart creation code is giving me fits!! Thanks in advance for any help!
Code:
    ' adding the chart
    Dim cNewSeries As Series
    Dim cNewChart As ChartObject
    Set cNewChart = ActiveSheet.ChartObjects.Add _
        (Left:=0, Width:=350, Top:=0, Height:=350)
    cNewChart.Chart.Location Where:=xlLocationAsObject, Name:="Chart"
    cNewChart.Chart.ChartType = sChartType
    
    ' loop for cycling through desired product codes
    nSeriesCounter = 0
    nRowCounter = 37
    nColumnCounter = 3
    For nCodeLoop = 1 To 8
        ' setting current code value
        sCurrentCode = Workbooks("Charts").Names("Code" & nCodeLoop).RefersToRange.Value
        
        ' testing to see if there a product code in that position
        If (sCurrentCode <> "") Then
            ' incrementing series counter
            nSeriesCounter = nSeriesCounter + 1
            
            ' starting loop to gather sales numbers
            For nSalesLoop = 0 To (nSeriesDataPoints - 1)
                ' getting total sales for this iteration
                nTotalSales = SUM_SALES_RANGE(sCurrentCode, nSeriesDivisor, (dStartDate + (nSeriesDivisor * nSalesLoop)))
               
                ' adding amount to cell on parameters worksheet
                Workbooks("Charts").Worksheets("Parameters").Cells((nSeriesCounter + nRowCounter), (nSalesLoop + nColumnCounter)).Value = nTotalSales
            Next nSalesLoop
            
            ' adding the series
            Set cNewSeries = cNewChart.SeriesCollection.NewSeries
            With cNewSeries
                .Name = sCurrentCode
                .Values = ActiveSheet.Range("C38,CY38")
                .XValues = ActiveSheet.Range("C39,CY39")
            End With
        End If
    Next nCodeLoop
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Something like this should work. Dave
Code:
 Dim Xvalues As Range, Yvalues As Range, ChartRange As Range
 Xvalues = ActiveSheet.Range("C39,CY39")
 Yvalues = ActiveSheet.Range("C38,CY38")
 Set ChartRange = ActiveSheet.Range(Xvalues, Yvalues)

 With cNewChart.Chart
 .SeriesCollection.Add Source:=ChartRange
 .Name = sCurrentCode
 End With

edit: whoops that added a series to an existing chart. Try this...

Code:
Dim Xvalues As Range, Yvalues As Range, ChartRange As Range
 Xvalues = ActiveSheet.Range("C39,CY39")
 Yvalues = ActiveSheet.Range("C38,CY38")
 Set ChartRange = ActiveSheet.Range(Xvalues, Yvalues)

With cNewChart.Chart
.SetSourceData Source:=ChartRange
.Name = sCurrentCode
End With
 
Upvote 0
Hmmmm, ok I'll give this a try when I get back to work. Why would the way I was doing it throw an error though? I think I'm missing some major theory on how charts work. In prior code, instead of the object variable, I used 'ActiveChart.'. It had issues with that as well. I thought it was due to the fact that I had gone to another workbook in code before that and it was no longer 'Active'. Your code seems to be simply a variant on the way I was doing it to begin with? :confused:
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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