I am doing some charting in Excel via VB.net and I am having an issue trying to create a new worksheet object for a chart I just created. When I am done creating the chart, I do the following to locate the chart on a new sheet:
chartPage.Location(XlChartLocation.xlLocationAsNewSheet, "Chart")
After doing this, I am then able to activate the new chart worksheet via:
Dim XLSheets As Sheets = xlWorkBook.Sheets
XLSheets("Chart").activate()
I then want to create a new Worksheet object for the new chart sheet I do:
Dim XLChartSheet As Excel.Worksheet
XLChartSheet = xlWorkBook.Sheets("Chart")
However, this throws an error:
Unable to cast COM object of type Microsoft.Office.Interop.Excel.ChartClass to interface type Microsoft.Office.Interop.Excel.Worksheet
I have also tried the code below and I get the same error
XLChartSheet = CType(xlWorkBook.ActiveSheet, Excel.Worksheet)
XLChartSheet = CType(XLSheets("Chart"), Excel.Worksheet)
I can create a new worksheet object for a non-chart sheet and it works fine. It appears to me that for some reason it doesn't think this chart sheet is a sheet.
Any suggestions?
What I have tried:
Here is more of my code:
Dim chartPage As Excel.Chart
Dim xlCharts As Excel.ChartObjects
Dim myChart As Excel.ChartObject
Dim R1 As Range, R2 As Excel.Range
Dim chartRange As Excel.Range
xlCharts = xlWorkSheet.ChartObjects
myChart = xlCharts.Add(250, 10, 500, 350)
chartPage = myChart.Chart
'Create a union range for the chart range
R1 = xlWorkSheet.Range(xlWorkSheet.Cells(1, 1), xlWorkSheet.Cells(LastCellRow, 2))
R2 = xlWorkSheet.Range(xlWorkSheet.Cells(1, 4), xlWorkSheet.Cells(LastCellRow, 4))
chartRange = xlApp.Union(R1, R2)
chartPage.SetSourceData(Source:=chartRange)
'Set the chart range to columns
chartPage.PlotBy = Excel.XlRowCol.xlColumns
'Change the chart type for each series
Dim ChartSeries As Excel.SeriesCollection = chartPage.SeriesCollection
ChartSeries.Item(1).ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue)
ChartSeries.Item(1).ChartType = XlChartType.xlColumnClustered
'Change the data label precision
If SelectedDownTimeChartUnits.ToUpper = "MINUTES" Then
ChartSeries.Item(1).DataLabels.numberformat = "0"
Else 'Hours
ChartSeries.Item(1).DataLabels.numberformat = "0.00"
End If
ChartSeries.Item(1).DataLabels.font.size = 7
'Create the 2nd series for the cumulative percentage
ChartSeries.Item(2).ApplyDataLabels(XlDataLabelsType.xlDataLabelsShowValue)
ChartSeries.Item(2).ChartType = XlChartType.xlLineMarkers
ChartSeries.Item(2).AxisGroup = XlAxisGroup.xlSecondary
'Format data labels
ChartSeries.Item(2).DataLabels.numberformat = "0.00%"
ChartSeries.Item(2).DataLabels.font.size = 7
ChartSeries.Item(2).DataLabels.position = Microsoft.Office.Interop.Excel.XlDataLabelPosition.xlLabelPositionAbove
'Move the legend
chartPage.Legend.Position = XlLegendPosition.xlLegendPositionBottom
'Add the main chart title
chartPage.HasTitle = True
chartPage.ChartTitle.Text = "Down Time Pareto for " & SelectedMachine & " From " & DatStart & " to " & DatEnd
'Add axis titles
XLAxisCategory = chartPage.Axes(, Excel.XlAxisGroup.xlPrimary)
XLAxisCategory.Item(XlAxisType.xlCategory).HasTitle = True
XLAxisCategory.Item(XlAxisType.xlCategory).AxisTitle.Text = "Down Time Reason"
XLAxisValue = chartPage.Axes(, XlAxisGroup.xlPrimary)
XLAxisValue.Item(XlAxisType.xlValue).HasTitle = True
XLAxisValue.Item(XlAxisType.xlValue).AxisTitle.Text = SelectedDownTimeChartUnits & " of Down Time"
'Add Secondary Axes title
XLSecondaryAxisValue = chartPage.Axes(, Excel.XlAxisGroup.xlSecondary)
XLSecondaryAxisValue.Item(XlAxisType.xlValue, XlAxisGroup.xlSecondary).HasTitle = True
XLSecondaryAxisValue.Item(XlAxisType.xlValue, XlAxisGroup.xlSecondary).AxisTitle.Text = "Cumulative %"
'AutoFit all the columns
xlWorkSheet.Columns.AutoFit()
'Move chart to separate sheet
chartPage.Location(XlChartLocation.xlLocationAsNewSheet, "Chart")