VBA - Adding Charts

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
Hi, I'm having an issue where by the charts I am adding in VBA are occasionally formatting incorrectly.

I think the issue is being caused when the chart is initially added, as excel kindly guesses the information I would like to include, and makes multiple series.

Is there a way to prevent it from doing this?

Here is my code below

Code:
ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=ws7.Range(ws7.Cells(ComboBox2.ListIndex + 1, weekstart + 2), ws7.Cells(ComboBox2.ListIndex + 1, weekend + 2))
    ActiveChart.SeriesCollection(1).Name = "=""Resolved % (Cumulative)"""
    ActiveChart.SeriesCollection(1).XValues = ws7.Range(ws7.Cells(1, weekstart + 2), ws7.Cells(1, weekend + 2))
    ActiveChart.Axes(xlCategory).CategoryType = xlCategoryScale
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Values = ws5.Range(ws5.Cells(ComboBox2.ListIndex + 1, weekstart + 2), ws5.Cells(ComboBox2.ListIndex + 1, weekend + 2))
    ActiveChart.SeriesCollection(2).Name = "=""Responses"""
    ActiveChart.SeriesCollection(2).ChartType = xlColumnClustered
    ActiveChart.SeriesCollection(2).AxisGroup = 2
    ActiveChart.ChartTitle.Text = "Responses"
    ActiveChart.Parent.Name = "Chart2"
    ActiveChart.Axes(xlValue).MaximumScale = 1


    With ws1.ChartObjects("Chart2")
        .Top = ws1.Range("K13").Top
        .Left = ws1.Range("K13").Left
        .Width = ws1.Range("K13").Width * 7
        .Height = ws1.Range("K13").Heigth * 15
    End With

Where ws represents a worksheet. Weekstart is the first column in the dataset, weekend is the last, and ComboBox2.ListIndex + 1 is the row to look in - all these numbers come out as expected.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Not to worry, have resolved this myself.

I just added ws1.range("A1").Select before adding the chart
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,814
Members
452,945
Latest member
Bib195

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