将单个单元格添加到循环中的图表范围时出现错误“1004”

时间:2013-07-08 14:48:42

标签: excel excel-vba vba

我尝试将单个单元格添加到图表中,以便只将符合特定条件的单元格添加到图表中。

ChartXDataString = "="
ChartFuelDriveDataString = "="

For i = 0 To 5 

    If Range("AP" & FirstDataRow + i) <> "0" Then

        If i = 5 Then
            ChartXDataString = ChartXDataString & "'RAW FleetData'!$A$" & (FirstDataRow + i)
            ChartFuelDriveDataString = ChartFuelDriveDataString & "'RAW FleetData'!$AP$" & (FirstDataRow + i)
        Else
            ChartXDataString = ChartXDataString & "'RAW FleetData'!$A$" & (FirstDataRow + i) & ";"
            ChartFuelDriveDataString = ChartFuelDriveDataString & "'RAW FleetData'!$AP$" & (FirstDataRow + i) & ";"
        End If
    End If
Next i

ActiveSheet.ChartObjects("gFMP_TotalFuelLiters").Activate
ActiveChart.SeriesCollection(1).XValues = ChartXDataString
ActiveChart.SeriesCollection(1).Values = ChartFuelDriveDataString

当我运行代码时,Excel会抛出以下错误:Run-time error '1004': Application-defined or object-defined error.

它就行:ActiveChart.SeriesCollection(1).XValues = ChartXDataString

即使我录制了一个添加单个单元格的宏,图表仍然有效,直到我运行录制的宏,它会抛出错误。

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

关闭以错误格式录制的宏录制器。 而不是像录音机那样, ChartXDataString =“='RAW FleetData'!$ A $ 2;'RAW FleetData'!$ A $ 3;'RAW FleetData'!$ A $ 4”, 正确的格式是 “=('RAW FleetData'!$ A $ 2,'RAW FleetData'!$ 3澳元,'RAW FleetData'!$ A $ 4)” 括号和逗号分隔符