dynamic的Excelgraphics

目前,我创build了四个不同的图表,只有在D5单元格的下拉框中调用时才会出现。 但是,我正在尝试创build一个dynamic图,根据单元格D5中的内容填充其数据。

这可能吗?

Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range ' The variable KeyCells contains the cells that will cause an alert when they are changed. Set KeyCells = Range("D5") If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then If Range("D5") = "Tremont" Then ActiveSheet.ChartObjects("Tremont").Visible = True ActiveSheet.ChartObjects("SaybrookPointe").Visible = False ActiveSheet.ChartObjects("21Fitzsimons").Visible = False ActiveSheet.ChartObjects("Mezzo").Visible = False ElseIf Range("D5") = "Saybrook Pointe" Then ActiveSheet.ChartObjects("Tremont").Visible = False ActiveSheet.ChartObjects("SaybrookPointe").Visible = True ActiveSheet.ChartObjects("21Fitzsimons").Visible = False ActiveSheet.ChartObjects("Mezzo").Visible = False ElseIf Range("D5") = "21 Fitzsimons" Then ActiveSheet.ChartObjects("Tremont").Visible = False ActiveSheet.ChartObjects("SaybrookPointe").Visible = False ActiveSheet.ChartObjects("21Fitzsimons").Visible = True ActiveSheet.ChartObjects("Mezzo").Visible = False ElseIf Range("D5") = "Mezzo" Then ActiveSheet.ChartObjects("Tremont").Visible = False ActiveSheet.ChartObjects("SaybrookPointe").Visible = False ActiveSheet.ChartObjects("21Fitzsimons").Visible = False ActiveSheet.ChartObjects("Mezzo").Visible = True End If End If End Sub 

 Private Sub Worksheet_Change(ByVal Target As Range) Dim KeyCells As Range ' The variable KeyCells contains the cells that will cause an alert when they are changed. Set KeyCells = Range("D5") If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then If Range("D5") = "Tremont" Then Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).XValues = Range(X_axis_values) Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).Name = "Tremont" Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).Values = Range(Y_axis_values) 'If a bar graph, with Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB= RGB(0,0,0) .Transparency = 0 .Solid End With ElseIf Range("D5") = "Saybrook Pointe" Then Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).XValues = Range(X_axis_values) Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).Name = "Saybrook Pointe" Activesheet.Chartobjects("Single_Dynamic_Chart").FullSeriesCollection(1).Values = Range(Y_axis_values) 'If a bar graph, with Selection.Format.Fill .Visible = msoTrue .ForeColor.RGB= RGB(0,0,0) .Transparency = 0 .Solid End With ElseIf Range("D5") = "21 Fitzsimons" Then 'Similarly like above cases, define the X-axis,the series name and the values. ElseIf Range("D5") = "Mezzo" Then 'Similarly like above cases, define the X-axis,the series name and the values. End If End If End Sub 

以下这些链接将为您提供更多关于如何在图表上工作的信息

导入到图表

图表系列元素

我没有看到这个问题。 但是你可以简化一下,避免对名字进行硬编码,你也可以节省一些时间:

 Dim ch As ChartObject For Each ch in ActiveSheet.ChartObjects ch.Visible = ch.Name = Range("D5").Value Next 

但是,您需要删除D5列表中名称的空格,让它们与图表名称完全相同