correction de macro Sub colorise()

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

olibelle0101

XLDnaute Occasionnel
Bonsoir le forum.
Je me trouve avec un petit soucis de code.
Voici ce code :
Sub colorise()
Worksheets("selections").Activate
For ligne = 3 To 369
Set r1 = Range(Cells(ligne, 9), Cells(ligne, 19))
r1.Select
For Each c In r1
If c.Value = Cells(ligne, 21).Value Then
c.Font.Bold = True
With c.Interior
.ColorIndex = 34
.Pattern = xlSolid
End With
End If
Next c
For Each c In r1
If c.Value = Cells(ligne, 22).Value Then
c.Font.Bold = True
With c.Interior
.ColorIndex = 40
.Pattern = xlSolid
End With
End If
Next c
For Each c In r1
If c.Value = Cells(ligne, 23).Value Then
c.Font.Bold = True
With c.Interior
.ColorIndex = 36
.Pattern = xlSolid
End With
End If
Next c
For Each c In r1
If c.Value = Cells(ligne, 24).Value Then
c.Font.Italic = True
c.Font.Bold = True
With c.Interior
.ColorIndex = 35
.Pattern = xlSolid
End With
End If
Next c
For Each c In r1
If c.Value = Cells(ligne, 25).Value Then
c.Font.Italic = True
With c.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
End If
Next c
Next ligne
End Sub
Il me met une couleur suivant des valeur identique (c'est parfait)
Mais si il y a aucune valeur dans "ligne 21 22 ou 23 etc..."
Je ne veut pas qu'il me met de couleur dans "Set r1 = Range(Cells(ligne, 9), Cells(ligne, 19))"
si dans celle-ci il y a aussi aucune valeur dans certaines cellules.
Merci pour vos conseil.
A bientôt.
 
Dernière édition:
Re : correction de macro Sub colorise()

bonsoir à tous

à tester

Code:
Sub colorise()
Dim Reponse As Range, Lg&, Lg1&, Pos%, Pos1%, Pos2%, Depart&, Fin&
 Worksheets("selections").Activate
On Error GoTo Err
 Set Reponse = Application.InputBox(prompt:="Sélectionner la ou les ligne(s) que vous voulez traitée(s)", _
    Title:="Formatage ligne(s)", Default:="$3:$369", Type:=8)
If Reponse.Address = "" Then Exit Sub
On Error GoTo 0
Pos = InStr(Reponse.Address, "$")
Pos1 = InStr(1, Reponse.Address, ":")
Pos2 = InStr(Pos + 1, Reponse.Address, "$")
If Pos2 < Pos1 Then GoTo Err
Depart = Mid(Reponse.Address, Pos + 1, Pos1 - (Pos + 1))
Fin = Right(Reponse.Address, Len(Reponse.Address) - Pos2)
 For ligne = Depart To Fin
    Set r1 = Range(Cells(ligne, 9), Cells(ligne, 19))
    r1.Select
    For Each c In r1
        If c.Value = Cells(ligne, 21).Value Then
            c.Font.Bold = True
            With c.Interior
               .ColorIndex = 34
               .Pattern = xlSolid
            End With
        End If
    Next c
    For Each c In r1
        If c.Value = Cells(ligne, 22).Value Then
            c.Font.Bold = True
            With c.Interior
                .ColorIndex = 40
                .Pattern = xlSolid
            End With
        End If
    Next c
    For Each c In r1
        If c.Value = Cells(ligne, 23).Value Then
            c.Font.Bold = True
            With c.Interior
            .ColorIndex = 36
            .Pattern = xlSolid
            End With
        End If
    Next c
    For Each c In r1
        If c.Value = Cells(ligne, 24).Value Then
            c.Font.Italic = True
            c.Font.Bold = True
            With c.Interior
                .ColorIndex = 35
                .Pattern = xlSolid
            End With
        End If
    Next c
    For Each c In r1
        If c.Value = Cells(ligne, 25).Value Then
            c.Font.Italic = True
            With c.Interior
                .ColorIndex = 15
                .Pattern = xlSolid
            End With
        End If
    Next c
 Next ligne
 Exit Sub
Err:
If Not Reponse Is Nothing Then
    MsgBox "Votre entrée n'est pas une plage valide." & vbCrLf & "Vous devez sélectionner des lignes.", vbCritical
End If
 End Sub

bonne nuit
a+
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
4
Affichages
284
Réponses
5
Affichages
915
Réponses
4
Affichages
737
  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
1 K
Réponses
15
Affichages
791
Retour