Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

 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 data series based on cell color with VBA code

Color the chart with multiple data series based on cell color with VBA code


Color the chart with one data series based on cell color with VBA code


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 with VBA code

The above code only can be applied to one data series chart, if the 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.


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Lisa · 4 months ago
    Thank you for the codes! How would you add a conditional format when the format is already established?
  • To post as a guest, your comment is unpublished.
    Armin · 8 months ago
    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?
  • To post as a guest, your comment is unpublished.
    WILLIAN GUSMÃO · 8 months ago
    If i have more charts in my sheet? do you have another code?