OVH Cloud OVH Cloud

If : Else

4 réponses
Avatar
Stéphan DuQuébec
Bonjour à toutes & tous,

Voici une procédure assurément très simple pour vous:

.........
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then Rows(c.Row).Interior.ColorIndex = 3
If c = "Projet" Then Rows(c.Row).Interior.ColorIndex = 5
If c = "Plantage" Then Rows(c.Row).Interior.ColorIndex = 6
If c = "Végétation" Then Rows(c.Row).Interior.ColorIndex = 7
If c = "Service" Then Rows(c.Row).Interior.ColorIndex = 8
If c = "Souterrain" Then Rows(c.Row).Interior.ColorIndex = 9
If c = "Autre" Or c = "Inconnu" Then Rows(c.Row).Interior.ColorIndex = 10
Next c
End Sub
................

Tout fonctionne bien comme je le veux mais lorsque que j'essaie de planter
un ELSE pour spécifier une couleur de ligne particulière pour toute valeur ne
correspondant pas à une ou l'autre des 10 prévues précédement, j'ai un
message d'erreur disant que mon Else n'est pas rattaché à un If !!!!!

Siffleux, il y en a 7 dans ma procédure !

Autre petite question, quel est le script que je devrais utiliser pour
modifier plutôt la couleur de police des cellules d'une même ligne plutôt que
l'intérieur ?

Merci.

4 réponses

Avatar
michdenis
Bonjour Stéphan,

essaie ceci : Le Else à définir ... fin de la liste


Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3
ElseIf c = "Projet" Then
Rows(c.Row).Interior.ColorIndex = 5
ElseIf c = "Plantage" Then
Rows(c.Row).Interior.ColorIndex = 6
ElseIf c = "Végétation" Then
Rows(c.Row).Interior.ColorIndex = 7
ElseIf c = "Service" Then
Rows(c.Row).Interior.ColorIndex = 8
ElseIf c = "Souterrain" Then
Rows(c.Row).Interior.ColorIndex = 9
ElseIf c = "Autre" Or c = "Inconnu" Then
Rows(c.Row).Interior.ColorIndex = 10
Else 'a difinir

End If
Next c
End Sub


Salutations!




"Stéphan DuQuébec" a écrit dans le message de news:


Bonjour à toutes & tous,

Voici une procédure assurément très simple pour vous:

.........
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then Rows(c.Row).Interior.ColorIndex = 3
If c = "Projet" Then Rows(c.Row).Interior.ColorIndex = 5
If c = "Plantage" Then Rows(c.Row).Interior.ColorIndex = 6
If c = "Végétation" Then Rows(c.Row).Interior.ColorIndex = 7
If c = "Service" Then Rows(c.Row).Interior.ColorIndex = 8
If c = "Souterrain" Then Rows(c.Row).Interior.ColorIndex = 9
If c = "Autre" Or c = "Inconnu" Then Rows(c.Row).Interior.ColorIndex = 10
Next c
End Sub
................

Tout fonctionne bien comme je le veux mais lorsque que j'essaie de planter
un ELSE pour spécifier une couleur de ligne particulière pour toute valeur ne
correspondant pas à une ou l'autre des 10 prévues précédement, j'ai un
message d'erreur disant que mon Else n'est pas rattaché à un If !!!!!

Siffleux, il y en a 7 dans ma procédure !

Autre petite question, quel est le script que je devrais utiliser pour
modifier plutôt la couleur de police des cellules d'une même ligne plutôt que
l'intérieur ?

Merci.
Avatar
JpPradier
Bonsoir Stéphan

En plus de la réponse de Denis, voici une autre possibilité ci-dessous.
j-p

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
Select Case c
Case "RN", "ES", "TR"
Rows(c.Row).Interior.ColorIndex = 3
Case "Projet"
Rows(c.Row).Interior.ColorIndex = 5
Case "Plantage"
Rows(c.Row).Interior.ColorIndex = 6
Case "Végétation"
Rows(c.Row).Interior.ColorIndex = 7
Case "Service"
Rows(c.Row).Interior.ColorIndex = 8
Case "Souterrain"
Rows(c.Row).Interior.ColorIndex = 9
Case "Autre", "Inconnu"
Rows(c.Row).Interior.ColorIndex = 10
Case Else
Rows(c.Row).Interior.ColorIndex = 1
End Select
Next c
End Sub
Avatar
Clément Marcotte
Bonjour,

Tout fonctionne bien comme je le veux mais lorsque que j'essaie de
planter

un ELSE pour spécifier une couleur de ligne particulière pour toute
valeur ne

correspondant pas à une ou l'autre des 10 prévues précédement, j'ai
un

message d'erreur disant que mon Else n'est pas rattaché à un If
!!!!!


Siffleux, il y en a 7 dans ma procédure !


Tu utilises des IF "monoligne" qui obligent à tout garder sur une
ligne. Je n'ai jamais testé pour VBA, mais certains BASIC
autorisaient des Else dans une instruction monoligne, si le Else était
la dernière instruction de la ligne, dans le genre de:

If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3 else a = 132


Par contre ta procédure actuelle pourrait probablement être réécrite
comme ceci:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3
elseIf c = "Projet" Then
Rows(c.Row).Interior.ColorIndex = 5
elseIf c = "Plantage" Then
Rows(c.Row).Interior.ColorIndex = 6
elseIf c = "Végétation" Then
Rows(c.Row).Interior.ColorIndex = 7
elseIf c = "Service" Then
Rows(c.Row).Interior.ColorIndex = 8
elseIf c = "Souterrain" Then
Rows(c.Row).Interior.ColorIndex = 9
elseIf c = "Autre" Or c = "Inconnu" Then
Rows(c.Row).Interior.ColorIndex = 10
else
"Toute instruction qui serait exécutée si aucune des conditions if ou
elseif n'est vraie
end if
Next c
End Sub
.
ou alternativement avec Select Case

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
select case C.value
case "RN" , "RS" , "TR" Then
Rows(c.Row).Interior.ColorIndex = 3
case "Projet"
Rows(c.Row).Interior.ColorIndex = 5
case "Plantage" Then
Rows(c.Row).Interior.ColorIndex = 6
case "Végétation" Then
Rows(c.Row).Interior.ColorIndex = 7
case "Service"
Rows(c.Row).Interior.ColorIndex = 8
Case "Souterrain"
Rows(c.Row).Interior.ColorIndex = 9
case "Autre" ,"Inconnu" Then
Rows(c.Row).Interior.ColorIndex = 10
case else
"Toute instruction qui serait exécutée si aucune des conditions if ou
elseif n'est vraie
end select
Next c
End Sub

Autre petite question, quel est le script que je devrais utiliser
pour

modifier plutôt la couleur de police des cellules d'une même ligne
plutôt que

l'intérieur ?


Avec l'enregistreur de macros, tu peux sans doute élaguer.

Met en vert les textes de la ligne 4:

Sub Macro3()
'
' Macro3 Macro
' Macro enregistrée le 2004-12-01 par Clément Marcotte
'

'
Rows("4:4").Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 10
End With
End Sub


"Stéphan DuQuébec" a écrit
dans le message de
news:

Bonjour à toutes & tous,

Voici une procédure assurément très simple pour vous:

.........
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3

If c = "Projet" Then Rows(c.Row).Interior.ColorIndex = 5
If c = "Plantage" Then Rows(c.Row).Interior.ColorIndex = 6
If c = "Végétation" Then Rows(c.Row).Interior.ColorIndex = 7
If c = "Service" Then Rows(c.Row).Interior.ColorIndex = 8
If c = "Souterrain" Then Rows(c.Row).Interior.ColorIndex = 9
If c = "Autre" Or c = "Inconnu" Then Rows(c.Row).Interior.ColorIndex
= 10

Next c
End Sub
................



Merci. "Projet"


Avatar
Stéphan DuQuébec
Avec mes remerciements et cordiales salutations, messieurs.

Vos conseils sont précieux et lus avec attention.


Bonjour,

Tout fonctionne bien comme je le veux mais lorsque que j'essaie de
planter

un ELSE pour spécifier une couleur de ligne particulière pour toute
valeur ne

correspondant pas à une ou l'autre des 10 prévues précédement, j'ai
un

message d'erreur disant que mon Else n'est pas rattaché à un If
!!!!!


Siffleux, il y en a 7 dans ma procédure !


Tu utilises des IF "monoligne" qui obligent à tout garder sur une
ligne. Je n'ai jamais testé pour VBA, mais certains BASIC
autorisaient des Else dans une instruction monoligne, si le Else était
la dernière instruction de la ligne, dans le genre de:

If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3 else a = 132


Par contre ta procédure actuelle pourrait probablement être réécrite
comme ceci:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3
elseIf c = "Projet" Then
Rows(c.Row).Interior.ColorIndex = 5
elseIf c = "Plantage" Then
Rows(c.Row).Interior.ColorIndex = 6
elseIf c = "Végétation" Then
Rows(c.Row).Interior.ColorIndex = 7
elseIf c = "Service" Then
Rows(c.Row).Interior.ColorIndex = 8
elseIf c = "Souterrain" Then
Rows(c.Row).Interior.ColorIndex = 9
elseIf c = "Autre" Or c = "Inconnu" Then
Rows(c.Row).Interior.ColorIndex = 10
else
"Toute instruction qui serait exécutée si aucune des conditions if ou
elseif n'est vraie
end if
Next c
End Sub
..
ou alternativement avec Select Case

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
select case C.value
case "RN" , "RS" , "TR" Then
Rows(c.Row).Interior.ColorIndex = 3
case "Projet"
Rows(c.Row).Interior.ColorIndex = 5
case "Plantage" Then
Rows(c.Row).Interior.ColorIndex = 6
case "Végétation" Then
Rows(c.Row).Interior.ColorIndex = 7
case "Service"
Rows(c.Row).Interior.ColorIndex = 8
Case "Souterrain"
Rows(c.Row).Interior.ColorIndex = 9
case "Autre" ,"Inconnu" Then
Rows(c.Row).Interior.ColorIndex = 10
case else
"Toute instruction qui serait exécutée si aucune des conditions if ou
elseif n'est vraie
end select
Next c
End Sub

Autre petite question, quel est le script que je devrais utiliser
pour

modifier plutôt la couleur de police des cellules d'une même ligne
plutôt que

l'intérieur ?


Avec l'enregistreur de macros, tu peux sans doute élaguer.

Met en vert les textes de la ligne 4:

Sub Macro3()
'
' Macro3 Macro
' Macro enregistrée le 2004-12-01 par Clément Marcotte
'

'
Rows("4:4").Select
With Selection.Font
.Name = "Arial"
.FontStyle = "Normal"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = 10
End With
End Sub


"Stéphan DuQuébec" a écrit
dans le message de
news:

Bonjour à toutes & tous,

Voici une procédure assurément très simple pour vous:

.........
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c
For Each c In Range("B26:B144")
If c = "RN" Or c = "RS" Or c = "TR" Then
Rows(c.Row).Interior.ColorIndex = 3

If c = "Projet" Then Rows(c.Row).Interior.ColorIndex = 5
If c = "Plantage" Then Rows(c.Row).Interior.ColorIndex = 6
If c = "Végétation" Then Rows(c.Row).Interior.ColorIndex = 7
If c = "Service" Then Rows(c.Row).Interior.ColorIndex = 8
If c = "Souterrain" Then Rows(c.Row).Interior.ColorIndex = 9
If c = "Autre" Or c = "Inconnu" Then Rows(c.Row).Interior.ColorIndex
= 10

Next c
End Sub
................



Merci. "Projet"