Little bit vibecoded, not perfectly clean, and do not run on large sets.
Call ColoredBar with two arguments : a float value valx and a param range. Param range should follow this format (you could add rows to add configurations, and custom labels and colors, must always have 3 columns):

Option Base 1
Private Type tuple_paramx
labelx As String
valx As Double
colorx As String
End Type
Private Function extract_param_elements(valx As Variant, param_rng As Variant) As tuple_paramx 'tuple_paramx
Dim res As tuple_paramx
Dim lastCell As Range
For Each rowx In param_rng.Rows
curr_row = rowx.Value2
curr_val = curr_row(1, 2)
Set lastCell = rowx.Cells(rowx.Cells.Count)
If curr_val = valx Then
res.valx = valx
res.labelx = curr_row(1, 1)
res.colorx = lastCell.Interior.Color
extract_param_elements = res
Exit Function
End If
Next rowx
Dim res2 As tuple_paramx
res2.colorx = 1000000
res2.labelx = "PARAM INCONNU !"
res.valx = 0#
extract_param_elements = res2
End Function
Function ColoredBar(valx As Double, param_rng As Variant) 'As String
' Effectuer des calculs et déterminer la valeur à retourner
'ColoredBar = "Calculated Value" ' Remplacez par votre logique
Dim res As tuple_paramx
res = extract_param_elements(valx, param_rng)
Dim caller As Range
Set caller = Application.caller
' Obtenir la feuille de la cellule appelante
Dim callerSheet As Worksheet
Set callerSheet = caller.Worksheet
' Obtenir la ligne et la colonne de la cellule appelante
Dim callerRow As Long
Dim callerColumn As Long
callerRow = caller.Row
callerColumn = caller.Column
' Concaténer les quatre arguments en une seule chaîne avec un délimiteur (ex: "|")
Dim CombinedArgs As String
CombinedArgs = callerSheet.Name & "|" & callerRow & "|" & callerColumn & "|" & res.valx & "|" & res.colorx 'res.labelx & "|" & res.colorx
ColoredBar = res.labelx
' Appeler la sub en utilisant Evaluate avec la chaîne combinée
Dim ma_str As String
ma_str = "SetRectangularGradientx(""" & CombinedArgs & """)"
Application.Evaluate ma_str
End Function
Private Sub SetRectangularGradientx(CombinedArgs As String)
Dim Args() As String
Args = Split(CombinedArgs, "|")
' Vérifier que nous avons exactement 4 arguments
'If UBound(Args) < 3 Then
'MsgBox "Erreur : Nombre d'arguments insuffisant."
'Exit Sub
'End If
' Assigner les arguments à des variables distinctes
Dim curr_sh As String, curr_row As Double, curr_col As Double, valx As Double, colorx As String
curr_sh = Args(0)
curr_row = Args(1)
curr_col = Args(2)
valx = Args(3)
colorx = Args(4)
Dim curr_shx As Worksheet
Set curr_shx = ThisWorkbook.Worksheets(curr_sh)
Dim myRange As Range
Set myRange = curr_shx.Cells(curr_row, curr_col)
With myRange
.Font.Italic = True
End With
With myRange.Borders
.LineStyle = xlLineStyleNone
'.Weight = xlThin
'.ColorIndex = 1 ' Black color
End With
If valx < 1 Then
to_add = valx + 0.000001
With myRange.Interior
.Pattern = xlPatternLinearGradient
.Gradient.Degree = 0 ' Horizontal (gauche à droite)
.Gradient.ColorStops.Clear
' Vert à gauche (position 0 à 0.5)
With .Gradient.ColorStops.add(0)
.Color = colorx 'RGB(0, 255, 0) ' Vert pur
End With
'valx = 0.5
With .Gradient.ColorStops.add(valx)
.Color = colorx 'RGB(0, 255, 0) ' Vert pur jusqu'au milieu
End With
With .Gradient.ColorStops.add(to_add)
.Color = RGB(255, 255, 255) ' blanc pur à partir du milieu
End With
With .Gradient.ColorStops.add(1)
.Color = RGB(255, 255, 255) ' blanc pur
End With
End With
Else
to_add = 1
With myRange.Interior
.Color = colorx
End With
End If
End Sub