How to color chart based on cell color in Excel?
Normally, when you create a chart, the color of the column bar is the default. If you need to format filled color on each bar based on the cell colors as following screenshot shown, how could you solve it in Excel?
Color the chart with one or multiple data series based on cell color with VBA codes
Color the chart with one or multiple data series based on cell color with an amazing feature
Color the chart with one or multiple data series based on cell color with VBA codes
Color the chart with one data series based on cell color
With the following VBA code, you can quickly change the color of the chart which contains one data series based on the original cell values color, please do as this:
1. First, create a bar or column chart as following screenshot shown (Select data and click Insert > Insert Column or Bar Chart):
2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
3. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Color chart bars with one data series based on cell color:
Sub ColorChartColumnsbyCellColor()
'Updateby Extendoffice
Dim xChart As Chart
Dim I As Long, xRows As Long
Dim xRg As Range, xCell As Range
On Error Resume Next
Set xChart = ActiveSheet.ChartObjects("Chart 1").Chart
If xChart Is Nothing Then Exit Sub
With xChart.SeriesCollection(1)
Set xRg = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1))
xRows = xRg.Rows.Count
Set xRg = xRg(1)
For I = 1 To xRows
.Points(I).Format.Fill.ForeColor.RGB = ThisWorkbook.Colors(xRg.Offset(I - 1, 0).Interior.ColorIndex)
Next
End With
End Sub
Note: In the above code, Chart 1 is the chart name that you want to use, please change it to your own.
4. After pasting the above code, please press F5 key to run this code, and the color of the chart bars have been changed based on the original cell color, see screenshot:
Color the chart with multiple data series based on cell color
If your chart with multiple data series, please apply the following VBA code:
1. Please create the bar or column chart which contains multiple data series as following screenshot shown:
2. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
3. Click Insert > Module, and paste the following code in the Module window.
VBA code: Color chart bars with multiple data series based on cell color:
Sub CellColorsToChart()
'Updateby Extendoffice
Dim xChart As Chart
Dim I As Long, J As Long
Dim xRowsOrCols As Long, xSCount As Long
Dim xRg As Range, xCell As Range
On Error Resume Next
Set xChart = ActiveSheet.ChartObjects("Chart 1").Chart
If xChart Is Nothing Then Exit Sub
xSCount = xChart.SeriesCollection.Count
For I = 1 To xSCount
J = 1
With xChart.SeriesCollection(I)
Set xRg = ActiveSheet.Range(Split(Split(.Formula, ",")(2), "!")(1))
If xSCount > 4 Then
xRowsOrCols = xRg.Columns.Count
Else
xRowsOrCols = xRg.Rows.Count
End If
For Each xCell In xRg
.Points(J).Format.Fill.ForeColor.RGB = ThisWorkbook.Colors(xCell.Interior.ColorIndex)
.Points(J).Format.Line.ForeColor.RGB = ThisWorkbook.Colors(xCell.Interior.ColorIndex)
J = J + 1
Next
End With
Next
End Sub
4. Then press F5 key to run this code, the chart bars are filled with original cells color at once, see screenshot:
Notes:
1. In the above code, Chart 1 is the chart name that you want to use, please change it to your own.
2. This code also can be applied to a line chart.
Color the chart with one or multiple data series based on cell color with an amazing feature
By using the above codes, the colors of the chart will not always be matched with the cell color, to solve this problem, here, I will introduce a useful tool - Change Chart Color According to Cell Color of Kutools for Excel, with this handy feature, you can color the chart based on the cell color quickly and easily.
After installing Kutools for Excel, please do as this:
1. First, please insert the chart that you want to use, and then select the chart, then click Kutools > Charts > Chart Tools > Change Chart Color According to Cell Color, see screenshot:
2. And then, a prompt box is popped out, please click OK button.
3. Now, the chart you selected has been colored based on the cell colors as below screenshots shown:
Color the chart with one data series based on cell color
Color the chart with multiple data series based on cell color
Download and free trial Kutools for Excel Now !
More relative chart articles:
- Create A Bar Chart Overlaying Another Bar Chart In Excel
- When we create a clustered bar or column chart with two data series, the two data series bars will be shown side by side. But, sometimes, we need to use the overlay or overlapped bar chart to compare the two data series more clearly. In this article, I will talk about how to create an overlapped bar chart in Excel.
- Copy One Chart Format To Others In Excel
- Supposing there are multiple different types of charts in your worksheet, you have formatted one chart to your need, and now you want to apply this chart formatting to other charts. Of course, you can format others manually one by one, but this will waste much time, are there any quick or handy ways for you to copy one chart format to others in Excel?
- Highlight Max And Min Data Points In A Chart
- If you have a column chart which you want to highlight the highest or smallest data points with different colors to outstand them as following screenshot shown. How could you identify the highest and smallest values and then highlight the data points in the chart quickly?
- Create A Step Chart In Excel
- A step chart is used to show the changes happened at irregular intervals, it is an extended version of a line chart. But, there is no direct way to create it in Excel. This article, I will talk about how to create a step chart step by step in Excel worksheet.
- Create Progress Bar Chart In Excel
- In Excel, progress bar chart can help you to monitor progress towards a target as following screenshot shown. But, how could you create a progress bar chart in Excel worksheet?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!