数据变化时如何更新图表?
How to update chart when data is changing?
我有两列是我在图表中使用的信息,它会根据我选择的时间范围而变化。主要问题是列的长度可能不同,可以是 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
您应该使用 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. 将此代码放入您的 worksheet 模块(不是录制宏后出现的常规模块)。
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
我有两列是我在图表中使用的信息,它会根据我选择的时间范围而变化。主要问题是列的长度可能不同,可以是 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
您应该使用 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. 将此代码放入您的 worksheet 模块(不是录制宏后出现的常规模块)。
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