颜色图表列由单元格颜色错误

我正在尝试使用特定工作表上活动单元格的单元格颜色创build图表。 当使用下面提供的macros时,我发现只有一些分配的单元RGB颜色代码与图表匹配。 我不知道为什么有些颜色会匹配,有些则不会。 当我手动input颜色代码时,图表中显示正确的颜色。 有没有什么我离开这个macros或我需要采取额外的步骤?

我为这个项目使用Excel 2016。

Sub ColorChartColumnsbyCellColor() With Sheets("Sheet1").ChartObjects(1).Chart.SeriesCollection(1) Set vAddress = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1)) For i = 1 To vAddress.Cells.Count .Points(i).Format.Fill.ForeColor.RGB = ThisWorkbook.Colors(vAddress.Cells(i).Interior.ColorIndex) Next i End With End Sub 

您正在为RGB属性分配一个颜色索引。 颜色索引与红绿蓝无关。 此外,@Tim William's有一个观点:条件格式可能在你正在做的事情中扮演一个angular色。

试试这个代码,它将颜色属性分配给RGB属性:

 Sub ColorChartColumnsbyCellColor() With Sheets("Sheet1").ChartObjects(1).Chart.SeriesCollection(1) Set vAddress = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1)) For i = 1 To vAddress.Cells.Count 'Comment the line below and uncomment the next one to take conditional formatting into account. .Points(i).Format.Fill.ForeColor.RGB = vAddress.Cells(i).Interior.Color '.Points(i).Format.Fill.ForeColor.RGB = vAddress.Cells(i).DisplayFormat.Interior.Color Next i End With End Sub