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.


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.
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.
    Lisa · 10 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 · 1 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.
    WILLIAN GUSMÃO · 1 years ago
    If i have more charts in my sheet? do you have another code?