创建VBA图表

时间:2016-02-17 13:06:44

标签: excel vba charts

我正处于问题的中间:我需要使用组合框值来搜索其他计划中的数据,当我找到相同的值时,在同一行中捕获一些单元格值并绘制它。有人能帮助我吗?我相信我的问题是使用图表创建一些循环,因为我需要验证此值是否大于0.01才能捕获它。 到目前为止的尝试:

XPath

我知道这没有多大意义。我执行了一个宏来看看如何使用图表,但我意识到这种方式我想做,不会工作。不知何故,我需要为我的图表选择一些单元格(它们不是顺序)的值,这将取决于ComboBox1选择。

我已经找到了如何用循环捕获范围,现在只需要输入图表:

Private Sub ComboBox1_Click()

x = 751
y = 1

If Sheets("Data").Cells(x, 7).Value = Me.ComboBox1.Value Then

Sheets("Análises").ChartObjects("Chart 3").Activate
ChartObjects("Chart 3").SeriesCollection.NewSeries
ChartObjects("Chart 3").SeriesCollection(1).Name = Sheets("Data").Cells(x, 7).Value

If Sheets("Data").Cells(x, 7 + y).Value > 0.01 Then

ActiveChart.SeriesCollection(1).Values = "=Data!$L$752,Data!$N$752,Data!$R$752,Data!$T$752"
ActiveChart.SeriesCollection(1).XValues = "=Data!$H$10"
ActiveChart.SeriesCollection(1).XValues = "=Data!$H$10,Data!$J$10,Data!$L$10,Data!$N$10"

End If

Else

x = x + 1

End If

End Sub

1 个答案:

答案 0 :(得分:0)

  

我相信我的问题是用图表创建一些循环

使用适当的对象变量:

Dim cObj as ChartObject
Dim cht as Chart

然后使用For/Next循环:

For each cObj in ...
    Set cht = cObj.Chart '## Chart is child of ChartObject
Next

像这样,也许(我不确定你需要循环或你可能需要的其他逻辑):

Dim cObj As ChartObject
Dim cht As Chart
Dim srs As Series
Dim x as Long
Dim y as Long

x = 751
y = 1

If Sheets("Data").Cells(x, 7).Value = Me.ComboBox1.Value Then

For each cht in Sheets("Análises").ChartObjects
    Set srs = cht.SeriesCollection.NewSeries
    srs.Name = Sheets("Data").Cells(x, 7).Value

    If Sheets("Data").Cells(x, 7 + y).Value > 0.01 Then

    '## NOTE: I suspect these ranges also need to change for each chart
    '         That is going to be better suited for a SEPARATE question.

    srs.Values = "=Data!$L$752,Data!$N$752,Data!$R$752,Data!$T$752"
    srs.XValues = "=Data!$H$10"
    srs.XValues = "=Data!$H$10,Data!$J$10,Data!$L$10,Data!$N$10"

    End If
Next

Else

x = x + 1

End If