Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeated calls of Chart.SetSourceData give error 1004

Tags:

excel

vba

charts

I have a problem with an application that was created in Excel 2003 in my company. The application retrieves data from a source and updates a Chart using the SetSourceData in a VBA routine passing a Range containing the cells where the relevant data is written.

The application runs just fine in Office 2003, but when the application is executed in Office 2010 it gives this error:

Run-time error '1004': Method 'SetSourceData' of object'_Chart' failed.

I have created a For loop in a simple Excel file in Office 2010 and depending on the number of columns passed in the Range to the Chart the error will come up sooner or later. The more columns passed in the Range the sooner it will come up. I guess this has to be related with the number of series in the Chart(more columns more series).

Is this some sort of mechanism/buffer in the Chart Object or Series implemented in Office 2010 that did not exist in Office 2003? The same For loop never shows a problem when it is run in Office 2003 and I am not sure how to solve this problem.

So far I have only been able to delete all the Series controlling the Error with a Goto instruction to delete all the series in the SeriesCollection using a For Each loop to select all the objects in the SeriesCollection of the Chart. If I do this and resume the execution of the application when I pass the Range again all the data is painted in the Chart Object properly.

Example to reproduce the error. The following code is to be put in a VBA module in a new Excel 2010 workbook. Run the Sub setDataChart and the application will run until the error message is displayed.

    Sub setDataChart()
    Call createAColValues
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlXYScatterSmoothNoMarkers
    ActiveChart.SetSourceData Source:=Range("A1:FA6"), PlotBy:=xlColumns
    ActiveSheet.ChartObjects(1).Activate
    With ActiveChart.Parent
         .Height = 325
         .Width = 900
         .Top = 120
         .Left = 10
    End With
    Call updateValues
    Call sendData
End Sub

    Sub sendData()
    Dim cht As ChartObject
    Set cht = ActiveSheet.ChartObjects(1)

    'On Error GoTo delSeries:
    For i = 0 To 1000
        cht.Chart.SetSourceData Source:=ActiveSheet.Range("A1:FA6"), PlotBy:=xlColumns
    Next i
End Sub

Sub createAColValues()
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "2"
    Range("A1:A2").Select
    Selection.AutoFill Destination:=Range("A1:A6"), Type:=xlFillDefault
    Range("A1:A6").Select
End Sub

Sub updateValues()
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "=RANDBETWEEN(0,10)"
    Range("B1").Select
    Selection.AutoFill Destination:=Range("B1:B6"), Type:=xlFillDefault
    Range("B1:B6").Select
    Selection.AutoFill Destination:=Range("B1:FA6"), Type:=xlFillDefault
    Range("B1:FA6").Select
End Sub
like image 850
Pocerus Avatar asked Oct 22 '14 08:10

Pocerus


People also ask

What is a 1004 error?

VBA 1004 Error is a runtime error in VBA which is also known as application-defined or object-defined error and why is that because we have limited number of columns in excel and when our code gives the command to go out of range we get 1004 error, there are other situations when we get this error when we refer to a ...

What is Visual Basic Run Time Error 1004?

Run-time error '1004' - Programmatic access to Visual Basic Project is not trusted (Mac) This error appears on Office 2016 or greater and Office 365 on the Mac version. This error is due to the addition of a security option in the Office options by Microsoft.


1 Answers

This doesn't address why the error occurs. This is a workaround.

Before calling SetSourceData, delete all the existing series currently in the chart, and the code will run as expected.

For j = cht.Chart.SeriesCollection.Count To 1 Step -1 
    cht.Chart.SeriesCollection(j).Delete
Next j

I'm not sure why the error occurs in the first place, but this makes it go away.

like image 99
Jean-François Corbett Avatar answered Oct 23 '22 14:10

Jean-François Corbett