ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 仪表盘图表的VBA程序化

仪表盘图表的VBA程序化

作者:绿色风 分类: 时间:2022-08-18 浏览:75
楼主
raulerini
前不久看到刘万祥老师用EXCEL制作出了类似仪表盘的炫图,觉得非常震撼  

我仅仅是将纯技巧操作的部分进行了VBA程式化。
说明如下:
raulerini 根据刘万祥老师思路整理编写而成
主要改进之处:
1、不用建立辅助单元格区域,就能快速准确的生成仪表盘到指定单元格位置
2、仪表盘的刻度标签不用辅助插件来生成,且会随着两端极值的变动而变动
注意点:1、仅在2003版本中测试,2007以上高版本请自行修改
2、使用时注意活动单元格要为空白单元格

操作如下图:

 

代码:
  1. Sub addchart()
  2.     'On Error Resume Next
  3.     If Application.Version <> 11 Then MsgBox "此程序仅在2003版本下完成测试,请用2003打开!": Exit Sub
  4.     If Selection.Value <> "" Then Exit Sub
  5.     Dim mychart As Chart, zhibiao As Range, mybottom As Range, mytop As Range
  6.     Set rng = Application.InputBox("请选择数据区域的范围:" & vbCrLf & "数据按照[指标值],[最小值],[最大值]纵向排列", "选择数据", , , , , , 8)
  7.     Set zhibiao = rng(1)
  8.     Set mybottom = rng(2)
  9.     Set mytop = rng(3)
  10.     ThisWorkbook.Names.Add Name:="内圈标签", RefersTo:="={0;27;0;27;0;27;0;27;0;27;0;27;0;27;0;27;0;27;0;27;0;0;90}"
  11.     ThisWorkbook.Names.Add Name:="预警色带", RefersTo:="={270;18;54;18}"
  12.     ThisWorkbook.Names.Add Name:="指针", RefersTo:="=CHOOSE({1;2;3},(" & zhibiao.Address(1, 1) & "-" & _
  13.                                                  mybottom.Address(1, 1) & ")/(" & mytop.Address(1, 1) & "-" & mybottom.Address(1, 1) & ")*270,0,360-(" & zhibiao.Address(1, 1) & "-" & _
  14.                                                  mybottom.Address(1, 1) & ")/(" & mytop.Address(1, 1) & "-" & mybottom.Address(1, 1) & ")*270)"
  15.     For kk = 1 To 11
  16.         ThisWorkbook.Names.Add Name:="标签" & kk, RefersTo:="=round(" & mybottom.Address(1, 1) & "+(" & mytop.Address(1, 1) & "-" & _
  17.                                                           mybottom.Address(1, 1) & ")/10*" & kk - 1 & ",0)"
  18.     Next
  19.     ThisWorkbook.Names.Add Name:="指标值", RefersTo:="=" & zhibiao.Parent.Name & "!" & zhibiao.Address(1, 1)
  20.     Set mychart = ActiveSheet.ChartObjects.Add(ActiveCell.Left, ActiveCell.Top, 250, 250).Chart
  21.     With mychart
  22.         .ChartType = xlDoughnut
  23.         .HasLegend = False
  24.         With .SeriesCollection.NewSeries
  25.             .Values = "='" & ThisWorkbook.Name & "'!内圈标签"
  26.             .Name = "内圈标签"
  27.             .ApplyDataLabels ShowValue:=True
  28.             .Interior.ColorIndex = xlNone
  29.             .Border.LineStyle = xlNone
  30.         End With
  31.         With .SeriesCollection.NewSeries
  32.             .Values = "='" & ThisWorkbook.Name & "'!预警色带"
  33.             .Name = "预警色带"
  34.             For i = 2 To 4
  35.                 .Points(i).Interior.ColorIndex = xlNone
  36.                 .Points(i).Border.LineStyle = xlNone
  37.             Next
  38.         End With
  39.         With .SeriesCollection.NewSeries
  40.             .Values = "='" & ThisWorkbook.Name & "'!内圈标签"
  41.             .Name = "外圈标签"
  42.             .Border.Color = vbWhite
  43.             .Interior.ColorIndex = 15
  44.         End With
  45.         With .ChartGroups(1)
  46.             .FirstSliceAngle = 225
  47.             .DoughnutHoleSize = 65
  48.         End With
  49.         With .SeriesCollection(2).Points(1).Fill
  50.             .ForeColor.SchemeColor = 46
  51.             .BackColor.SchemeColor = 4
  52.             .TwoColorGradient msoGradientVertical, 1
  53.         End With
  54.         For j = 1 To 23
  55.             With .SeriesCollection(1).Points(j).DataLabel
  56.                 If j = 23 Then .Text = "='" & ThisWorkbook.Name & "'!指标值"
  57.                 If j Mod 2 Then
  58.                     .Text = "='" & ThisWorkbook.Name & "'!标签" & (j + 1) / 2
  59.                     .Font.Size = 9
  60.                 Else
  61.                     .Delete
  62.                 End If
  63.             End With
  64.         Next
  65.         .PlotArea.Interior.ColorIndex = xlNone
  66.         .PlotArea.Border.LineStyle = 0
  67.         .ChartArea.Interior.ColorIndex = xlNone
  68.         .ChartArea.Border.LineStyle = 0
  69.         With .SeriesCollection.NewSeries
  70.             .Values = "='" & ThisWorkbook.Name & "'!指针"
  71.             .ChartType = xlPie
  72.             .Explosion = 30
  73.             For k = 1 To 3
  74.                 .Points(k).Explosion = 0
  75.                 .Points(k).Interior.ColorIndex = IIf(k Mod 2, xlNone, 3)
  76.                 .Points(k).Border.ColorIndex = IIf(k Mod 2, xlNone, 3)
  77.             Next
  78.         End With
  79.         .SeriesCollection(1).Points(23).DataLabel.Top = .SeriesCollection(1).Points(23).DataLabel.Top - 45
  80.         .ChartGroups(2).FirstSliceAngle = 225
  81.         .Shapes.AddShape msoShapeOval, .PlotArea.Left + .PlotArea.Width / 2 - 5, .PlotArea.Top + .PlotArea.Height / 2 - 5, 12#, 12#
  82.     End With
  83. End Sub



纯图表的仪表盘 豪华版 .rar


2楼
zhashutiao
好东西啊,就是2010下不能运行。
3楼
bobbyhust
原来是你写的,这东西可以整合封装了
4楼
liu12345678jing
5楼
qiluyskb
致富经视频2014全集
致富经cctv7
致富经视频全集
致富经养殖
zhifubang.org
6楼
資料更新中……
蛮好玩的貌似

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap