Creat A EXCEL CHART WITH VBA
2008-02-04 12:33
411 查看
Excel VBA之生成图表
1 准备图表数据源
![]() Sub 图表作成() Dim myData As String, sh As String '定义变量myData, sh为字符型 myData = Selection.Address '设置变量myData为当前选取的单元格区域 sh = ActiveSheet.Name '设置变量sh为当前工作表的名称 Charts.Add '添加图表 ActiveChart.ChartType = xlColumnClustered '图表类型为柱形图 ActiveChart.SetSourceData Source:=Sheets(sh).Range(myData), PlotBy:=xlColumns '设置图表数据源为myData(当前选取的单元格区域),数据系列为数据源中的列。 ActiveChart.Location Where:=xlLocationAsObject, Name:=sh '设置图表的位置是当前工作表sh With ActiveChart '设置图表的各项参数 .HasTitle = True '有图表标题 .ChartTitle.Characters.Text = "Snood 图表标题" '图表标题的文字 .Axes(xlCategory, xlPrimary).HasTitle = True '有主要横坐标标题 .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "主要横坐标" '设置主要横坐标标题的文字 .Axes(xlValue, xlPrimary).HasTitle = True '有主要纵坐标标题 .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "主要纵坐标" '设置主要纵坐标标题的文字 End With '结束设置图表的参数 ActiveChart.ChartArea.Select '选取当前图表的绘图区 End Sub ======================================================== Sub 图表导出为图片() ActiveChart.Export ThisWorkbook.Path & "/" & Format(Now(), "yymmddhhmm") & ".gif", "gif" End Sub ActiveChart.Export指导出当前活动图表 ThisWorkbook.Path指当前工作簿的路径 Format(Now(), "yymmddhhmm")指格式为“年年月月日日时时分分”的当前时间,以此命名图片文件,一般不会有重复。 "gif"图片格式,后缀名,文件比较小。 =========================================================== Sub 指定数据系列() '编制一个循环分别为每个系列(点)指定数据区域(X,Y坐标) For i = 1 To ActiveChart.SeriesCollection.Count ActiveChart.SeriesCollection(ActiveChart.SeriesCollection.Count).Delete '一个循环,删除当前图表中的全部数据系列 Next For i = 2 To 7 '循环 ActiveChart.SeriesCollection.NewSeries '增加新的数据系列 ActiveChart.SeriesCollection(ActiveChart.SeriesCollection.Count).XValues = "=Sheet1!R" & i & "C1" '(XY散点图)设置数据系列的X值引用单元格 ActiveChart.SeriesCollection(ActiveChart.SeriesCollection.Count).Values = "=Sheet1!R" & i & "C2" '设置数据系列的Y值引用单元格 Next End Sub |
相关文章推荐
- 【转】Custom Menu Items Created on the fly With Excel VBA
- Excel 2007 Power Programming with VBA
- Excel ChartType 属性的说明帮助(VBA)
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- Developing Excel Utilities with VBA
- 翻译Excel 2013 Power Programming with VBA 第14章的前两部分
- Excel VBA 自動生成圖表(chart)
- 《Excel 2013 Power Programming with VBA》试读章节翻译
- Excel VBA to Interact with Other Applications
- Mastering Excel 2003 Programming with VBA
- Chart sizes with window frame在Excel 2007中也被抛弃了
- Developing Excel Utilities with VBA 章节翻译
- Programming Excel with VBA and .NET
- 翻译《Excel 2013 Power Programming with VBA》段落
- 《Excel 2013 Power Programming with VBA》部分章节译文
- Excel: How to run C# code behind with a click of a button on a Worksheet, without VBA code
- 《Excel 2013 Power Programming with VBA》 试译样文
- Get ColumnLetter by ColumnNumber in Excel with VBA
- EXCEL&VBA: 中国热力图 HeatMap of China
- Excel VBA 常用文件处理