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?

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

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!
officetab bottom
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.
    danie · 5 months ago
    If i have more charts in my sheet? do you have another code?
  • To post as a guest, your comment is unpublished.
    Santi · 5 months ago
    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.
  • To post as a guest, your comment is unpublished.
    Nat · 8 months ago
    i cant make it work :( is this supposed to work on a stacked bar chart? Thanks
  • To post as a guest, your comment is unpublished.
    aubrey · 8 months ago
    This was an awesome start, but the colors of the chart and the cells don't always match. Any idea why?
  • To post as a guest, your comment is unpublished.
    Lisa · 1 years 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 · 2 years 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.
    GUSMAO · 2 years ago
    If i have more charts in my sheet? do you have another code?