couleurs conditionnelles (VBA)

Anonyme
2018-03-06T16:02:28+00:00

Bonjour,

ai défini des couleurs conditionnelles de fond sur plusieurs cellules. Selon la couleur affichée je fais des calculs.

voici la fonction ColorIndexOfCF (qui a été développée par un membre de la communauté) qui donne souvent le bon résultat mais malheureusement pas toujours. Ai un pb lorsque la couleur est un rouge pur (RVB ; 255,0,0), la fonction me donne #VALEUR!

============================================================

Function ColorIndexOfCF(Rng As Range, Optional OfText As Boolean = False) As Integer

    Dim AC As Integer

    AC = ActiveCondition(Rng)

    If AC = 0 Then

        If OfText = True Then

            ColorIndexOfCF = Rng.Font.ColorIndex

        Else

           ColorIndexOfCF = Rng.Interior.ColorIndex

        End If

    Else

        If OfText = True Then

           ColorIndexOfCF = Rng.FormatConditions(AC).Font.ColorIndex

        Else

           ColorIndexOfCF = Rng.FormatConditions(AC).Interior.ColorIndex

        End If

    End If

End Function


Function ActiveCondition(Rng As Range) As Integer

    Dim Ndx     As Long

    Dim FC      As FormatCondition

    Dim Temp    As Variant

    Dim Temp2   As Variant

    If Rng.FormatConditions.Count = 0 Then

        ActiveCondition = 0

    Else

        For Ndx = 1 To Rng.FormatConditions.Count

            Set FC = Rng.FormatConditions(Ndx)

            Select Case FC.Type

                Case xlCellValue

                Select Case FC.Operator

                    Case xlBetween

                        Temp = GetStrippedValue(FC.Formula1)

                        Temp2 = GetStrippedValue(FC.Formula2)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) >= CDbl(FC.Formula1) And _

                               CDbl(Rng.Value) <= CDbl(FC.Formula2) Then

                               ActiveCondition = Ndx

                               Exit Function

                           End If

                       Else

                          If Rng.Value >= Range(Temp).Value And _

                             Rng.Value <= Range(Temp2).Value Then

                             ActiveCondition = Ndx

                             Exit Function

                          End If

                       End If

                    Case xlGreater

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) > CDbl(FC.Formula1) Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        Else

                           If Rng.Value > Range(Temp).Value Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                    Case xlEqual

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) = CDbl(FC.Formula1) Then

                               ActiveCondition = Ndx

                               Exit Function

                           End If

                        Else

                           If Rng.Value = Temp Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                    Case xlGreaterEqual

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) >= CDbl(FC.Formula1) Then

                               ActiveCondition = Ndx

                               Exit Function

                           End If

                        Else

                           If Rng.Value >= Range(Temp).Value Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                    Case xlLess

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                            If CDbl(Rng.Value) < CDbl(FC.Formula1) Then

                               ActiveCondition = Ndx

                               Exit Function

                            End If

                        Else

                            If Rng.Value < Range(Temp).Value Then

                               ActiveCondition = Ndx

                               Exit Function

                            End If

                        End If

                    Case xlLessEqual

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) <= CDbl(FC.Formula1) Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        Else

                           If Rng.Value <= Range(Temp).Value Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                    Case xlNotEqual

                        Temp = GetStrippedValue(FC.Formula1)

                        If IsNumeric(Temp) Then

                           If CDbl(Rng.Value) <> CDbl(FC.Formula1) Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        Else

                           If Rng.Value <> Range(Temp).Value Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                   Case xlNotBetween

                        Temp = GetStrippedValue(FC.Formula1)

                        Temp2 = GetStrippedValue(FC.Formula2)

                        If IsNumeric(Temp) Then

                           If Not (CDbl(Rng.Value) <= CDbl(FC.Formula1)) And _

                              (CDbl(Rng.Value) >= CDbl(FC.Formula2)) Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        Else

                           If Not Rng.Value <= Range(Temp).Value And _

                              Rng.Value >= Range(Temp2).Value Then

                              ActiveCondition = Ndx

                              Exit Function

                           End If

                        End If

                   Case Else

                        Debug.Print "UNKNOWN OPERATOR"

               End Select

            Case xlExpression

                If Application.Evaluate(FC.Formula1) Then

                   ActiveCondition = Ndx

                   Exit Function

                End If

            Case Else

                Debug.Print "UNKNOWN TYPE"

           End Select

        Next Ndx

    End If

    ActiveCondition = 0

End Function


Function GetStrippedValue(CF As String) As String

    Dim Temp        As String

    If InStr(1, CF, "=", vbTextCompare) Then

        If Right(CF, 2) = """""" Then

            Temp = Mid(CF, 3, Len(CF) - 3)

        Else

            Temp = Mid(CF, 3, Len(CF) - 2)

        End If

       If Left(Temp, 1) = "=" Then

           Temp = Mid(Temp, 2)

       End If

    Else

       Temp = CF

    End If

    GetStrippedValue = Temp

End Function


Private Function GetCFInteriorColor(rngCel As Range) As Long

' Initial version from someone else

    Dim strHcolor       As String

    ' Get Hex interior color as String

    strHcolor = Right("000000" & Hex(rngCel.DisplayFormat.Interior.Color), 6)

    ' Convert each Hex portion to a Long & pass them to the RGB function

    GetCFInteriorColor = RGB(CLng("&H" & Right(strHcolor, 2)), _

                             CLng("&H" & Mid(strHcolor, 3, 2)), _

                             CLng("&H" & Left(strHcolor, 2)))

End Function

=========================================================

Microsoft 365 et Office | Excel | Pour la maison | Windows

Question verrouillée. Cette question a été migrée à partir de la Communauté Support Microsoft. Vous pouvez voter pour indiquer si elle est utile, mais vous ne pouvez pas ajouter de commentaires ou de réponses ni suivre la question.

0 commentaires Aucun commentaire
Réponse acceptée par l’auteur de la question
DanielCo 107.1K Points de réputation
2018-03-10T21:46:42+00:00

Bonjour,

Apparemment, VBA comporte une anomalie dans Excel 2007. Pour la première condition (=""), c'est la 3e condition qui est reprise :

Le n° de la condition est donné par Ndx. Alors que Excel 2016 gère bien cette condition :

Compare Operator, Formula1 et Formula2 pour les deux versions.

Daniel

Cette réponse a-t-elle été utile ?

0 commentaires Aucun commentaire

25 réponses supplémentaires

  1. DanielCo 107.1K Points de réputation
    2018-03-06T18:11:35+00:00

    J'ai peut-être mal compris. J'ai 26/03/18 09:00 en B87 (remplissage rouge pétant) et 3 en B93.

    Je n'ai pas compris le reste des explications...

    Daniel

    Cette réponse a-t-elle été utile ?

    0 commentaires Aucun commentaire
  2. Anonyme
    2018-03-06T17:44:21+00:00

    le pb persiste.

    si tu peux voir, mon fichier est sur : https://we.tl/xOH59QBnHm


    explications fichier Service UR2

    Voir les cours jour J, J+1, J+2, J+I1

    à l'ouverture I1=0 (pour que J = aujourd'hui) et D1=8 (voir les cours de la semaine)

    en B87 ai cours le 26 mars, aujourd'hui sommes le 6 donc mettre 20 en I1

    (en C87 le 28 mars ; mettre 22 en I1...)

    si aujourd'hui + I1 =26 alors B87 sur fond rouge mais en B93  ai #VALEUR! au lieu de 3

    en cliquant sur la partie supérieure de D1 (photo AL2:AM6), récapitulatif du travail de J à J+I1

    légendes : effu=effectué - prés = présenciel - comp = complémentaires.

    Cette réponse a-t-elle été utile ?

    0 commentaires Aucun commentaire
  3. DanielCo 107.1K Points de réputation
    2018-03-06T16:46:49+00:00

    Essaie  :

    Function ActiveCondition(Rng As Range) As Integer

        Dim Ndx     As Long

        Dim FC      As FormatCondition

        Dim Temp    As Variant

        Dim Temp2   As Variant

        If Rng.FormatConditions.Count = 0 Then

            ActiveCondition = 0

        Else

            For Ndx = 1 To Rng.FormatConditions.Count

                Set FC = Rng.FormatConditions(Ndx)

                Select Case FC.Type

                    Case xlCellValue

                    Select Case FC.Operator

                        Case xlBetween

                            Temp = GetStrippedValue(FC.Formula1)

                            Temp2 = GetStrippedValue(FC.Formula2)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) >= Temp And _

                                   CDbl(Rng.Value) <= Temp2 Then

                                   ActiveCondition = Ndx

                                   Exit Function

                               End If

                           Else

                              If Rng.Value >= Range(Temp).Value And _

                                 Rng.Value <= Range(Temp2).Value Then

                                 ActiveCondition = Ndx

                                 Exit Function

                              End If

                           End If

                        Case xlGreater

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) > CDbl(FC.Formula1) Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            Else

                               If Rng.Value > Range(Temp).Value Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                        Case xlEqual

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) = CDbl(FC.Formula1) Then

                                   ActiveCondition = Ndx

                                   Exit Function

                               End If

                            Else

                               If Rng.Value = Temp Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                        Case xlGreaterEqual

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) >= CDbl(FC.Formula1) Then

                                   ActiveCondition = Ndx

                                   Exit Function

                               End If

                            Else

                               If Rng.Value >= Range(Temp).Value Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                        Case xlLess

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                                If CDbl(Rng.Value) < CDbl(FC.Formula1) Then

                                   ActiveCondition = Ndx

                                   Exit Function

                                End If

                            Else

                                If Rng.Value < Range(Temp).Value Then

                                   ActiveCondition = Ndx

                                   Exit Function

                                End If

                            End If

                        Case xlLessEqual

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) <= CDbl(FC.Formula1) Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            Else

                               If Rng.Value <= Range(Temp).Value Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                        Case xlNotEqual

                            Temp = GetStrippedValue(FC.Formula1)

                            If IsNumeric(Temp) Then

                               If CDbl(Rng.Value) <> CDbl(FC.Formula1) Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            Else

                               If Rng.Value <> Range(Temp).Value Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                       Case xlNotBetween

                            Temp = GetStrippedValue(FC.Formula1)

                            Temp2 = GetStrippedValue(FC.Formula2)

                            If IsNumeric(Temp) Then

                               If Not (CDbl(Rng.Value) <= CDbl(FC.Formula1)) And _

                                  (CDbl(Rng.Value) >= CDbl(FC.Formula2)) Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            Else

                               If Not Rng.Value <= Range(Temp).Value And _

                                  Rng.Value >= Range(Temp2).Value Then

                                  ActiveCondition = Ndx

                                  Exit Function

                               End If

                            End If

                       Case Else

                            Debug.Print "UNKNOWN OPERATOR"

                   End Select

                Case xlExpression

                    If Application.Evaluate(FC.Formula1) Then

                       ActiveCondition = Ndx

                       Exit Function

                    End If

                Case Else

                    Debug.Print "UNKNOWN TYPE"

               End Select

            Next Ndx

        End If

        ActiveCondition = 0

    End Function

    '--------------------------------------------------------------------

    Function GetStrippedValue(CF As String) As String

        Dim Temp        As String

        If InStr(1, CF, "=", vbTextCompare) Then

            If Right(CF, 2) = """""" Then

                Temp = Mid(CF, 3, Len(CF) - 3)

            Else

                Temp = Mid(CF, 3, Len(CF) - 2)

            End If

           If Left(Temp, 1) = "=" Then

               Temp = Mid(Temp, 2)

           End If

           If Temp = "" Then Temp = Evaluate(CF)

        Else

           Temp = CF

        End If

        GetStrippedValue = Temp

    End Function

    '-------------------------------------------------------------------

    Private Function GetCFInteriorColor(rngCel As Range) As Long

    ' Initial version from someone else

        Dim strHcolor       As String

        ' Get Hex interior color as String

        strHcolor = Right("000000" & Hex(rngCel.DisplayFormat.Interior.Color), 6)

        ' Convert each Hex portion to a Long & pass them to the RGB function

        GetCFInteriorColor = RGB(CLng("&H" & Right(strHcolor, 2)), _

                                 CLng("&H" & Mid(strHcolor, 3, 2)), _

                                 CLng("&H" & Left(strHcolor, 2)))

    End Function

    Daniel

    Cette réponse a-t-elle été utile ?

    0 commentaires Aucun commentaire
  4. DanielCo 107.1K Points de réputation
    2018-03-06T16:31:16+00:00

    Bonjour,

    Est-ce que tu peux poster le classeur ?

    Daniel

    Cette réponse a-t-elle été utile ?

    0 commentaires Aucun commentaire