sharathdude
05-19-2010, 06:32 AM
I have written code in Excel VBA to create XYscatter chart inside chartspace.The code contains a number of data series to be plotted inside the chartspace. The values for the data series are tobe read from the sheet1 of the workbook. The code is as follows.
Private Sub CommandButton1_Click()
ChartSpace1.Height = 282 * 1.75
ChartSpace1.Width = 318 * 1.75
Dim chConstants
'Dim xValues As Variant, yValues As Variant
Dim Spreadsheet1 As OWC10.Spreadsheet
' Set the data source of the chart to the Spreadsheet control.
Set Spreadsheet1 = CreateObject("OWC10.Spreadsheet")
Spreadsheet1.Range("A1", "Z1000") = Sheet1.Range("A1", "Z1000").Value
'Set c = ChartSpace1.Constants
Set ChartSpace1.DataSource = Spreadsheet1
Set chConstants = ChartSpace1.Constants
'Create a Xy scatter chart chart.
ChartSpace1.Charts(0).Type = chChartTypeScatterLine
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a2"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B2:B3"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C2:C3"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a3"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B16:B17"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C16:C17"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a4"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B18:B19"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C18:C19"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a5"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B20:B21"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C20:C21"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a6"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B22:B23"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C22:C23"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a7"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B24:B25"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C24:C25"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a8"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B26:B27"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C26:C27"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a9"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B29:B30"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C29:C30"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a10"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B32:B33"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C32:C33"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a11"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B35:B36"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C35:C36"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a12"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B38:B39"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C38:C39"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a13"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B41:B42"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C41:C42"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a14"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B44:B45"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C44:C45"
End Sub
Th problem with this code is when I click the command button, it plots only the last data series in the chartspace and not all the data series. I want all the data series to be plotted inside the chartspace when I click the PLOT button in the user form. Can some one correct my code. I am also attaching the excel file contaning the VB code for ur reference
Regards,
Sharath
Private Sub CommandButton1_Click()
ChartSpace1.Height = 282 * 1.75
ChartSpace1.Width = 318 * 1.75
Dim chConstants
'Dim xValues As Variant, yValues As Variant
Dim Spreadsheet1 As OWC10.Spreadsheet
' Set the data source of the chart to the Spreadsheet control.
Set Spreadsheet1 = CreateObject("OWC10.Spreadsheet")
Spreadsheet1.Range("A1", "Z1000") = Sheet1.Range("A1", "Z1000").Value
'Set c = ChartSpace1.Constants
Set ChartSpace1.DataSource = Spreadsheet1
Set chConstants = ChartSpace1.Constants
'Create a Xy scatter chart chart.
ChartSpace1.Charts(0).Type = chChartTypeScatterLine
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a2"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B2:B3"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C2:C3"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a3"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B16:B17"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C16:C17"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a4"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B18:B19"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C18:C19"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a5"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B20:B21"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C20:C21"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a6"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B22:B23"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C22:C23"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a7"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B24:B25"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C24:C25"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a8"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B26:B27"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C26:C27"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a9"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B29:B30"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C29:C30"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a10"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B32:B33"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C32:C33"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a11"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B35:B36"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C35:C36"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a12"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B38:B39"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C38:C39"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a13"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B41:B42"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C41:C42"
' Bind the series name to cell B1 in the first sheet of Spreadsheet1.
'ChartSpace1.Charts(0).SeriesCollection.Add
ChartSpace1.Charts(0).SetData chConstants.chDimSeriesNames, chConstants.chDataBound, "a14"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimXValues, chConstants.chDataBound, "B44:B45"
' Bind the values of the data series to cells B2:B28 in the first sheet of Spreadsheet1.
ChartSpace1.Charts(0).SeriesCollection(0).SetData chConstants.chDimYValues, chConstants.chDataBound, "C44:C45"
End Sub
Th problem with this code is when I click the command button, it plots only the last data series in the chartspace and not all the data series. I want all the data series to be plotted inside the chartspace when I click the PLOT button in the user form. Can some one correct my code. I am also attaching the excel file contaning the VB code for ur reference
Regards,
Sharath