Hi James,
The problem is that the use of Range("H" & LastDataRow) will fail
because the activesheet is a chart sheet not a worksheet.
So try this small modification.
..SetSourceData Source:= _
ThisWorkbook.Sheets("Sheet2").Range("C1", "H" & CStr(LastDataRow)), _
PlotBy:=xlColumns
Cheers
Andy
James Stephens wrote:
I have this macro basically done, I am having a problem with settign the source range for the chart though. I think it is simply a syntax error but I am not sure - any advice would be helpful as I am stuck. Here is the part of the code:
Dim LastDataRow As Long
LastDataRow = Cells(Rows.Count, "C").End(xlUp).Row
If LastDataRow = 1 Then GoTo MyErrorHandler
Else
Sheets("InputSheet").Visible = False
Sheets("Chart1").Visible = True
Sheets("Chart1").Select
With ActiveChart
.SetSourceData Source:=Workbooks("Main.xls").Sheets("DataSheet"). Range("C1", Range("H" & LastDataRow)), _
PlotBy:=xlColumns
.HasTitle = True
.ChartTitle.Characters.Text = "Chart"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Percentage"
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlValue, xlPrimary) = True
.Axes(xlCategory, xlPrimary).CategoryType = xlAutomatic
.HasLegend = False
.HasDataTable = True
.DataTable.ShowLegendKey = True
End With
End If
Exit Sub
MyErrorHandler:
'"Whole bunch of stuff here"
End Sub
I am getting the error "Mehtod 'Range' of Object '_Global' Failed" on the .SetSourceData command. And I get some problems with my "if" and "else" statements. What am I doing wrong - looking at it, it should work, I just don't know where I am going wrong.
Thanks for any advice,
Jim
--
Andy Pope, Microsoft MVP - Excel
http://www.andypope.info