XL 2016 simplifier une macro enregistrée

  • Initiateur de la discussion Initiateur de la discussion dindin
  • Date de début Date de début

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 !

dindin

XLDnaute Occasionnel
Bonjour,
Comment faire pour simplifier cette macro enregistrée
VB:
Sub couleur()
'
' couleur Macro

    Range("D15:j200").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""CA"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent5
        .TintAndShade = 0
    End With
   
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""RTT"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
    End With
   
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=""CR"""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = 0
    End With
 
End Sub
c'est une liste de 7 choix, donc en MFC c'est trop long
 
Bonsoir Dinidin,
A essayer :
VB:
Sub Couleur()
    Set Plage = Range("D15:j200")
    Plage.FormatConditions.Delete
    Texte = "CA":   Fond = xlThemeColorAccent5: MFC Plage, Texte, Fond
    Texte = "RTT":  Fond = xlThemeColorAccent6: MFC Plage, Texte, Fond
    Texte = "CR":   Fond = xlThemeColorAccent3: MFC Plage, Texte, Fond
End Sub
Sub MFC(Plage, Texte, Fond)
    With Plage
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Texte
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).Interior.ThemeColor = Fond
    End With
End Sub
Pour rajouter une MFC il suffit de rajouter dans Couleur la ligne :
Code:
Texte = "TEXTE":   Fond = COULEUR: MFC Plage, Texte, Fond
 
Ou peut être plus simple, à vous de voir :
VB:
Sub Couleur2()
    Range("D15:J200").FormatConditions.Delete
    MFC2 "CA", xlThemeColorAccent5
    MFC2 "RTT", xlThemeColorAccent6
    MFC2 "CR", xlThemeColorAccent3
End Sub
Sub MFC2(Texte, Fond)
    With Range("D15:J200")
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Texte
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).Interior.ThemeColor = Fond
    End With
End Sub
 
Ou peut être plus simple, à vous de voir :
VB:
Sub Couleur2()
    Range("D15:J200").FormatConditions.Delete
    MFC2 "CA", xlThemeColorAccent5
    MFC2 "RTT", xlThemeColorAccent6
    MFC2 "CR", xlThemeColorAccent3
End Sub
Sub MFC2(Texte, Fond)
    With Range("D15:J200")
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:=Texte
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        .FormatConditions(1).Interior.ThemeColor = Fond
    End With
End Sub
Bonjour sylvanu,
Merci beaucoup
 
Bonjour le fil

Une autre syntaxe posssible
VB:
Sub test()
MFC_3 Range("D15:J20")
End Sub

Private Function MFC_3(Rng As Range)
Dim c
c = Array(Array("CA", 3), Array("RTT", 6), Array("CR", 12))
Rng.FormatConditions.Delete
For i = LBound(c) To UBound(c)
Rng.FormatConditions.Add Type:=1, Operator:=3, Formula1:=c(i)(0)
Rng.FormatConditions(i + 1).Interior.ColorIndex = c(i)(1)
Next
End Function
Faire les adaptations nécessaires pour Excel 2016.
(Car test effectué sur Excel 2003 et Excel 2003 ne connait pas cette syntaxe xlThemeColorAccent5)

@sylvanu
Si tu as le temps et la gentilesse de faire la transcription et le test sur un Excel plus récent, merci 😉
 
Bonjour le fil

Une autre syntaxe posssible
VB:
Sub test()
MFC_3 Range("D15:J20")
End Sub

Private Function MFC_3(Rng As Range)
Dim c
c = Array(Array("CA", 3), Array("RTT", 6), Array("CR", 12))
Rng.FormatConditions.Delete
For i = LBound(c) To UBound(c)
Rng.FormatConditions.Add Type:=1, Operator:=3, Formula1:=c(i)(0)
Rng.FormatConditions(i + 1).Interior.ColorIndex = c(i)(1)
Next
End Function
Faire les adaptations nécessaires pour Excel 2016.
(Car test effectué sur Excel 2003 et Excel 2003 ne connait pas cette syntaxe xlThemeColorAccent5)

@sylvanu
Si tu as le temps et la gentilesse de faire la transcription et le test sur un Excel plus récent, merci 😉
Encore merci @Staple1600
 
Re

@dindin
J'ai fait l'adaptation en me basant sur les infos de Microsoft
(mais pas pu testé)
Je te laisse tester
VB:
Sub test_B()
MFC_4 Range("D15:J20")
End Sub

Private Function MFC_4(Rng As Range)
Dim c
c = Array(Array("CA", 9), Array("RTT", 10), Array("CR", 7))
Rng.FormatConditions.Delete
For i = LBound(c) To UBound(c)
Rng.FormatConditions.Add Type:=1, Operator:=3, Formula1:=c(i)(0)
Rng.FormatConditions(i + 1).Interior.ThemeColor = c(i)(1)
Next
End Function
 
Re

@dindin
J'ai fait l'adaptation en me basant sur les infos de Microsoft
(mais pas pu testé)
Je te laisse tester
VB:
Sub test_B()
MFC_4 Range("D15:J20")
End Sub

Private Function MFC_4(Rng As Range)
Dim c
c = Array(Array("CA", 9), Array("RTT", 10), Array("CR", 7))
Rng.FormatConditions.Delete
For i = LBound(c) To UBound(c)
Rng.FormatConditions.Add Type:=1, Operator:=3, Formula1:=c(i)(0)
Rng.FormatConditions(i + 1).Interior.ThemeColor = c(i)(1)
Next
End Function
je viens de le tester sur Excel 2016, c'est excellent. Merci

1681295295228.png
 
- 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
9
Affichages
382
  • Question Question
Microsoft 365 comparaison texte
Réponses
5
Affichages
681
  • Question Question
Microsoft 365 colorer une plage
Réponses
2
Affichages
838
Réponses
2
Affichages
751
Réponses
22
Affichages
3 K
Réponses
4
Affichages
2 K
Réponses
8
Affichages
905
Retour