Home » excel » excel – Application-defined or object-defined error

excel – Application-defined or object-defined error

Posted by: admin May 14, 2020 Leave a comment


So I have a sheet called “Dashboard” that I have several charts on, but every week I’m adding data to the tables these charts are based on. The second sheet that has the data for this table is “Historical Totals” and the data I want is in columns A through E (this includes labels and headers). I’m trying to automate the charts updating, and I don’t quite know why this isn’t working. Here is the code I have so far:

ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Historical Totals").Range("A1", Range("E1").End(xlDown))

I’ve tried seperating our the range, and it is selecting the appropriate data, so I don’t really know where I’m making the mistake.

How to&Answers:

try the following. You need to reference the data sheet every time you use Range:

Sheets("Dashboard").ChartObjects("Chart 4").Activate
ActiveChart.SetSourceData _
        Source:=Sheets("Historical Totals").Range(Sheets("Historical Totals").Range("A1"), _
                Sheets("Historical Totals").Range("E1").End(xlDown))


Assuming you only have the table in the worksheet “Historical Totals” I would usually go xlUp rather than xlDown but the following is effectively the same as Philip A Barnes’s answer:

Sub setArea()

With Excel.ActiveWorkbook
        '.Sheets("Dashboard").Select '<==no need to select for the code to execute
        .Sheets("Dashboard").ChartObjects("Chart 1").Activate
End With

With Excel.ActiveWorkbook.Sheets("Historical Totals")
     Excel.ActiveChart.SetSourceData _
                      Source:=.Range( _
                                    .Cells(1, 1), _
                                    .Cells(.Cells(.Rows.Count, 5).End(Excel.xlUp).Row, 5) _
End With

End Sub