VBA creer une macro

atoss77

XLDnaute Nouveau
Bonjour

j'ai une fichier excel avec des valeurs en % en A et B. En colonne C je voudrais faire C= A-B

Je voudrais une macro pour faire :
si C% est inférieur à 0 format cellule = rouge
sinon si C% supérieur à 0 format cellule = vert foncé
sinon si C% = 0 alors vert clair


Merci
 

Pièces jointes

  • Test.xls
    33.5 KB · Affichages: 34
  • Test.xls
    33.5 KB · Affichages: 42
  • Test.xls
    33.5 KB · Affichages: 43

gilbert_RGI

XLDnaute Barbatruc
Re : VBA creer une macro

Bonjour,

dans le code de la feuille

VB:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 3 Then Exit Sub
    If Target = "" Then Exit Sub
    If Target < 0 Then Target.Interior.Color = RGB(255, 0, 0)
    If Target = 0 Then Target.Interior.Color = RGB(20, 255, 210)
    If Target > 0 Then Target.Interior.Color = RGB(0, 255, 0)
End Sub
 

DoubleZero

XLDnaute Barbatruc
Re : VBA creer une macro

Bonjour, atoss77, gilbert_RGI, le Forum,

Une autre suggestion avec le présent code, placé dans un module standard.


Code:
Option Explicit
Sub Couleur_selon_valeur()
    Dim i As Long
    Application.ScreenUpdating = False
    For i = Cells(Rows.Count, "a").End(xlUp).Row To 2 Step -1
        With Range("c" & i)
            .FormulaR1C1 = "=RC[-1]-RC[-2]"
            If .Value < 0 Then .Interior.ColorIndex = 3
            If .Value > 0 Then .Interior.ColorIndex = 43
            If .Value = 0 Then .Interior.ColorIndex = 4
        End With
    Next i
    Application.ScreenUpdating = True
End Sub

A bientôt :)
 

gilbert_RGI

XLDnaute Barbatruc
Re : VBA creer une macro

ou une autre

VB:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, [c1:c100]) Is Nothing And Target.Count = 1 Then
        Select Case Target.Value
        Case Is < 0
            Target.Interior.ColorIndex = 3
        Case Is = 0
            Target.Interior.ColorIndex = 4
        Case Is > 0
            Target.Interior.ColorIndex = 35
        Case Else
            Target.Interior.ColorIndex = xlNone
        End Select
    End If
End Sub

Edit: Bonjour 00
 

atoss77

XLDnaute Nouveau
Re : VBA creer une macro

Merci pour vos réponse

et si j'ai 2 fois la même chose

en plus des colonnes a, b, c j'ai les colonnes FGH avec h=F-G

comment je modifie pour la macro fonctionne en colonne C et En colonne H
 

Pièces jointes

  • Test.xls
    37.5 KB · Affichages: 25
  • Test.xls
    37.5 KB · Affichages: 33
  • Test.xls
    37.5 KB · Affichages: 33

DoubleZero

XLDnaute Barbatruc
Re : VBA creer une macro

Re-bonjour,

Comme ceci ?

Code:
Option Explicit
Sub Couleur_selon_valeur_V2()
    Dim c As Range
    Application.ScreenUpdating = False
    For Each c In Union(Range("a2", Cells(Rows.Count, "a").End(xlUp)), Range("f2", Cells(Rows.Count, "f").End(xlUp)))
        With c.Offset(, 2)
            .FormulaR1C1 = "=RC[-1]-RC[-2]"
            If .Value < 0 Then .Interior.ColorIndex = 3
            If .Value > 0 Then .Interior.ColorIndex = 43
            If .Value = 0 Then .Interior.ColorIndex = 4
        End With
    Next c
    Application.ScreenUpdating = True
End Sub

A bientôt :)
 
Dernière édition:

DoubleZero

XLDnaute Barbatruc
Re : VBA creer une macro

Re-bonjour,

Si les colonnes a et f devaient comporter des "trous"...

Code:
Option Explicit
Sub Couleur_selon_valeur_V3()
    Dim c As Range
    Application.ScreenUpdating = False
    For Each c In Union(Range("a2", Cells(Rows.Count, "a").End(xlUp)), Range("f2", Cells(Rows.Count, "f").End(xlUp))).SpecialCells(xlCellTypeConstants, 23)
        With c.Offset(, 2)
            .FormulaR1C1 = "=RC[-1]-RC[-2]"
            If .Value < 0 Then .Interior.ColorIndex = 3
            If .Value > 0 Then .Interior.ColorIndex = 43
            If .Value = 0 Then .Interior.ColorIndex = 4
        End With
    Next c
    Application.ScreenUpdating = True
End Sub

A bientôt :)
 

Discussions similaires

Statistiques des forums

Discussions
312 471
Messages
2 088 707
Membres
103 927
dernier inscrit
Mkeal