XL 2010 Code VBA formule SI

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 !

Philou0607

XLDnaute Nouveau
Bonsoir,
Je souhaite changer le format d'une cellule en fonction de sa valeur en code VBA.
Mon exemple est simple : Si la valeur de la cellule A1 < 10 alors le format de la cellule change (police et remplissage)
Merci pour votre aide
 
Bonjour Philou,
Un exemple en PJ avec :
VB:
Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        If Target < 10 Then
            Target.Interior.Color = RGB(255, 0, 0)    ' Fond rouge
            Target.Font.Color = RGB(255, 255, 0)      ' Caractères jaunes
            Target.Font.Name = "Calibri"              ' Police Calibri
            Target.Font.Size = 18                     ' Taille 18
            Target.Font.Bold = True                   ' Gras
            Target.Font.Italic = True                 ' Italique
        Else
            Target.Interior.Color = RGB(255, 255, 255)
            Target.Font.Color = RGB(0, 0, 0)
            Target.Font.Name = "Arial"
            Target.Font.Size = 8
            Target.Font.Bold = False
            Target.Font.Italic = False
        End If
    End If
End Sub
 

Pièces jointes

Bonjour,

j'ai réécrit ainsi le code VBA de @sylvanu :
VB:
Sub Worksheet_Change(ByVal Target As Range)
  With Target
    If .CountLarge > 1 Then Exit Sub
    If .Address(0, 0) <> "A1" Then Exit Sub
    Dim cf&, ct&, np$, tp As Byte, b As Boolean
    If .Value < 10 Then
      'fond rouge, texte jaune, gras italique
      cf = RGB(255, 0, 0): ct = RGB(255, 255, 0)
      np = "Calibri": tp = 18: b = -1
    Else
      cf = RGB(255, 255, 255): ct = RGB(0, 0, 0)
      np = "Arial": tp = 8: b = 0
    End If
    .Interior.Color = cf
    With .Font
      .Color = ct: .Name = np: .Size = tp
      .Bold = b: .Italic = b
    End With
  End With
End Sub
noter qu'il y a 2 « With .. End With »

soan
 
Bonjour,
Le but était d'être didactique afin de picorer ce qui est intéressant, non d'optimiser. L'important est d'être réutilisable au plus simple.
Sinon on fait ça :
VB:
Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        c = vbRed: b = vbBlack: w = vbWhite: j = vbYellow: p = "Calibri": q = "Arial": t = 18: u = 8: i = True: f = False
        With Target
                If Target < 10 Then
                    .Interior.Color = vbRed
                    With .Font: .Color = j: .Name = p: .Size = t: .Bold = i: .Italic = i: End With
                Else
                    .Interior.Color = b
                    With .Font: .Color = w: .Name = q: .Size = u: .Bold = f: .Italic = f: End With
                End If
        End With
    End If
End Sub
Ca fait la même chose en 13 lignes ... et est difficilement réutilisable.
 
Dernière édition:
- 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
5
Affichages
132
Réponses
14
Affichages
223
Réponses
17
Affichages
321
Réponses
2
Affichages
282
Retour