Macro selection colonne VBA

Draesel -  
f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,
J'ai colonne avec uniquement des évaluations et j'aimerais changer la couleur de mes cellules selon la note, j'arrive à faire toute les conditions via If,... le problème que je ne sais pas selectionner la colonne qui m'intéresse ou enfin je pense.
Voici mon code :

'Color cell according to evaluation
Sub Color_evaluation(ByVal Target As Range)
        
    'Target column 8 or H, column of evaluation and put the condition
    
   If Target.Column = 8 Then
        
        'If the evaluation is under 5 then the color of the cell will be dark red
         If UCase(Target.value) < 5 Then
    
        Target.EntireColumn.Interior.ColorIndex = 30
        
        'If the evaluation is 5 or 6 then the color of the cell will be orange
        ElseIf UCase(Target.value) = 5 Or 6 Then
        
        Target.EntireColumn.Interior.ColorIndex = 53
        
        'If the evaluation is 10 then the color of the cell will be blue
        ElseIf UCase(Target.value) = 10 Then
        
        Target.EntireColumn.Interior.ColorIndex = 87
        
        'If the evaluation is 0 or 1 then the color of the cell will be red
        ElseIf UCase(Target.value) = 0 Or 1 Then
        
        'In other case, the cell will be white
        Target.EntireColumn.Interior.ColorIndex = 3
      
        Else
    
      Target.EntireColumn.Interior.ColorIndex = 0
      
        End If
  
  End If

End Sub


Mais celui-ci ne se s'execute pas ou il n'y a aucune modification sur la colonne, qqu peut m'aider ?

Merci d'avance

1 réponse

  1. f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   1 717
     
    Bonjour,

    Code a mettre dans le VBA de la feuille de la colonne a colorier, vous reprendrez les couleurs

    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 = Target.Value
    If Valeur < 5 Then 'If the evaluation is under 5 then the color of the cell will be black
    Target.EntireColumn.Interior.ColorIndex = 1
    ElseIf Valeur = 5 Or Valeur = 6 Then 'If the evaluation is 5 or 6 then the color of the cell will be cyan
    Target.EntireColumn.Interior.ColorIndex = 7
    ElseIf Valeur = 10 Then 'If the evaluation is 10 then the color of the cell will be blue
    Target.EntireColumn.Interior.ColorIndex = 5
    ElseIf Valeur = 0 Or Valeur = 1 Then ' 'If the evaluation is 0 or 1 then the color of the cell will be red
    Target.EntireColumn.Interior.ColorIndex = 3
    Else 'In other case, the cell will be white
    Target.EntireColumn.Interior.ColorIndex = 2
    End If
    End If
    End Sub
    0
    1. Draesel
       
      Merci de ta réponse, l'ennui est que cela colore toute la colonne et non uniquement la cellule. Merci quand même et bonnes fêtes !
      0
    2. f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   1 717
       
      Bonjour,

      Ben, c'est votre code donc vous coloriez la colonne, pour la cellule c'est avec ceci:

      comme j suppose que vous voulez que le couleur change sur changement de valeur

      Private Sub Worksheet_Change(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 = Target.Value
      If Valeur < 5 Then 'If the evaluation is under 5 then the color of the cell will be black
      Target.Interior.ColorIndex = 1
      ElseIf Valeur = 5 Or Valeur = 6 Then 'If the evaluation is 5 or 6 then the color of the cell will be cyan
      Target.Interior.ColorIndex = 7
      ElseIf Valeur = 10 Then 'If the evaluation is 10 then the color of the cell will be blue
      Target.Interior.ColorIndex = 5
      ElseIf Valeur = 0 Or Valeur = 1 Then ' 'If the evaluation is 0 or 1 then the color of the cell will be red
      Target.Interior.ColorIndex = 3
      Else 'In other case, the cell will be white
      Target.Interior.ColorIndex = 2
      End If
      End If
      End Sub
      0