mise en forme conditionnelle en vba

winkel

XLDnaute Nouveau
Bonjour,
j'ai plusieurs feuilles avec des mise en f.c. qui marchent très bien. mais dès que je fais des copier coller ou deplacement ca devient le foutoir complet.
Est-il possible d'ecrire ces mise en forme de sorte à ce que cela reste en place.

si les valeurs de "F5 à F34"
est supérieur à "F4" => mettre la cel en rouge et la valeur gras+blanc
est inférieur à "F4/2" => mettre la val. en rouge+gras

Merci à +
 

porcinet82

XLDnaute Barbatruc
Re : mise en forme conditionnelle en vba

Salut,

Essais de copier le code ci-dessous dans le module ThisWorkbook, comme ca, il devrait fonctionner quelque soit la feuille.

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Range("F5:F34")) Is Nothing Then
    If Target.Count = 1 Then
        If Target.Value <> "" Then
            If Target.Value > Range("F4") Then
                With Target
                    .Interior.ColorIndex = 3
                    .Font.ColorIndex = 2
                    .Font.Bold = True
                End With
            ElseIf Target.Value < (Range("F4") / 2) Then
                With Target
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 3
                    .Font.Bold = True
                End With
            Else
                With Target
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 0
                    .Font.Bold = False
                End With
            End If
        Else
            With Target
                .Interior.ColorIndex = xlNone
                .Font.ColorIndex = 0
                .Font.Bold = False
            End With
        End If
    End If
End If
End Sub

@+
 

winkel

XLDnaute Nouveau
Re : mise en forme conditionnelle en vba

merci porcinet,

Ca marche, mais dès que tu change la valeur "F4" la mise en forme ne fonctionne que si tu retapes les valeurs.

Et peut-on la mettre sur la feuille et non pas dans le workbook, car j'ai des mise en forme différentes pour chaque feuille

Merci
 

porcinet82

XLDnaute Barbatruc
Re : mise en forme conditionnelle en vba

Salut,

Essais de mettre ce code dans le module de la feuille concernée :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i%
If Not Intersect(Target, Range("F5:F34")) Is Nothing Then
    If Target.Count = 1 Then
        If Target.Value <> "" Then
            If Target.Value > Range("F4") Then
                With Target
                    .Interior.ColorIndex = 3
                    .Font.ColorIndex = 2
                    .Font.Bold = True
                End With
            ElseIf Target.Value < (Range("F4") / 2) Then
                With Target
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 3
                    .Font.Bold = True
                End With
            Else
                With Target
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 0
                    .Font.Bold = False
                End With
            End If
        Else
            With Target
                .Interior.ColorIndex = xlNone
                .Font.ColorIndex = 0
                .Font.Bold = False
            End With
        End If
    End If
ElseIf Not Intersect(Target, Range("F4")) Is Nothing Then
    For i = 5 To 34
        If Range("F" & i).Value <> "" Then
            If Range("F" & i).Value > Range("F4") Then
                With Range("F" & i)
                    .Interior.ColorIndex = 3
                    .Font.ColorIndex = 2
                    .Font.Bold = True
                End With
            ElseIf Range("F" & i).Value < (Range("F4") / 2) Then
                With Range("F" & i)
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 3
                    .Font.Bold = True
                End With
            Else
                With Range("F" & i)
                    .Interior.ColorIndex = xlNone
                    .Font.ColorIndex = 0
                    .Font.Bold = False
                End With
            End If
        Else
            With Range("F" & i)
                .Interior.ColorIndex = xlNone
                .Font.ColorIndex = 0
                .Font.Bold = False
            End With
        End If
    Next i
End If
End Sub

@+
 

Discussions similaires

Statistiques des forums

Discussions
312 508
Messages
2 089 139
Membres
104 047
dernier inscrit
bravetta