0

我需要帮助。我在 excel 上创建一个宏来执行不同的图表。我在图表的 VALUES 规范中收到一条错误消息。由于我需要创建几个具有不同 X 和 Y 值的类似图表,因此我通过一个字符串指定范围,其内容每次都会更新。我正在使用的表格称为“数据”和“图表”,这就是为什么您会在代码中看到这些名称。我在 Excel 2010 上使用 VBA。

这是代码。

Sub repetutive_charts()

Dim day As Integer
Dim row_in As Long
Dim row_end As Long

row_in = 3602
row_end = 90001

For day = 2 To day = 13
' Need to select an empty cell, like D1, otherwise the old graph is still selected and excel cannot create a new graph

Range("D1").Select
ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).name = "=Graph!$A$" & CStr(row_in)
    ActiveChart.SeriesCollection(1).Values = "=Data!$K$" & CStr(row_in) & ":$K$" & CStr(row_end)
    ActiveChart.SeriesCollection(1).Xvalues = "=Data!$I$" & CStr(row_in) & ":$I$" & CStr(row_end)

row_in = row_in + 5400
row_end = row_end + 5400

Next day
End Sub
4

1 回答 1

0

抱歉,我犯了语法错误,David Zemens 让我注意到了这一点。我刚开始用 VBA 编程,所以我不是语法专家......

Sub repetutive_charts()

Dim day As Integer
Dim row_in As Long
Dim row_end As Long

row_in = 3602
row_end = 90001

For day = 2 To 13
' Need to select an empty cell, like D1, otherwise the old graph is still selected and excel cannot create a new graph

Range("D1").Select
ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).name = "=Graph!$A$" & CStr(row_in)
    ActiveChart.SeriesCollection(1).Values = "=Data!$K$" & CStr(row_in) & ":$K$" & CStr(row_end)
    ActiveChart.SeriesCollection(1).Xvalues = "=Data!$I$" & CStr(row_in) & ":$I$" & CStr(row_end)

row_in = row_in + 5400
row_end = row_end + 5400

Next day
End Sub
于 2013-10-23T15:24:10.130 回答