XL 2010 Code VBA formule SI

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
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
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

  • Font.xlsm
    14.2 KB · Affichages: 19

soan

XLDnaute Barbatruc
Inactif
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
 

sylvanu

XLDnaute Barbatruc
Supporter XLD
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:

Discussions similaires

Statistiques des forums

Discussions
312 104
Messages
2 085 332
Membres
102 864
dernier inscrit
abderrashmaen