1、首先在开发工具中打开VBA编辑器
2、在单元格区域当中输入一些内容作为例子
3、在VBA编辑器中插入模块
4、在模块当中输入如下代码,然后运行Sub 动态图表() '定义名称 Appl坡纠课柩ication.Intersect烫喇霰嘴(ActiveCell.CurrentRegion, ActiveCell.CurrentRegion.Offset(0, 1)).Select Selection.CreateNames Top:=True '添加数据有效性 With [a9].Validation .Delete .Add Type:=xlValidateList,AlertStyle:=xlValidAlertStop, Formula1:="=" & Intersect(Selection, Rows(1)).Address .IgnoreBlank=True .InCellDropdown=True End With '生成图表 ActiveCell.CurrentRegion.Select ActiveSheet.Shapes.AddChart.Select '修饰图表 With ActiveChart .SetSourceData Source:=ActiveCell.CurrentRegion, PlotBy:=xlColumns '设置数据源 .ChartType=xlColumnClustered '图表类型 .SetElement (msoElementDataLabelOutSideEnd) '显示标签.Legend.Delete '删除图例End WithEnd Sub
5、将光标置于数据区域中任意单元格,用快捷键Alt+F8调出运行宏窗口,然后单击“执行”按钮,工作表中生成一个图表,如图