数据更改时如何更新图表?

时间:2015-12-01 09:03:03

标签: excel vba excel-vba charts onchange

我有两列是我在图表中使用的信息,它根据我选择的时间范围而变化。主要问题是列长度可以不同,它可以是5行,当我选择另一段时间时,它可以是7行或更少,然后是5.图表中的数据是刷新的,但列数不是,例如,我有这样的数据:

 Tom 20
 Susan 30
 John 15

因此图表将有三列,它们的颜色根据第一列中的值而不同,但当我选择另一段时间时,数据会更改为:

 Peter 40
 Patrick 70
 Joe 36
 Megan 57
 Susan 74

现在,图表将包含三列,只有前三个名称:Peter,Patrick,Joe和列的颜色与第一个图表中的颜色相同。

我希望很容易理解我的问题,主要是图表的可视化并没有像它应该的那样改变。我添加了部分代码:

Sub Macro1()

    Dim MyRangex As Range
    Dim LastRow As Long
    Dim ChartRange1 As Range

    LastRow = Worksheets("Calculate").Cells(Rows.Count, "E").End(xlUp).row

    Set MyRangex = Worksheets("Calculate").Range("E2:E" & LastRow)
    Set ChartRange1 = Sheets("Calculate").Range("G2:G" & LastRow)

    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).XValues = MyRangex
    ActiveChart.SeriesCollection(1).Values = ChartRange1

    For i = 1 To Worksheets("Calculate").Cells(9, 10).value
        ActiveChart.SeriesCollection(1).Points(i).Select
        Select Case Worksheets("Calculate").Cells(i + 1, 5).value
            Case Is = "Tom"
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(255, 0, 0)
                    .Transparency = 0
                    .Solid
                End With
            Case Is = "Susan"
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(0, 176, 240)
                    .Transparency = 0
                    .Solid
                End With
            Case Is = "Joe"
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(255, 255, 0)
                    .Transparency = 0
                    .Solid
                End With
            Case Is = "John"
                With Selection.Format.Fill
                    .Visible = msoTrue
                    .ForeColor.RGB = RGB(191, 191, 191)
                    .Transparency = 0
                    .Solid
                End With
        End Select
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

您应该使用Worksheet_Change事件。它必须位于工作表模块中。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Me.Range("C2"), Target) Is Nothing Then
        MsgBox "data in C2 was changed"
    End If
End Sub

此示例中的MsgBox仅在单元格C2中的数据发生更改时才会显示。根据您更改新期间的方式,应更改此部分。

无论您是手动更新还是自动更新,我相信以下代码都适合您。 (除非这些名称下有公式。)

使用要求:
1。将此代码放在工作表模块中(不是录制宏后出现的常规模块)。
2。将您的图表重命名为" MyChartName"或在代码中将此名称替换为您的实际图表名称

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim MyRangex As Range
    Dim LastRow As Long
    Dim ChartRange1 As Range
    Dim i As Long
    Dim mySeries As Series

    If Not Intersect(Me.Range("E2:E100"), Target) Is Nothing Then

        LastRow = Worksheets("Calculate").Cells(Rows.Count, "E").End(xlUp).Row

        Set MyRangex = Worksheets("Calculate").Range("E2:E" & LastRow)
        Set ChartRange1 = Sheets("Calculate").Range("G2:G" & LastRow)
        Set mySeries = ActiveSheet.ChartObjects("MyChartName").Chart.SeriesCollection(1)

        mySeries.XValues = MyRangex
        mySeries.Values = ChartRange1

        For i = 1 To Worksheets("Calculate").Cells(9, 10).Value
            With mySeries.Points(i).Format.Fill
                Select Case Worksheets("Calculate").Cells(i + 1, 5).Value
                    Case Is = "Tom"
                        .Visible = msoTrue
                        .ForeColor.RGB = RGB(255, 0, 0)
                        .Transparency = 0
                        .Solid
                    Case Is = "Susan"
                        .Visible = msoTrue
                        .ForeColor.RGB = RGB(0, 176, 240)
                        .Transparency = 0
                        .Solid
                    Case Is = "Joe"
                        .Visible = msoTrue
                        .ForeColor.RGB = RGB(255, 255, 0)
                        .Transparency = 0
                        .Solid
                    Case Is = "John"
                        .Visible = msoTrue
                        .ForeColor.RGB = RGB(191, 191, 191)
                        .Transparency = 0
                        .Solid
                End Select
            End With
        Next
    End If
End Sub