Question : Adding a Chart to a UserForm using a Macro

In my macro, I am able to create a new Chart:
  Dim tempChart As Chart
  Set tempChart = ActiveWorkbook.Charts.Add
..assign a Range as data source, and use the .ChartWizard method to set up some formatting:
  tempChart.ChartWizard _
      Source:=cRg, _
      gallery:=xlColumn, _
      PlotBy:=xlRows, _
      serieslabels:=1,
..etc.  But I don't want my new Chart to be as a Sheet;  I want my Chart to display in a UserForm.

So I created a UserForm with a ChartSpace control, and proceeded to add a new chart that way.  I've been successful in getting a dummy chart to show up but I can't seem to use the .ChartWizard method with a chart on a UserForm (???), and I've been unable to figure out how to use the properties of the Chart object to correctly display my Range of data (which consists of two columns, where the first is Series Labels and the second is Values).

Can I assign the Chart in my ChartSpace on my UserForm *to* the Chart I've created normally?  i.e. can this ChartSpace.Chart "display" the chart that's already on my Workbook.Chart?
OR
How can I assign my Range of source data properly to a UserForm.ChartSpace.Chart?  The properties available to this control seem rather limited.

Any assistance would be greatly appreciated.. Thanks!

Answer : Adding a Chart to a UserForm using a Macro

The Chartspace object is entirely different from the Chart object that goes on a worksheet.

See the following reference

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/owcvba10/html/octocChartWorkspaceObjectModel.asp

One good thing with the Chartspace object is that you can assign data directly with arrays, something which isn't possible with certain types of chart

Name the object in the properies window and put the following code in the form_initialize event


With Myplot.Charts(0)  
    .PlotArea.Border.Color = "black"
    .HasTitle = True
    .Title.Caption = "My Plot Title"
    .Title.Font.Color = "white"
     .Title.Font.Bold = True
    .Type = chChartTypeScatterMarkers
    .Interior.Color = background_col
    .PlotArea.Interior.Color = background_col
    .Axes.Item(0).HasMajorGridlines = False
    'etc. etc.
end with
 
Then add data, which can either be a range of cells, database column, SQL result or array

You can add data to the chart like this. Put it on a form control along with something that maniplulates your data and you can dynamic plots controlled by the user

With MyPlot.Charts(0).SeriesCollection(0)
               .SetData chDimXValues, chDataLiteral, Myarray_x
               .SetData chDimYValues, chDataLiteral, Myarray_y  
End With
Random Solutions  
 
programming4us programming4us