在Excel 2010图表中更改默认颜色

时间:2015-07-08 00:43:54

标签: excel-vba charts excel-2010 vba excel

我有一个工作簿而不是动态生成图表,具体取决于所选的工作表,这是数据的来源。其中两个是系列,其中两个系列是条形图,其中一个系列的值高于和低于其中一个。

我希望条形图正值的颜色为绿色,负值为红色。无论我尝试了多少变化,当生成图表时,它们默认为标准颜色(烧焦的橙色和浅蓝色。)

生成图表后,如果直接编辑系列属性,我将填充颜色更改为红色和绿色没有问题。

我的代码:

ActiveSheet.Shapes.AddChart.Select
Application.ActiveChart.Parent.Name = "Chart1"
ActiveSheet.ChartObjects("Chart1").Activate
ActiveChart.PlotVisibleOnly = False

ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = ValueMin - 0.1
ActiveChart.Axes(xlValue).MaximumScale = ValueMax + 0.1    

ActiveChart.SeriesCollection(1).Select                   'Negative Series
ActiveChart.SeriesCollection(1).ChartType = xlColumnClustered
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 0, 0)                      'Red
    .Transparency = 0
    .Solid
End With

ActiveChart.SeriesCollection(2).Select                   'Positive Series
ActiveChart.SeriesCollection(2).ChartType = xlColumnClustered
With Selection.Format.Fill
    .Visible = msoTrue
    .ForeColor.RGB = RGB(0, 176, 80)                     'Green
    .Transparency = 0
    .Solid
End With

ActiveChart.SeriesCollection("3").Select
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 255, 0)                              'Yellow
   .Transparency = 0
End With
ActiveChart.SeriesCollection(4).Select                             'Signal
With Selection.Format.Line
    .Visible = msoTrue
    .ForeColor.RGB = RGB(255, 51, 204)                             'Pink
    .Transparency = 0
End With

有关如何击败默认颜色并使用所选颜色的任何想法? 感谢。

2 个答案:

答案 0 :(得分:0)

我写这段代码有点懒。因此为您提供伪代码(未经测试)。您可以自行决定测试和使用它...... 在生成图表后立即运行此代码。用户不会注意到后端的更改 - 如果您在开始时禁用屏幕更新并在结束时启用它。我没有看到您的图表生成代码。所以我根据你的问题写了一代又一代。

Option Explicit

Public Sub ColureMePlusMinus()
Dim ws as Worksheet  
Dim cht as ChartObject
Dim srs as Series
Dim i as Integer
Dim pcRed as Integer 'if you intend to use RGB, use Long
Dim ncGreen as Integer   

'--change as per your own sheet
Set ws  = ActiveWorkbook.Sheets("Sheet1")

pcRed = 4 
ncGreen = 3 

For Each cht In ws.ChartObjects
   For Each srs In cht.Chart.SeriesCollection
       With srs.Fill
        .Solid
        '--do the honours
       End With
   Next
Next

End Sub
  

将颜色归功于peltier

更新

根据OP的评论进行更新:

Changing Excel workbook's colour theme and How to change the default colors that Excel uses for chart series.

答案 1 :(得分:0)

有时最困难的解决方案是最平凡的。

ActiveChart.SeriesCollection(2).Select               
ActiveChart.SeriesCollection(2).ChartType = xlColumnClustered
With Selection.Format.Fill
...etc

应切换到:

ActiveChart.SeriesCollection(2).ChartType = xlColumnClustered
ActiveChart.SeriesCollection(2).Select               
With Selection.Format.Fill
...etc

使Format.Fill作用于SeriesCollection属性,而不是ChartType属性。 卫生署!

现在就像魅力一样。