Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

condition avec CASE

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

gerard55

XLDnaute Occasionnel
Boujour à tous
J'ai besoin d'un petit coup de main pour aller plus loin. A partir du code ci-dessous. Je voudrais mettre une coulleur non pas dans la colonne où il y a la condition mais dans les colonnes à droite de cette colonne soit dans la col D12, E12, f12, g12 (la condition serait en C pour mettre un format en D en E en F ou en G.
je n'arrive pas à trouver la syntaxe.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("c12:c100")) Is Nothing Then
With Target
Select Case Target.Value
Case Is = "AT"
.Interior.ColorIndex = 3
Case Is = "AM"
.Interior.ColorIndex = 44

End Select
End With
End If
End Sub
Merci pour votre aide
a+
 
Re : condition avec CASE

Bonsoir Gérard. bonsoir le fourm,

Essaie comme ça :
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C12:C100")) Is Nothing Then
    Select Case Target.Value
        Case Is = "AT"
            Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
    Case Is = "AM"
        Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 44
    End Select
End If
End Sub
 
Re : condition avec CASE

Bonsoir gerard55,
Essaie :
Code:
[COLOR=blue]Private Sub[/COLOR] Worksheet_Change([COLOR=blue]ByVal[/COLOR] Target [COLOR=blue]As[/COLOR] Range)
[COLOR=blue]If Not[/COLOR] Intersect(Target, Range("c12:c100")) [COLOR=blue]Is Nothing And[/COLOR] Target.Count = 1 [COLOR=blue]Then[/COLOR]
    [COLOR=blue]Select Case[/COLOR] Target.Value
        [COLOR=blue]Case Is[/COLOR] = "AT"
            Target.Interior.ColorIndex = 3
        [COLOR=blue]Case Is[/COLOR] = "AM"
            Target.Interior.ColorIndex = 44
    [COLOR=blue]End Select[/COLOR]
[COLOR=blue]End If[/COLOR]
[COLOR=blue]End Sub[/COLOR]
Cordialement

EDIT: Bonsoir Robert 🙂, en retard et pas vu la même chose ....
 
Dernière édition:
Re : condition avec CASE

Merci Robert et Elgé pour votre réponse. La solution de Elgé ne semble pas fonctionner. A l'inverse, la proposition de Robert fonctionne. Mais, à partir du code que j'ai adapté, quand le pas d'un critère à l'autre parfois la couleur ne se corrige pas.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C12:C100")) Is Nothing Then
Select Case Target.Value
Case Is = "A"
Range(Cells(Target.Row, 4), Cells(Target.Row, 4)).Interior.ColorIndex = 3
Case Is = "B"
Range(Cells(Target.Row, 4), Cells(Target.Row, 5)).Interior.ColorIndex = 3
Case Is = "C"
Range(Cells(Target.Row, 4), Cells(Target.Row, 6)).Interior.ColorIndex = 3
Case Is = "C"
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
Case Is = "C-1"
Range(Cells(Target.Row, 7), Cells(Target.Row, 6)).Interior.ColorIndex = 44

End Select
End If
End Sub
Gérard
 
Re : condition avec CASE

Re à tous,
Avant de "décrocher" une proposition (il est a noter que la demande n'est pas réalisable) :
Case Is = "C"
Range(Cells(Target.Row, 4), Cells(Target.Row, 6)).Interior.ColorIndex = 3
Case Is = "C"
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
Enfin bref,
Code:
[COLOR=blue]Private Sub[/COLOR] Worksheet_Change([COLOR=blue]ByVal[/COLOR] Target [COLOR=blue]As[/COLOR] Range)
[COLOR=blue]If Not[/COLOR] Intersect(Target, Range("c12:c100")) [COLOR=blue]Is Nothing And[/COLOR] Target.Count = 1 [COLOR=blue]Then[/COLOR]
    [COLOR=blue]Select Case[/COLOR] Target.Value
        [COLOR=blue]Case[/COLOR] "A"
            Target.Interior.ColorIndex = 3
        [COLOR=blue]Case[/COLOR] "B"
            Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
        [COLOR=blue]Case[/COLOR] "C"
            Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
        [COLOR=blue]Case[/COLOR] "C-1"
            Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = 3
        [COLOR=blue]Case Else[/COLOR]
            Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = xlNone
    [COLOR=blue]End Select[/COLOR]
[COLOR=blue]End If[/COLOR]
[COLOR=blue]End Sub[/COLOR]
Bon W.E à tous 😉
Cordialement
 
Dernière édition:
Re : condition avec CASE

Bonjour à tous,
Je reviens vers vous. Avec votre aide j'ai réussi à adapter les formats avec 'case'. Cependant, je voudrais que quand il n'y a rien d'inscrit dans la cellule "c" le format conditionnel s'efface. Or même si je met la condition cellule "c" vide, il conserve le format initial.
If Not Intersect(Target, Range("c12:c100")) Is Nothing And Target.Count = 1 Then
Select Case Target.Value
Case "A"
Range(Cells(Target.Row, 5), Cells(Target.Row, 7)).Interior.ColorIndex = xlNone
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.Pattern = xlSolid
Range(Cells(Target.Row, 4), Cells(Target.Row, 4)).Interior.ColorIndex = 33
Case ""
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = xlNone
Cette dernière ligne ne marche pas. Si je mets "0" là ça marche.
Merci pour votre aide
a+
 
Re : condition avec CASE

Merci Hasco pour ta réponse rapide. Cependant, ça ne fonctionne pas. Quand je marque "A" dans la colonne "C" ça indique le format souhaité. Mais quand j'efface la lettre "A" le format reste.
J'ai essayé les 2 codes ci-dessous. Ni l'un ni l'autre ne fonctionne.
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = xlColorIndexNone
Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).xlColorIndexNone
Merci pour vos suggestions
A+
 
Re : condition avec CASE

Salut

pour s'affranchir des "Case" :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("C12:C100")) Is Nothing And Target.Count = 1 Then
    Range(Cells(Target.Row, 3), Cells(Target.Row, 7)).Interior.ColorIndex = xlNone
    Target.Interior.ColorIndex = -33 * (Target = "A")
    Range(Cells(Target.Row, 4), Cells(Target.Row, 7)).Interior.ColorIndex = -3 * (Target = "B" Or Target = "C" Or Target = "C-1")
  End If
End Sub
mais pas des "Copier & Coller" (hors sujet).
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
43
Affichages
786
Réponses
9
Affichages
508
  • Question Question
Microsoft 365 Probléme VBA
Réponses
8
Affichages
588
Réponses
7
Affichages
366
Réponses
7
Affichages
448
Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…