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

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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