Defined coloring of dynamic diagrams

WalmitAal

New Member
Joined
Nov 29, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I would like to color the diagrams "Result" automatically according to the specified color.
However, the diagrams should be based entirely on the arrays and adapt themselves completely dynamically.
--> For example, if I add "Spain", Spain should also be added in the color green in all charts, Spain should also
be added in the color green.
The chart type varies and is therefore not limited to a barchart and linechart. However, it does not matter whether existing charts are only colored or completely new ones are created.

I have already been able to create the following code using ChatGPT (I hope it is allowed for questions). However, this is only limited to non-dynamic areas.

The result should look something like this
1701238641630.png


In advance, many thanks for any help :)

VBA Code:
Sub FormatDiagramAsCellValue_adacoolio()
Dim targetNameRange As Range
Dim targetColorCodeRange As Range
Dim selectedChart As chartObject
Dim series As series
Dim cellName As Range
Dim cellColor As Range
Dim targetName As String
Dim targetColorCode As String
Dim chartName As String
' Disable updates to speed up execution
Application.ScreenUpdating = False
' Prompt for the range of names
On Error Resume Next
Set targetNameRange = Application.InputBox("Select range for names", Type:=8)
On Error GoTo 0
' Check if the user canceled the input or selected an invalid range
If targetNameRange Is Nothing Then
MsgBox "Canceled or invalid range selected for names.", vbExclamation
GoTo CleanupAndExit
End If
' Prompt for the range of color codes
On Error Resume Next
Set targetColorCodeRange = Application.InputBox("Select range for color codes", Type:=8)
On Error GoTo 0
' Check if the user canceled the input or selected an invalid range
If targetColorCodeRange Is Nothing Then
MsgBox "Canceled or invalid range selected for color codes.", vbExclamation
GoTo CleanupAndExit
End If
' Ask the user for the chart name
chartName = InputBox("Enter the chart name:")
' Check if a chart with the specified name exists
On Error Resume Next
Set selectedChart = ActiveSheet.ChartObjects(chartName)
On Error GoTo 0
If selectedChart Is Nothing Then
MsgBox "Chart with the specified name not found.", vbExclamation
GoTo CleanupAndExit
End If
' Iterate through all cells in the range for names
For Each cellName In targetNameRange
' Check if the cell is not empty
If Not IsEmpty(cellName.Value) Then
' Get the target name and target color code
targetName = cellName.Value
' Search for the target color code in the corresponding row in the range for color codes
On Error Resume Next
Set cellColor = targetColorCodeRange.Cells(cellName.Row - targetColorCodeRange.Rows(1).Row + 1, 1)
On Error GoTo 0
' Check if the target color code was found
If Not cellColor Is Nothing Then
' Get the target color code
targetColorCode = cellColor.Value
' Iterate through all data series in the selected chart
For Each series In selectedChart.chart.SeriesCollection
' Check if the name of the data series matches the target name
If InStr(1, series.Name, targetName, vbTextCompare) > 0 Then
' Set the fill color and line color of the data series according to the color code
series.Format.Fill.ForeColor.RGB = RGBFromHex(targetColorCode)
series.Format.Line.ForeColor.RGB = RGB(0, 0, 0) ' Black
series.Format.Line.Weight = 1 ' Width of the line
End If
Next series
End If
End If
Next cellName
CleanupAndExit:
' Enable updates
Application.ScreenUpdating = True
End Sub
Function RGBFromHex(hexCode As String) As Long
' Convert a hex code to an RGB color
Dim red As Integer, green As Integer, blue As Integer
red = CLng("&H" & Mid(hexCode, 2, 2))
green = CLng("&H" & Mid(hexCode, 4, 2))
blue = CLng("&H" & Mid(hexCode, 6, 2))
RGBFromHex = RGB(red, green, blue)
End Function




Financial_examples.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1CountryHex code for CountrySum Units soldØ ProfitSales per DayDateCanadaGermanyFranceMexicoUnited States of America
2Canada#BF00FF247428,525208,801.01.20141186256,5874935,11544720,71655822,91346026,5
3Germany#201C8F19994926377,601.06.20142725979,41611485,21647724,02210094,41323610,8
4France#FF00FF24247626914,401.12.20143959729,42283342,42389929,21633894,71731892,1
5Mexico#9932CC20332520768,001.03.2014811132,5479509,61559748,8946494,61789974,5
6United States of America#008B8B232627,521396,701.07.20142109549,31609549,81148065,1926957,92308798,1
7#32CD3201.08.2014952043,01046755,2779802,11078756,02007266,1
8#7CFC0001.09.2014938647,61255161,91753193,01022441,31429253,5
9#2E8B5701.10.20131229608,32555442,62390477,01166052,61954030,7
1001.02.20141482166,01347335,91537438,51597700,41332890,7
1101.09.2013939195,21095488,2821599,6646694,2981022,9
1201.10.20142215924,53421587,33379661,61855574,31503072,3
1301.11.20131419826,11718947,9985577,31616106,41526745,6
1401.12.20131587259,2857856,9935141,31325568,1662615,6
1501.04.20141593563,01394813,51332862,71026911,51616624,5
1601.05.2014783941,71317483,01042777,01116760,11949249,4
1701.11.2014952833,3617106,51123994,61123522,81566757,0
18
19
Tabelle1
Cell Formulas
RangeFormula
J1:N1J1=TRANSPOSE(UNIQUE(financials[Country]))
A2:A6A2=UNIQUE(financials[Country])
C2:C6C2=SUMIFS(financials[Units Sold],financials[Country],A2#)
D2:D6D2=AVERAGEIFS(financials[Profit],financials[Country],A2#)
I2:I17I2=UNIQUE(financials[Date])
J2:N17J2=SUMIFS(financials[ [ Sales] ],financials[Country],J1#,financials[Date],I2#)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
Country_a=Tabelle1!$A$2#C2:D2
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,215,096
Messages
6,123,074
Members
449,093
Latest member
ripvw

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