Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Général VBA
Général VBA Forum général VBA . Pour les logiciels spécifiques (Access, Excel, Word, ...), postez dans les bons sous forums.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 26/04/2007, 16h23   #1
Membre à l'essai
 
Inscription : avril 2007
Messages : 175
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 175
Points : 20
Points : 20
Par défaut de If vers Case

Bonjour a tous,

j'ai une macro qui contien bcp de If et je veux mettre a la place des If des case afin que mon code soit bien structuré

ma macro et
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
ub CouleursIndex1()
    For i = 3 To 167
      For j = 6 To 14
      If Cells(i, j) = Empty Or Cells(i, j).Value <> ED Or Cells(i, j).Value <> AF Or Cells(i, j).Value <> DP Or Cells(i, j).Value <> CB Or Cells(i, j).Value <> RO Or Cells(i, j).Value <> JPF Then
       Cells(i, j).Interior.ColorIndex = 0
        End If
        If Cells(i, j).Value = "PG" Then
         Cells(i, j).Interior.ColorIndex = 3
        End If
        If Cells(i, j).Value = "JPF" Then
         Cells(i, j).Interior.ColorIndex = 4
        End If
        If Cells(i, j).Value = "RO" Then
         Cells(i, j).Interior.ColorIndex = 5
        End If
        If Cells(i, j).Value = "CB" Then
         Cells(i, j).Interior.ColorIndex = 6
        End If
        If Cells(i, j).Value = "DP" Then
         Cells(i, j).Interior.ColorIndex = 7
        End If
        If Cells(i, j).Value = "AF" Then
        Cells(i, j).Interior.ColorIndex = 8
        End If
        If Cells(i, j).Value = "ED" Then
         Cells(i, j).Interior.ColorIndex = 15
        End If
     Next
    Next
End Sub

pour remplcer les iF par des case j'ai fait
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Sub CouleursIndex3()
Dim i As Integer
Dim j As Integer
 
    For i = 3 To 167
      For j = 6 To 14
      Select Case Cells(i, j)
       Case Cells(i, j) = Empty Or Cells(i, j).Value <> ED Or Cells(i, j).Value <> AF _
       Or Cells(i, j).Value <> DP Or Cells(i, j).Value <> CB Or Cells(i, j).Value <> RO _
       Or Cells(i, j).Value <> JPF
            Cells(i, j).Interior.ColorIndex = 0
       Case Cells(i, j).Value = "PG"
            Cells(i, j).Interior.ColorIndex = 3
       Case Cells(i, j).Value = "JPF"
            Cells(i, j).Interior.ColorIndex = 4
       Case Cells(i, j).Value = "RO"
            Cells(i, j).Interior.ColorIndex = 5
       Case Cells(i, j).Value = "CB"
            Cells(i, j).Interior.ColorIndex = 6
       Case Cells(i, j).Value = "DP"
            Cells(i, j).Interior.ColorIndex = 7
       Case Cells(i, j).Value = "AF"
            Cells(i, j).Interior.ColorIndex = 8
       Case Cells(i, j).Value = "ED"
            Cells(i, j).Interior.ColorIndex = 15
        End Select
     Next
    Next
End Sub

mais sa na pas bien marché , par ce que la macro devvrais colorée selon certains conditions . la macro des Case donne a toutes les cellules la même couleur ui est le Rouge.

merci de m'expliquer commment sa marche la Condition Case
k-eisti est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/04/2007, 16h28   #2
Membre habitué
 
Inscription : juillet 2005
Messages : 141
Détails du profil
Informations forums :
Inscription : juillet 2005
Messages : 141
Points : 117
Points : 117
Bonjour
quand tu fais un select case, tu n'a pas besoin de rappeler ce que tu testes ainsi dans ton cas:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Sub CouleursIndex3()
Dim i As Integer
Dim j As Integer
 
    For i = 3 To 167
      For j = 6 To 14
      Select Case Cells(i, j).value
       Case "PG"
            Cells(i, j).Interior.ColorIndex = 3
       Case "JPF"
            Cells(i, j).Interior.ColorIndex = 4
       Case "RO"
            Cells(i, j).Interior.ColorIndex = 5
       Case "CB"
            Cells(i, j).Interior.ColorIndex = 6
       Case "DP"
            Cells(i, j).Interior.ColorIndex = 7
       Case "AF"
            Cells(i, j).Interior.ColorIndex = 8
       Case "ED"
            Cells(i, j).Interior.ColorIndex = 15
       Case Else
            Cells(i, j).Interior.ColorIndex = 0
        End Select
     Next
    Next
End Sub
rémi
gruget est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/04/2007, 17h04   #3
Membre à l'essai
 
Inscription : avril 2007
Messages : 175
Détails du profil
Informations forums :
Inscription : avril 2007
Messages : 175
Points : 20
Points : 20
Merci gruget , c'est plus partique et plus jolie avec les CASES
k-eisti est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 02h08.


 
 
 
 
Partenaires

Hébergement Web