Offset avec step variable

Bonjour le forum

Merci de m'aider à trouver une solution à mon problème.

Je veux colorer mes colonnes suivant les champs qui se trouvent dans la ligne 3,

j'ai mis dans mon code un offset avec un step 5.

cela fonctionne parfaitement mais quand j'ai inséré des colonnes en ("U" - "AK" et "BA" ) ça change entre colonnes "P" et "V" le step devient 6.

J'attends votre aide.

Cordialement.

8essai-atoto.xlsx (12.51 Ko)

Bonjour Atoto

Merci de joindre des fichiers avec soit du code, soit des MFC

Là ton fichier ne contient que des cellules colorée, quel es t'intérêt ?

@+

Bonjour

Merci de votre aide

Ce code est fait pour plusieurs onglets.

j'ai créé un module pour les colonnes du champ "DR" et un autre pour les colonnes du champ "NL".

Sub colorer_Dr()
Dim onglet As Worksheet
Dim derniere_ligne As Long
Dim ligne_en_cours As Long
Dim Seuil As Variant
Dim Classe As Long
Dim s&
Dim n As Byte
For s = 4 To Sheets.Count
Set onglet = Worksheets(s)
derniere_ligne = onglet.Cells(Rows.Count, 7).End(xlUp).Row
For ligne_en_cours = 3 To derniere_ligne
For n = 0 To 45 Step 5
Seuil = onglet.Cells(ligne_en_cours, 7).Offset(0, n).Value
Classe = onglet.Cells(ligne_en_cours, 5).Value
Application.ScreenUpdating = False
If Seuil = "" Then

onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = -4142
ElseIf Classe = 2 And Seuil >= 1.2 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 22
ElseIf Classe = 2 And Seuil <= 1 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 35
Else
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 36

If Classe = 3 And Seuil >= 1.5 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 22
ElseIf Classe = 3 And Seuil <= 1.2 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 35
Else
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 36

If Classe = 4 And Seuil >= 1.8 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 22
ElseIf Classe = 4 And Seuil <= 1.5 Then
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 35
Else
onglet.Cells(ligne_en_cours, 7).Offset(0, n).Interior.ColorIndex = 36

  End If
  End If
  End If
 Next
Next
Next
End Sub

Bonsoir Atoto

Voici le code

Sub ColorerCol()
  Dim Onglet As Worksheet
  Dim Lig As Long, NbCol As Long, NbLig As Long
  Dim Classe As Long, s As Integer
  Dim Inc As Integer, IncS As Integer, NumCol As Long
  Dim Seuil As Variant
  'For s = 4 To Sheets.Count
  s = 1
  Set Onglet = Worksheets(s)
  ' Combien de colonnes
  NbCol = Onglet.Cells(3, Columns.Count).End(xlToLeft).Column
  ' Combien de ligne
  NbLig = Onglet.Cells(Rows.Count, 7).End(xlUp).Row
  ' Pour chaque ligne
  For Lig = 3 To NbLig
    ' Récupérer la classe de la ligne
    Classe = Onglet.Cells(Lig, 5).Value
    ' Pour chaque colonne
    For NumCol = 6 To NbCol Step 5
      ' incrément par rapport au pas
      If NumCol < Range("U1").Column Then
        Inc = 0
      ElseIf NumCol < Range("AK1").Column Then
        Inc = 1
      ElseIf NumCol < Range("BA1").Column Then
        Inc = 2
      End If
      ' Incrément du seuil : 0 poru NL et 1 pour DC
      For IncS = 0 To 1
        Seuil = Onglet.Cells(Lig, Inc + NumCol + IncS).Value
        Application.ScreenUpdating = False
        If Seuil = "" Then
          If IncS = 0 Then
            ' Couleur que doit avoir NL
            'Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex =
          Else
            Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex = -4142
          End If
          GoTo SuiteCol
        End If
        If Seuil >= 1.2 And (Classe = 2 Or Classe = 3 Or Classe = 4) Then
          If IncS = 0 Then
            ' Couleur que doit avoir NL
            'Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex =
          Else
            Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex = 22
          End If
          GoTo SuiteCol
        ElseIf Seuil <= 1 And (Classe = 2 Or Classe = 3 Or Classe = 4) Then
          If IncS = 0 Then
            ' Couleur que doit avoir NL
            'Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex =
          Else
            Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex = 35
          End If
          GoTo SuiteCol
        Else
          If IncS = 0 Then
            ' Couleur que doit avoir NL
            'Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex =
          Else
            Onglet.Cells(Lig, Inc + NumCol + IncS).Interior.ColorIndex = 36
          End If
          GoTo SuiteCol
        End If
SuiteCol:
      Next IncS
    Next NumCol
  Next Lig
  'Next
End Sub

Et le fichier

@+

BrunoM45

Merci pour ton aide

Rechercher des sujets similaires à "offset step variable"