Macro excel mise en forme conditionnelle

Estelle -  
Gyrus Messages postés 3360 Statut Membre -
Bonjour!!

J'aimerais mettre des mises en forme conditionnelle, voici ce que ça a donné:

Windows("12-05-2016_extraction.xlsx").Activate
Range("P12").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=SI(($F2<=AUJOURDHUI()-10)*($N2=0);VRAI;FAUX)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -3880704
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Windows("Macro Extraction de Base.xlsm").Activate

Windows("12-05-2016_extraction.xlsx").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AA2<AUJOURDHUI()"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Cells.FormatConditions.Delete
Range("A2:AB4000").Select
Range("T7").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AA2<AUJOURDHUI()"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

Windows("12-05-2016_extraction.xlsx").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AA2<=AUJOURDHUI()+7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 14286847
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Cells.FormatConditions.Delete
Range("A2:AB4000").Select
Range("P13").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AA2<=AUJOURDHUI()+7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 14286847
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A2:AB4000").Select
Range("P13").Activate
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AA13<AUJOURDHUI()"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = 0
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True

Seulement ça bug, certaines cases ne sont pas pris en compte et je n'arrive pas à interrompre une formule par rapport à une autre, ça ne veut pas le prendre dans la macro!

J'explique ce que je souhaite faire, il y a 3 formules conditionnelles que je souhaite appliquer, toutes s'applique à "=$A$2:$AB$4000";

1. =SI(($F2<=AUJOURDHUI()-10)*($N2=0);VRAI;FAUX)
mettre au format (texte en vert)
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

2. "=$AA2<AUJOURDHUI()
Mettre au format (remplissage en rouge)
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

3. =AA2<=AUJOURDHUI()+7 (mais prendre en compte d'abord le point 2!)
Mettre au format (remplissage jaune clair):
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 14286847
.TintAndShade = 0
End With

Est-ce que quelqu'un peut m'aider?

Merci à vous!

1 réponse

  1. Gyrus Messages postés 3360 Statut Membre 526
     
    Bonjour,

    A tester :
    Sub Test()
    Cells.FormatConditions.Delete
    With Workbooks("12-05-2016_extraction.xlsx").Worksheets("Feuil1").Range("A2:AB4000")

    .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($AA2<=AUJOURDHUI()+7;NON(ESTVIDE($AA2)))"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
    .PatternColorIndex = 0
    .Color = 14286847
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False

    .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($AA2<AUJOURDHUI();NON(ESTVIDE($AA2)))"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = False

    .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($F2<AUJOURDHUI()-10;$N2=0;NON(ESTVIDE($F2)))"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
    .Pattern = xlSolid
    .PatternColorIndex = 0
    .Color = 5296274
    .TintAndShade = 0
    .PatternTintAndShade = 0
    End With
    .FormatConditions(1).StopIfTrue = True

    End With
    End Sub

    A+
    0
    1. Estelle
       
      Bonjour,

      Merci de votre aide, cependant ça ne marche pas, ça me met:

      "Erreur de compilation:
      Référence incorrect ou non qualifiée"

      Et ça sélectionne
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      0
    2. Gyrus Messages postés 3360 Statut Membre 526 > Estelle
       
      Vérifie la ligne de code :
      With Workbooks("12-05-2016_extraction.xlsx").Worksheets("Feuil1").Range("A2:AB4000")

      As-tu adapté le nom de feuille ("Feuil1" dans l'exemple) ?

      Profites-en pour déplacer la ligne Cells.FormatConditions.Delete
      Sub Test()
      With Workbooks("12-05-2016_extraction.xlsx").Worksheets("Feuil1").Range("A2:AB4000")
      .Cells.FormatConditions.Delete
      .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($AA2<=AUJOURDHUI()+7;NON(ESTVIDE($AA2)))"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      With .FormatConditions(1).Interior
      .PatternColorIndex = 0
      .Color = 14286847
      .TintAndShade = 0
      .PatternTintAndShade = 0
      End With
      .FormatConditions(1).StopIfTrue = False

      .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($AA2<AUJOURDHUI();NON(ESTVIDE($AA2)))"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      With .FormatConditions(1).Interior
      .PatternColorIndex = xlAutomatic
      .Color = 255
      .TintAndShade = 0
      End With
      .FormatConditions(1).StopIfTrue = False

      .FormatConditions.Add Type:=xlExpression, Formula1:="=ET($F2<AUJOURDHUI()-10;$N2=0;NON(ESTVIDE($F2)))"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      With .FormatConditions(1).Interior
      .Pattern = xlSolid
      .PatternColorIndex = 0
      .Color = 5296274
      .TintAndShade = 0
      .PatternTintAndShade = 0
      End With
      .FormatConditions(1).StopIfTrue = True
      End With
      End Sub

      A+
      0
    3. Estelle > Gyrus Messages postés 3360 Statut Membre
       
      Bonjour,

      Je ne peux pas spécifié
      With Workbooks("12-05-2016_extraction.xlsx").Worksheets("Feuil1").Range("A2:AB4000")

      Car le nom de fichier va changer toutes les semaines.
      Une idée?

      merci.
      0
    4. Gyrus Messages postés 3360 Statut Membre 526 > Estelle
       
      Tu peux intégrer l'ouverture du classeur à ta macro.
      Sub Test()
      Dim résultatOK As Boolean
      Do While Not résultatOK
      résultatOK = Application.Dialogs(xlDialogOpen).Show
      If Not résultatOK Then
      MsgBox "vous devez choisir un fichier"
      End If
      Loop

      With ActiveWorkbook.Worksheets("Feuil1").Range("A2:AB4000")
      etc........
      0
    5. Estelle > Gyrus Messages postés 3360 Statut Membre
       
      Je ne veux pas devoir ouvrir une boite de dialogue.
      Avez-vous une autre solution?

      merci.
      Estelle
      0