Affichage conditionnel + changement de valeur de cellule

Bonjour,

Je voudrais, sur une plage définie par l'utilisateur, appliquer un formatage couleur et un changement de valeur (nouveau code de classification) en fonction de la valeur présente dans chaque cellule de la plage.

Je ne suis pas arrivé à combiner les deux en une seule procédure.

Voici le code de la macro qui change la couleur de la cellule en fonction de sa valeur:

Sub CellColour()
Dim user As Range
On Error Resume Next
    Set user = Application.InputBox("Sélection de la plage :", Type:=8)
    On Error GoTo 0
    If user Is Nothing Then MsgBox "Sélection annulée"
    user.Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=-5"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=-3", Formula2:="=-5"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=-2", Formula2:="=-3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 49407
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=-1,645", Formula2:="=-2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=-1,28", Formula2:="=-1,645"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65280
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=-1,28"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

Peut-on dans la même procédure changer la valeur de la cellule?

Sinon, j'ai essayé d'appliquer après le changement de couleur le changement de valeur en utilisant la "fonction" "For each cell in selection" mais la macro ci-dessous ne fonctionne pas. Seule la première cellule de la sélection est analysée et la valeur changée.

    For Each cell In user
    If ActiveCell.Value > -1.28 Then ActiveCell.Value = 0
    If ActiveCell.Value > -1.28 And ActiveCell.Value < -1.645 Then ActiveCell.Value = 1
    If ActiveCell.Value > -1.645 And ActiveCell.Value < -2 Then ActiveCell.Value = 2
    If ActiveCell.Value > -2 And ActiveCell.Value < -3 Then ActiveCell.Value = 3
    If ActiveCell.Value > -3 And ActiveCell.Value < -5 Then ActiveCell.Value = 4
    If ActiveCell.Value < -5 Then ActiveCell.Value = 5
    Next cell

Quelqu'un peut-il m'aider ?

Merci beaucoup

Uspa

Salut le forum

Pourquoi Activecell alors que tu boucles avec la variable cell

For Each cell In user
    If cell.Value > -1.28 Then cell.Value = 0
' . . . 

Mytå

Merci beaucoup pour votre réponse rapide.

Cela fonctionne correctement maintenant.

Du coup je me suis rendu compte que l'on ne pouvait pas changer la valeur dans le formatage conditionnel puisque celui-ci reste actif. Il faudrait copier la plage sans le formatage conditionnel puis appliquer le code de classification.

J'ai donc simplement inversé les deux actions: affectation d'un code puis d'une couleur.

Sub Code_Class_Color()
Dim user, cell As Range
On Error Resume Next
    Set user = Application.InputBox("Sélectionnez une cellule ou une plage :", Type:=8)
    On Error GoTo 0
    If user Is Nothing Then MsgBox "Sélection annulée"
    user.Select
    For Each cell In user
    If cell.Value > -1.28 Then cell.Value = 0
    If cell.Value < -1.28 And cell.Value > -1.645 Then cell.Value = 1
    If cell.Value < -1.645 And cell.Value > -2 Then cell.Value = 2
    If cell.Value < -2 And cell.Value > -3 Then cell.Value = 3
    If cell.Value < -3 And cell.Value > -5 Then cell.Value = 4
    If cell.Value < -5 Then cell.Value = 5
    Next cell
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=5"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=4"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = -0.249946592608417
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=3"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 49407
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=2"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65280
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

Encore merci pour votre analyse critique et rapide du code

uspa

Rechercher des sujets similaires à "affichage conditionnel changement valeur"