在excel的实际使用过程中,任何常常会使用到各种各样的统计数据,我们都知道,Excel默认画图是根据数据排列的顺序确定各序列的颜色的,请看下面图1所示。
http://s10/middle/6cb3ff23g9cde21eb60e9&690
同样的数据,只是因为4个序列的排序方式不一样(前者是ABCD,后者是DABC),直接导致两边的颜色完全不一样,我们如何保证两边画的图各序列颜色一样呢?常见的操作是直接更改图表的序列颜色。是的!这样可以实现,但如果比较的序列有10个,20个呢?!!手动调整10种20种颜色,眼睛都会花掉,你一定希望能够自动更改颜色了!
利用VBA编程是解决这个问题是方便的途径了,下面是我写的代码,供各位参考参考。
Public Sub changeColor()
For
i = 1 To
Worksheets("sheet1").ChartObjects.Count//统计总共有几个图
Worksheets("sheet1").ChartObjects(i).Activate//激活一个图表
With
ActiveChart
For
j = 1 To .SeriesCollection.Count//获取当前图表中的序列数量
If
.SeriesCollection(j).Name = "A" Then //根据序列名称来判断
.SeriesCollection(j).Border.Color = RGB(255, 0, 0)
//根据RGB确定线条颜色
.SeriesCollection(j).MarkerBackgroundColor = RGB(255, 0, 0)
//更改数据标志的点的颜色
.SeriesCollection(j).MarkerForegroundColor = RGB(255, 0,
0)
End
If
If
.SeriesCollection(j).Name = "B" Then
.SeriesCollection(j).Border.Color = RGB(0, 255, 0)
.SeriesCollection(j).MarkerBackgroundColor = RGB(0, 255,
0)
.SeriesCollection(j).MarkerForegroundColor = RGB(0, 255,
0)
End
If
If
.SeriesCollection(j).Name = "C" Then
.SeriesCollection(j).Border.Color = RGB(0, 0, 255)
.SeriesCollection(j).MarkerBackgroundColor = RGB(0, 0,
255)
.SeriesCollection(j).MarkerForegroundColor = RGB(0, 0,
255)
End
If
If
.SeriesCollection(j).Name = "D" Then
.SeriesCollection(j).Border.Color = RGB(31, 95, 39)
.SeriesCollection(j).MarkerBackgroundColor = RGB(31, 95,
39)
.SeriesCollection(j).MarkerForegroundColor = RGB(31, 95,
39)
End
If
Next
End With
Next
运行后的效果如图2所示。
http://s4/middle/6cb3ff23g9cde60e4fd53&690
图2
程序运行后的结果
我们可以发现各序列在不同的图中都是同一个颜色了,如果不显示图例的话两个图就完全一样了。
有了这个以后汇报就不怕数据多了吧,哈哈!
加载中,请稍候......