Somme Couleur fond
Somme Couleur Fond avec couleur de référence
Somme Couleur fond/texte
Compte Couleur fond
Compte Couleur fond/Texte
Cacule la somme des cellules d'un champ ayant la couleur de fond spécifiée - Somme Couleur Fond -
Dans un module (Alt+F11 puis Insertion/Module)
Function SommeCouleurFond(champ As Range, couleurFond)
Application.Volatile
Dim c, temp
temp = 0
For Each c In champ
If c.Interior.ColorIndex = couleurFond Then
If IsNumeric(c.Value) Then temp =
temp + c.Value
End If
Next c
SommeCouleurFond = temp
End Function
-Lorsque la couleur de fond est modifiée, le résultat
n'est pas mis à jour immédiatement (sauf si on utilisé
le pinceau pour reproduide la mise en forme).
La MAj se fait lorsque lorsqu'un calcul est activé (option volatile)
-On peut utiliser F9 pour provoquer un calcul immédiat
ou utiliser l'événnement Selection_Change
Dim celluleAvant
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not IsEmpty(celluleAvant) Then
If Not Intersect(Range(celluleAvant), [B2:G3])
Is Nothing Then Calculate
End If
celluleAvant = Target.Address
End Sub
Afin d'éviter de connaître les numéros de
couleur, on passe à la fonction la cellule qui contient la couleur de
rérérence.
Les couleurs de référence sont définies dans les cellules
G1,F1,... - Fonction
Somme CouleurFondRef -
Dans un module:alt+F11 puis Insertion/Module
Function SommeCouleurFondRef(champ As Range, couleurFond As Range)
Application.Volatile
Dim c, temp
temp = 0
For Each c In champ
If c.Interior.ColorIndex = couleurFond.Interior.ColorIndex
Then
If IsNumeric(c.Value) Then temp = temp +
c.Value
End If
Next c
SommeCouleurFondRef = temp
End Function
Les no de couleurs sont donnés par la couleur de la cellule où la formule est écrite - Fonction Somme CouleurFondTexteRef -
Function SommeCouleurFondTexte2(champ As Range)
Dim c, temp
Application.Volatile
couleurFond = Range(Application.Caller.Address).Interior.ColorIndex
couleurTexte = Range(Application.Caller.Address).Font.ColorIndex
temp = 0
For Each c In champ
If c.Interior.ColorIndex = couleurFond And c.Font.ColorIndex
= couleurTexte Then
If IsNumeric(c.Value) Then temp =
temp + c.Value
End If
Next c
SommeCouleurFondTexte2 = temp
End Function
Function CompteCouleurFond(champ As Range, couleurfond)
Application.Volatile
Dim c, temp
temp = 0
For Each c In champ
If c.Interior.ColorIndex = couleurfond Then
temp = temp + 1
End If
Next c
CompteCouleurFond = temp
End Function
Autre version
Une cellule contient la couleur de référence. On utilise Color au lieu de ColorIndex (2007)
Function CompteCouleurFond2(champ As Range, couleurfond As Range)
Application.Volatile
Dim c, temp
temp = 0
cf = couleurfond.Interior.Color
For Each c In champ
If c.Interior.Color = cf Then
temp = temp + 1
End If
Next c
CompteCouleurFond2 = temp
End Function
Function CompteCouleurFondTexte(champ As Range)
Application.Volatile
couleurFond = Range(Application.Caller.Address).Interior.ColorIndex
couleurTexte = Range(Application.Caller.Address).Font.ColorIndex
Dim c, temp
temp = 0
For Each c In champ
If c.Interior.ColorIndex = couleurFond And c.Font.ColorIndex
= couleurTexte Then
temp = temp + 1
End If
Next c
CompteCouleurFondTexte = temp
End Function