使用宏生成图形

时间:2013-01-03 00:32:51

标签: excel excel-vba vba

我目前正在使用此宏来生成图表,我从其他工作表中获取值。 如何通过在未检测到时添加系列来简化此操作sheet1在A列中没有任何输入。

        ActiveSheet.ChartObjects("CH 1").Activate
        ActiveChart.SeriesCollection.NewSeries.MarkerStyle = False
        ActiveChart.SeriesCollection(1).Name = "='Sheet1'!$A$2"
        ActiveChart.SeriesCollection(1).Values = "='data'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(2).Name = "='Sheet1'!$A$3"
        ActiveChart.SeriesCollection(2).Values = "='data (2)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(3).Name = "='Sheet1'!$A$4"
        ActiveChart.SeriesCollection(3).Values = "='data (3)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(4).Name = "='Sheet1'!$A$5"
        ActiveChart.SeriesCollection(4).Values = "='data (4)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(5).Name = "='Sheet1'!$A$6"
        ActiveChart.SeriesCollection(5).Values = "='data (5)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(6).Name = "='Sheet1'!$A$7"
        ActiveChart.SeriesCollection(6).Values = "='data (6)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(7).Name = "='Sheet1'!$A$8"
        ActiveChart.SeriesCollection(7).Values = "='data (7)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(8).Name = "='Sheet1'!$A$9"
        ActiveChart.SeriesCollection(8).Values = "='data (8)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(9).Name = "='Sheet1'!$A$10"
        ActiveChart.SeriesCollection(9).Values = "='data (9)'!$B$2:$B$400"
        ActiveChart.SeriesCollection.NewSeries
        ActiveChart.SeriesCollection(10).Name = "='Sheet1'!$A$11"
        ActiveChart.SeriesCollection(10).Values = "='data (10)'!$B$2:$B$400"

2 个答案:

答案 0 :(得分:1)

尝试这样的事情。你的问题有点不连贯,所以如果我误解了你的想法,请发表评论。

Dim wks as Worksheet
Set wks = Sheets("Sheet1")

If WorksheetFunction.Count(wks.Columns(1)) <> 0 Then

    'insert your code here

End If

答案 1 :(得分:0)

如果你发现我重复这个(见下文)一行,在我的图表中添加更多的值/系列。 我如何在那里与您的代码集成。

    ActiveChart.SeriesCollection(1).Name = "='Sheet1'!$A$2"
    ActiveChart.SeriesCollection(1).Values = "='data'!$B$2:$B$400"
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(2).Name = "='Sheet1'!$A$3"
    ActiveChart.SeriesCollection(2).Values = "='data (2)'!$B$2:$B$400"