Macro Forme conditionnelle pour 10 condition

GéGé la menace -  
Mike-31 Messages postés 18405 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,
je cherche comment changer la couleur d'une cellule en fonction de 10 conditions
avec une macro.
Si A7=0 Alors le Format de La Cellule est rouge
Si A7=1 " " " " " vert
Si A7=2 " " " " " jaune
Si A7=3 " " " " " orange
Si A7=4 " " " " " bleu
Si A7=5 " " " " " maron
Si A7=6 " " " " " noir
Si A7=7 " " " " " gris
Si A7=8 " " " " " rose
Si A7=9 " " " " " violet
Si A7=10 " " " " " blanc
Merci d'avance.
A+ GéGé
Configuration: Windows XP
Internet Explorer 7.0

3 réponses

  1. Mike-31 Messages postés 18405 Date d'inscription   Statut Contributeur Dernière intervention   5 147
     
    Salut,

    Je t'ai écrit le code pour le font de cellule et le texte, a toi de supprimer les ligne inutiles

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'condition 1
    If Range("A7") = 0 Then 'colonne A
    Range("A7").Font.ColorIndex = 3 'colonne A texte Rouge
    Range("A7").Interior.ColorIndex = 3 'colonne B cellule rouge
    'condition 2
    ElseIf Range("A7") = 1 Then 'colonne A
    Range("A7").Font.ColorIndex = 4 'colonne A texte vert
    Range("A7").Interior.ColorIndex = 4 'colonne B cellule vert
    'condition 3
    ElseIf Range("A7") = 2 Then 'colonne A
    Range("A7").Font.ColorIndex = 6 'colonne A texte jaune
    Range("A7").Interior.ColorIndex = 6 'colonne B cellule jaune
    'condition 4
    ElseIf Range("A7") = 3 Then 'colonne A
    Range("A7").Font.ColorIndex = 46 'colonne A texte orange
    Range("A7").Interior.ColorIndex = 46 'colonne B cellule orange
    'condition 5
    ElseIf Range("A7") = 4 Then 'colonne A
    Range("A7").Font.ColorIndex = 5 'colonne A texte bleu
    Range("A7").Interior.ColorIndex = 5 'colonne B cellule bleu
    'condition 6
    ElseIf Range("A7") = 5 Then 'colonne A
    Range("A7").Font.ColorIndex = 53 'colonne A texte marron
    Range("A7").Interior.ColorIndex = 53 'colonne B cellule marron
    'condition 7
    ElseIf Range("A7") = 6 Then 'colonne A
    Range("A7").Font.ColorIndex = 1 'colonne A texte noir
    Range("A7").Interior.ColorIndex = 1 'colonne B cellule noir
    'condition 8
    ElseIf Range("A7") = 7 Then 'colonne A
    Range("A7").Font.ColorIndex = 15 'colonne A texte gris
    Range("A7").Interior.ColorIndex = 15 'colonne B cellule gris
    'condition 9
    ElseIf Range("A7") = 8 Then 'colonne A
    Range("A7").Font.ColorIndex = 26 'colonne A texte rose
    Range("A7").Interior.ColorIndex = 26 'colonne B cellule rose
    'condition 10
    ElseIf Range("A7") = 9 Then 'colonne A
    Range("A7").Font.ColorIndex = 39 'colonne A texte violet
    Range("A7").Interior.ColorIndex = 39 'colonne B cellule violet
    'condition 11
    ElseIf Range("A7") = 10 Then 'colonne A
    Range("A7").Font.ColorIndex = 2 'colonne A texte blanc
    Range("A7").Interior.ColorIndex = 2 'colonne B cellule blanc
    End Sub

    Bonne nuit
    0
    1. GéGé la menace
       
      Merci Beaucoup Mike.
      Mais je suis désolé j'ai mal formulé ma question,( déjà ca, ca me convient très bien), mais comment je peut faire pour appliquer cette condition dans un tableau "classique" de 7 colonnes et 20 lignes par exemple ? sans faire copier coller et corriger dans Visual Basic pour chaque cellule du tableau.

      encors désolé

      A+ et Bonne Journée

      merci
      0
  2. michel_m Messages postés 18903 Date d'inscription   Statut Contributeur Dernière intervention   3 320
     
    Bonjour,

    D3:H13 est le tableau (pour exemple) , tu choisis les coordonnées dont tu as besoin
    pour les couleurs, je te joins un tableau que tu pourras utiliser à l'avenir...
    http://www.cijoint.fr/cjlink.php?file=cj200902/cijfrxTpNP.xls

    macro à terminer avec tes couleurs désirées (évite les " elself" lents et lourds)

    Sub colorier_tableau()
    Dim tablo As Range
    Set tablo = Range("D3:H13")
    With tablo
    Select Case Range("A7")
        Case Is = 0
            .Interior.ColorIndex = 3
        Case Is = 1
            .Interior.ColorIndex = 4
        'etc...
     End Select
     End With
     Set tablo = Nothing
    End Sub

    0
  3. Mike-31 Messages postés 18405 Date d'inscription   Statut Contributeur Dernière intervention   5 147
     
    Bonjour à vous,

    Pour appliquer les conditions à un tableau de plusieur lignes et colonnes,

    Commences par mettre ton tableau en surbrillance et tu le nommes, dans ce cas tu devras modifier le code

    Pour utiliser mon code en l'état nommes ton tableau Tableau en respectant la majuscule et colles ce code dans le code Feuille

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Tabl As Range
    Dim Cellule As Variant
    For Each Cellule In Range("Tableau")
    'condition 1
    If Cellule = 0 Then
    Cellule.Interior.ColorIndex = 3
    'condition 2
    ElseIf Cellule = 1 Then
    Cellule.Interior.ColorIndex = 4
    'condition 3
    ElseIf Cellule = 2 Then
    Cellule.Interior.ColorIndex = 6
    'condition 4
    ElseIf Cellule = 3 Then
    Cellule.Interior.ColorIndex = 46

    'condition 5
    ElseIf Cellule = 4 Then
    Cellule.Interior.ColorIndex = 5

    'condition 6
    ElseIf Cellule = 5 Then
    Cellule.Interior.ColorIndex = 53

    'condition 7
    ElseIf Cellule = 6 Then
    Cellule.Interior.ColorIndex = 1

    'condition 8
    ElseIf Cellule = 7 Then
    Cellule.Interior.ColorIndex = 15

    'condition 9
    ElseIf Cellule = 8 Then
    Cellule.Interior.ColorIndex = 26

    'condition 10
    ElseIf Cellule = 9 Then
    Cellule.Interior.ColorIndex = 39

    'condition 11
    ElseIf Cellule = 10 Then
    Cellule.Interior.ColorIndex = 2
    Next Cellule
    End Sub

    avec ce code, ton tableau sera rouge parce que vide est considéré comme 0 enfin testes le et on en reparle

    A+
    0