Vba

she -  
 tech_57 -
Bonjour,
j'ai un programme a faire pour un stage d'économie qui consiste à dans un premier temps comparer les chiffres de deux colonnes b et c et d'écrire positive ou négative dans une troisieme colonne d et que si c positive le fond de la colonne a et de la colonne d soient vertes et si c negative rouge ??????

Sub test()
For i = 2 To range("D2").End(xlDown).Row
If Cells(i, 4).Value < Cells(i, 5).Value Then Cells(i, 6).Value = "positive" Else: Cells(i, 3).Value = "négative"

If Cells(i, 4).Value < Cells(i, 5).Value Then Cells(i, 3).Interior.ColorIndex = 3 And Cells(i, 6).Interior.ColorIndex = 3 Else: Cells(i, 3).Interior.ColorIndex = 43
Cells(i, 6).Interior.ColorIndex = 43

End
End
Next i
End Sub
Configuration: Windows XP Internet Explorer 7.0

2 réponses

  1. tech_57
     
    Bonjour,

    Le raisonnement est bon mais pas la syntaxe
    NB: Le cas ou les cellules sont égales n'est pas traité

    Voici comment il faut l'écrire

    Sub test()
    dim i as integer
    
    For i = 2 To range("D2").End(xlDown).Row
    
    If Cells(i, 4).Value < Cells(i, 5).Value Then 
       Cells(i, 6).Value = "positive"
       Cells(i, 3).Interior.ColorIndex = 3
       Cells(i, 6).Interior.ColorIndex = 3
    Else
       Cells(i, 3).Value = "négative"
       Cells(i, 3).Interior.ColorIndex = 43
       Cells(i, 6).Interior.ColorIndex = 43
    End If
    
    Next i
    End Sub
    0
    1. she15
       
      merci mille fois je vais esseyer
      0
      1. she15 > she15
         
        help
        pouvez vous me dire ce qui cloche avec mon programme ????

        Dim i As Integer
        Cells(2, 2).Interior.ColorIndex = 25
        Cells(2, 5).Interior.ColorIndex = 25
        Cells(10, 2).Interior.ColorIndex = 25
        Cells(10, 5).Interior.ColorIndex = 25
        Cells(18, 5).Interior.ColorIndex = 25

        Cells(21, 5).Interior.ColorIndex = 50
        Cells(23, 5).Interior.ColorIndex = 3
        Cells(22, 5).Interior.ColorIndex = 50


        For i = 3 To range("B3").End(xlDown).Row
        If Cells(i, 3).Value = "positive" Then
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
        ElseIf Cells(i, 3).Value = "négative" Then
        Cells(i, 2).Interior.ColorIndex = 3
        Cells(i, 3).Interior.ColorIndex = 3
        Else: Cells(i, 3).Value = "en ligne"
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
        End If
        Next i

        For i = 3 To range("E3").End(xlDown).Row
        If Cells(i, 6).Value = "positive" Then
        Cells(i, 5).Interior.ColorIndex = 50
        Cells(i, 6).Interior.ColorIndex = 50
        ElseIf Cells(i, 6).Value = "négative" Then
        Cells(i, 5).Interior.ColorIndex = 3
        Cells(i, 6).Interior.ColorIndex = 3
        Else: Cells(i, 6).Value = "en ligne"
        Cells(i, 5).Interior.ColorIndex = 50
        Cells(i, 6).Interior.ColorIndex = 50
        End If
        Next i



        For i = 11 To range("E11").End(xlDown).Row
        If Cells(i, 6).Value = "positive" Then
        Cells(i, 5).Interior.ColorIndex = 50
        Cells(i, 6).Interior.ColorIndex = 50
        ElseIf Cells(i, 6).Value = "négative" Then
        Cells(i, 5).Interior.ColorIndex = 3
        Cells(i, 6).Interior.ColorIndex = 3
        Else: Cells(i, 6).Value = "en ligne"
        Cells(i, 5).Interior.ColorIndex = 50
        Cells(i, 6).Interior.ColorIndex = 50
        End If
        Next i

        For i = 11 To range("B11").End(xlDown).Row
        If Cells(i, 3).Value = "positive" Then
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
        ElseIf Cells(i, 3).Value = "négative" Then
        Cells(i, 2).Interior.ColorIndex = 3
        Cells(i, 3).Interior.ColorIndex = 3
        Else: Cells(i, 3).Value = "en ligne"
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
        End If
        Next i

        For i = 19 To range("E19").End(xlDown).Row
        If Cells(19, 6).Value = "positive" Then
        Cells(19, 5).Interior.ColorIndex = 50
        Cells(19, 6).Interior.ColorIndex = 50
        If Cells(19, 6).Value = "négative" Then
        Cells(19, 5).Interior.ColorIndex = 3
        Cells(19, 6).Interior.ColorIndex = 3
        Else: Cells(19, 6).Value = "en ligne"
        Cells(19, 5).Interior.ColorIndex = 50
        Cells(19, 6).Interior.ColorIndex = 50
        End If
        Next i


        End Sub
        0
  2. tech_57
     
    Bonjour,

    Je pense que l'ecriture des bloc If .. Then ... EsleIf ... Else .. En If est incorrecte
    Voici comment je l'ecrirai moi

    For i = 3 To range("B3").End(xlDown).Row
    
    If Cells(i, 3).Value = "positive" Then
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
    ElseIf Cells(i, 3).Value = "négative" Then
        Cells(i, 2).Interior.ColorIndex = 3
        Cells(i, 3).Interior.ColorIndex = 3
    Else
       Cells(i, 3).Value = "en ligne"
       Cells(i, 2).Interior.ColorIndex = 50
       Cells(i, 3).Interior.ColorIndex = 50
    End If
    
    Next i 


    ou encore

    For i = 3 To range("B3").End(xlDown).Row
    
    If Cells(i, 3).Value = "positive" Then
        Cells(i, 2).Interior.ColorIndex = 50
        Cells(i, 3).Interior.ColorIndex = 50
    Else
          If Cells(i, 3).Value = "négative" Then
               Cells(i, 2).Interior.ColorIndex = 3
               Cells(i, 3).Interior.ColorIndex = 3
          End If
    Else
       Cells(i, 3).Value = "en ligne"
       Cells(i, 2).Interior.ColorIndex = 50
       Cells(i, 3).Interior.ColorIndex = 50
    End If
    
    Next i 


    Personnellement je préfère la seconde, elle est mieux structurée.

    En fait chez toi c'est le Else qui pose problème.
    Si tu veux conserver ta syntaxe il faut tout mettre sur la meme ligne comme ça:
    Else: Cells(i, 3).Value = "en ligne" : Cells(i, 2).Interior.ColorIndex = 50 : Cells(i, 3).Interior.ColorIndex = 50

    Un conseil, apprends à structurer tes programmes, la lecture et le déboguage sera plus facile.

    A+.
    0