Vba couleur cellule

Fermé
panda567 - Modifié par pijaku le 26/12/2014 à 11:04
eriiic Messages postés 24569 Date d'inscription mardi 11 septembre 2007 Statut Contributeur Dernière intervention 28 décembre 2023 - 26 déc. 2014 à 12:30
Bonjour,

Dans une feuille excel, j'ai une colonne nommée "evaluation " qui est la colonne H. Dans cette colonne j'ai les évaluations des différents disons reporter sur chaque ligne. Cette évaluation est sur et j'aimerais colorier le fonds de la cellule uniquement ( pas la colonne entière ) selon la nnote contenue dans la cellule. J'ai réussi les conditions sans problèmes mais je n'arrive pas à
1) Faire que cette macros traite uniquement ma colonne H ou 8
2) Selectionner les valeurs des cellules de toute la colonne pour ne pas devoir faire un code uniquement avec des conditions pour chaque ligne.
Voici mon code :
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Color cell according to evaluation
    'Target column 8 or H, column of evaluation and put the condition
   If Target.Column = 8 Then
        Valeur = Range.value
         If Valeur < 5 Then  'If the evaluation is under 5 then the color of the cell will be black
            Range.Interior.ColorIndex = 30
        ElseIf Valeur = 5 Or Valeur = 6 Then     'If the evaluation is 5 or 6 then the color of the cell will be cyan
            Range.Interior.ColorIndex = 46
        ElseIf Valeur = 10 Then      'If the evaluation is 10 then the color of the cell will be blue
            Range.Interior.ColorIndex = 3
        ElseIf Valeur = 0 Or Valeur = 1 Then        ' 'If the evaluation is 0 or 1 then the color of the cell will be red
            Range.Interior.ColorIndex = 3
        Else        'In other case, the cell will be white
            Range.Interior.ColorIndex = 2
        End If
    End If
End Sub


Qqu pourrait il m'aider svp ? :)

Merci d'avance et bonne fête !

2 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
25 déc. 2014 à 19:05
Bonjour,

Essaie cela

Option Explicit
Dim Valeur As Variant
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
For_X_to_Next_Ligne
End Sub
Sub For_X_to_Next_Ligne()
Dim FL1 As Worksheet, Cell As Range, NoCol As Integer
Dim NoLig As Long
Dim DernLigne As Long
DernLigne = Range("A1048576").End(xlUp).Row
    'Instance de la feuille qui permet d'utiliser FL1 partout dans
    'le code à la place du nom de la feuille
    Set FL1 = Worksheets("Feuil1")
 'Fixe le N° de la colonne à lire
    NoCol = 8
 'Utilisation du N° de ligne dans une boucle For ... Next
    For NoLig = 1 To DernLigne
        Valeur = FL1.Cells(NoLig, NoCol)
With FL1.Range("H" & NoLig)
If Valeur < 5 Then 'If the evaluation is under 5 then the color of the cell will be black
.Interior.ColorIndex = 30
 ElseIf Valeur = 5 Or Valeur = 6 Then 'If the evaluation is 5 or 6 then the color of the cell will be cyan
.Interior.ColorIndex = 46
 ElseIf Valeur = 10 Then 'If the evaluation is 10 then the color of the cell will be blue
.Interior.ColorIndex = 3
 ElseIf Valeur = 0 Or Valeur = 1 Then ' 'If the evaluation is 0 or 1 then the color of the cell will be red
.Interior.ColorIndex = 3
 Else 'In other case, the cell will be white
.Interior.ColorIndex = 2
 End If
 End With
 Next
    Set FL1 = Nothing
End Sub

0
eriiic Messages postés 24569 Date d'inscription mardi 11 septembre 2007 Statut Contributeur Dernière intervention 28 décembre 2023 7 212
26 déc. 2014 à 12:30
Bonjour,

Quelle version d'excel ?
Si >=2007 une simple MFC suffit.
eric
0