Excel应用-使用VBA自动绘制所有适用类型的Excel图表(代码及效果图)
来源:互联网 发布:网络赚钱项目 编辑:程序博客网 时间:2024/04/27 19:30
深夜,没有睡意。因为,再次发现VBA的魅力。
给出一组数据,如下:
可以绘制多少种Excel图表呢?
答案是以下70多种(Excel 2016实测):
这效果绝对炫酷,熬夜也值了。
以上Excel图表类型见下表,
VBA代码共享给有缘人:
Public Sub MonthlyCalc() Application.ScreenUpdating = False'定义用于循环的整型变量Dim Itemp As IntegerDim ChartTypeArray() As VariantDim ChartCount As Integer For Itemp = 1 To 12 '计算商品(甲)的销售额合计 Cells(4, Itemp + 2) = Cells(2, Itemp + 2) * Cells(3, Itemp + 2) '计算商品(乙)的销售额合计 Cells(7, Itemp + 2) = Cells(5, Itemp + 2) * Cells(6, Itemp + 2) '计算商品(丙)的销售额合计 Cells(10, Itemp + 2) = Cells(8, Itemp + 2) * Cells(9, Itemp + 2) '计算三种商品总的销售额合计 Cells(11, Itemp + 2) = Cells(4, Itemp + 2) + Cells(7, Itemp + 2) _ + Cells(10, Itemp + 2) Next Itemp 'ChartTypeArray = Array(xlLineMarkers, xlLineMarkersStacked, xlLineMarkersStacked100) 'ChartTypeArray = Array(63, 64, 65, 66, 67) '88, 89, 90, 91 excluded 'ChartTypeArray = Array(-4169, -4151, -4120, -4102, -4101, -4100, -4098, 1, 4, 5, 15, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87) 'ChartTypeArray = Array(92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112) ChartTypeArray = Array(-4169, -4151, -4120, -4102, -4101, -4100, -4098, 1, 4, 5, 15, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, _ 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, _ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112) ChartCount = 1 Do While (ChartCount <= (UBound(ChartTypeArray) + 1)) '添加图表 Charts.Add '定义图表类型 ActiveChart.ChartType = ChartTypeArray(ChartCount - 1) '图表数据源 ActiveChart.SetSourceData Source:=Sheets("chap5_2").Range( _ "A1:N1,A4:N4,A7:N7,A10:N10"), PlotBy:=xlRows '设置图表添加的位置 ActiveChart.Location Where:=xlLocationAsObject, Name:="charts2" With ActiveChart '使图表带有“标题” .HasTitle = True '设置图表“标题” .ChartTitle.Characters.Text = ChartTypeArray(ChartCount - 1) & "——月销售情况对比" '使图表带有X坐标标题 '.Axes(xlCategory, xlPrimary).HasTitle = True '设置图表X坐标标题 '.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "月份" '使图表带有Y坐标标题 ' .Axes(xlValue, xlPrimary).HasTitle = True '使图表带有Y坐标标题 ' .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "合计(元)" End With With ActiveChart.Parent ' .Left = 10 + 368 * (ChartCount - 1) '此处用以修改图表在工作表中的位置 ' .Top = 200 .Left = 10 + Int(ChartCount / 15) * 356 '此处用以修改图表在工作表中的位置 If (ChartCount Mod 15 <> 0) Then .Top = 222 * (ChartCount Mod 15) Else .Top = 222 End If End With ChartCount = ChartCount + 1 Loop Application.ScreenUpdating = TrueEnd Sub
阅读全文
0 0
- Excel应用-使用VBA自动绘制所有适用类型的Excel图表(代码及效果图)
- Excel VBA高效办公应用-第五章-销售数据的处理与分析-Part1(自动插入多张图表)
- Excel VBA高效办公应用-第二章-Excel VBA编辑基础-Part2(VBA的数据类型)
- 关于Excel Vba 自动生成代码的一些研究
- VBA的代码,在Excel启动时自动执行
- excel 中VBA的应用.
- vba 自动导入excel报表遇到的问题及解决方法
- Excel中的VBA常量和编码值所代表的标准图表类型
- Excel中的VBA常量和编码值所代表的标准图表类型
- Excel VBA练习:字典类型与正则表达式的使用
- Excel中VBA的使用
- excel中VBA的使用
- java绘制excel图表(jxcell)
- Excel使用心得(vba)
- 几个Excel自动阅卷的VBA(1)
- 经典Excel VBA代码
- 经典Excel VBA代码
- Excel VBA代码学习
- ARKit从入门到精通(5)-ARScnView介绍
- android中AsyncTask源码解析
- 简单理解PHP的面向对象编程方式
- ARKit从入门到精通(6)-ARSession介绍
- 卡尔曼滤波器优秀博文链接
- Excel应用-使用VBA自动绘制所有适用类型的Excel图表(代码及效果图)
- Giraph消息发送结构刨析
- ARKit从入门到精通(7)-ARCamera介绍
- R语言时间数据
- git使用二
- Windows MySQL 升级步骤和常见问题
- [JSON]2017年最新县及县以上行政区划代码
- Shortest Prefixes 【字典树 求给定字符串的 最短确定前缀】
- Spring Boot