Home » excel » vba – Excel 2010 Macro to Create Charts

vba – Excel 2010 Macro to Create Charts

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to create macros to build charts in Excel 2010.

If I record a macro it gives me the following code:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A$1:$I$15")
ActiveChart.ChartType = xlColumnStacked

But when I rerun it, it gives me the following error:

Run time error ‘1004’: Application defined or object defined error.

I have tried many different variations:

With ActiveSheet.ChartObjects.Add _
        (Left:=100, Width:=690, Top:=75, Height:=375)
    With .Chart
       .Parent.Name = "Chart MVO"
       .HasTitle = True
       .ChartTitle.Text = "MVO (628) CF and DS Delivery Performance"
    End With
 End With

But each gives a different run time error. What am I doing wrong?

How to&Answers:

Didn’t you rename your sheet? Are you sure that you have a sheet active? When I try your code it runs without problem.

When you run your code and it gives you an error, choose Debug in the window that appears and check what line it crashes on. Then hover your mouse over different variables on that line and see if all of them are valid.