Using ChartSpace and DataSource

JamesW

Well-known Member
Joined
Oct 30, 2009
Messages
1,197
Hi guys,

I'm trying to add a chart to a userform using the Microsoft Office Chart 9.0 control.

I have the following code, taken from a website, but I get a 'type mismatch' error at the red line:

Code:
Option Explicit

Private Sub UserForm_Initialize()
     
    'Sheet1.Visible = False
    With ChartSpace1
         ' Add a chart.
        .Charts.Add
         
         ' Set the data source of the chart to the Spreadsheet control.
        [COLOR=red][B]Set ChartSpace1.DataSource = Sheets("Sheet1").Range("H2:S3")
[/B][/COLOR]        
        With .Charts(0)
             ' Create a bar chart.
            .Type = chChartTypeBarClustered
             
             ' Add two data series to the chart.
            .SeriesCollection.Add
            .SeriesCollection.Add
             
             ' Set the properties of the first data series.
            With .SeriesCollection(0)
                .SetData chDimSeriesNames, 0, "B1"
                .SetData chDimCategories, 0, "A2:A5"
                .SetData chDimValues, 0, "B2:B5"
            End With
             
             ' Set the properties of the second data series.
            With .SeriesCollection(1)
                .SetData chDimSeriesNames, 0, "C1"
                .SetData chDimValues, 0, "C2:C5"
            End With
             
             ' Display the legend.
            .HasLegend = True
        End With
    End With
End Sub

How on earth do I set the data series to a range on a spreadsheet?

Cheers,

James
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You can't. You can bind an OWC chart to a spreadsheet control that is also on the form, but you can't bind it directly to an Excel spreadsheet.
 
Upvote 0
Bah, rubbish.

How would you suggest putting a chart on a form, or any other way of doing the following:

I have 10 items each with a seperate % running from Jan to Dec 2010. I want to add a button linked to each item which then shows a chart relating to the item's data.

Clicking on the first item shows the chart relating to it, then when you click on the second item it removes the first item's chart and shows the second items chart etc, etc..

Cheers,

James
 
Upvote 0
Either use the spreadsheet control too (though you should be aware that OWC is gone from 2007 onwards) or use an image control on the form and copy actual charts from your workbook to the form.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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