Chart columns coloring automatically based on cell font colors in a row

deacat

New Member
Joined
May 5, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I'm a rookie in VBA and I have an Excel VBA issue that is driving me crazy. I have created dynamic charts that select their data using an offset formula and a defined range. I want to color the columns of the charts according to the second row of the sheet, but the numer of columns needs to come from the data of the charts. I want to apply the same macro to all of the charts in a sheet, all sheets of the workbook, if possible, that all have the same structure, but the number of columns varies.

I tried to write the code, but (obviously), it's not working:

VBA Code:
Sub ReColor()

    Dim cht As Chart
    Dim i As Integer
    Dim vntValues As Variant
    Dim s As String
    Dim j As Integer
    Dim s_new As Range
    Dim j_max As Integer
    Dim rColor As Range
    Dim xColumns As Long
    Dim ColumnLetter As String

j_max = ActiveSheet.ChartObjects.Count
    For j = 1 To j_max
        Set cht = ActiveSheet.ChartObjects(j).Chart
        With cht.SeriesCollection(1)
            Set s_new = ActiveSheet.Range(Split(Split(.Formula, ",")(1), "!")(1))
            xColumns = s_new.Columns.Count
            ColumnLetter = Split(Cells(1, xColumns + 1).Address, "$")(1)
    Set rColor = .Range("$B$2:" & ColumnLetter & "$2")
For i = 1 To xColumns
                .Points(i).Interior.Color = rgbBlack
                .Points(i).Format.Fill.BackColor.RGB = Range(rColor).Cells(i).Font.Color
          Next
        End With
    Next

End Sub

Any help would be highly appreciated. Thanks!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,216,100
Messages
6,128,827
Members
449,470
Latest member
Subhash Chand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top