Skip to main content

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.

Note: To apply this Change Chart Color According to Cell Color, firstly, you should download the Kutools for Excel, and then apply the feature 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

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

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...

Description


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!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
For more than 1 Chart:



Sub CellColorsToChart()'Updateby Extendoffice
Dim xChart As Chart
Dim I As Long, J As Long, Y As Long
Dim xRowsOrCols As Long, xSCount As Long Dim xRg As Range, xCell As Range
On Error Resume Next

For Y = 1 To 100Set xChart = ActiveSheet.ChartObjects("Chart "&Y).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 WithNext
Next
End Sub

This comment was minimized by the moderator on the site
If i have more charts in my sheet? do you have another code?
This comment was minimized by the moderator on the site
It is ok. But... why simply not going to "Format Legend Entry" option in every data series in the chart, and go "Fill" and make it the color you want. I mean... all is ok, but a VBA code for this simple task is kinda overkill. But thanks. Appreciate you share it.
This comment was minimized by the moderator on the site
i cant make it work :( is this supposed to work on a stacked bar chart? Thanks
This comment was minimized by the moderator on the site
This was an awesome start, but the colors of the chart and the cells don't always match. Any idea why?
This comment was minimized by the moderator on the site
Thank you for the codes! How would you add a conditional format when the format is already established?
This comment was minimized by the moderator on the site
Thank you for these codes! This was exactly what I've been looking for with one detail not quite fitting. When I ran the code, the bar graphs colored in correctly but not legend which stayed unchanged. Is there a variation to the code that would include the legend? Or is there a way to match the Legend to the changes in the chart without a code?
This comment was minimized by the moderator on the site
If i have more charts in my sheet? do you have another code?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
Rate this post:
0   Characters
Suggested Locations