As soon as you run the myChart.Location statement, myChart is no longer
defined, because the chart sheet no longer exists. You would have to use
something like (untested)
Set myChart = myChart.Location(Where:=xlLocationAsObject, Name:="Sheet3")
or better yet, simply start by creating the chart object:
Set myChart = Worksheets("Sheet3").ChartObjects.Add({left, top, width,
height}).Chart
- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. -
http://PeltierTech.com
_______
"Ian" <(E-Mail Removed)> wrote in message
news:FDFA73C5-8EBE-48A0-A825-(E-Mail Removed)...
> Hello,
>
> I am starting to play with macro features in Excel. For testing purposes,
> I
> have created a table of random values and want to creat a macro that will
> plot a line chart of selected values. I've encountered an error and am
> asking if someone can explain why it is happening and how to correct the
> code. Sample code is provided below. The last line of the sample code
> (i.e.
> myChart.ChartType = XlChartType.xlLine) generates the error:
> run-time error '-2147221080 (800401a8): Automation error
>
> The code runs without problems if I replace the problem line with:
> ActiveChart.ChartType = XlChartType.xlLine
>
> My question is why does 'ActiveChart.ChartType ' work but not
> 'myChart.ChartType '?
>
> Thanks,
>
> Ian
>
>
>
>
>
>
>
> Public Sub TestChart()
>
> Dim myWorkBook As Workbook
> Set myWorkBook = ActiveWorkbook
>
> Dim strWorkSheet As String
> strWorkSheet = "Sheet3"
> Dim myWorkSheet As Worksheet
> Set myWorkSheet = myWorkBook.Worksheets(strWorkSheet)
>
> Dim ctGroups As Integer
> ctGroups = 2
>
> Dim ctGroupRows As Integer
> ctGroupRows = 10
>
> Dim myChart As Chart
> Set myChart = myWorkBook.Charts.Add
> myChart.Location Where:=xlLocationAsObject, Name:="Sheet3"
> myChart.ChartType = XlChartType.xlLine
>
>
> End Sub
>