Optimiser son code VBA

Fmiste

XLDnaute Junior
Bonjour Le Forum,

Je voudrais faire en sorte d'alleger mon code, et pour cela, j'aimerai, au lieu d'avoir pleins de "if", une syntaxe plus compacte.

Pour cela, je pense que d'inserer des "for" a la place des touts mes "if" pourrait m'aider

Comme je ne suis pas a l'aise encore avec les structures "for", je voudrais savoir si une personne pouvait m'adapter ce bout de code avec une boucle "for", pour la serie de "if" en milieu de code, si cela est possible, afin de me servir de modele pour l'adapter au reste de mon code.

Voici le code en question :

Code:
       top_section = Split(Range("topAml").Address, "$")
bot_section = Split(Range("botAml").Address, "$")
bot_sectionfin = Split(Range("botAu").Address, "$")
i = top_section(2)
Do
    If (Range("B" & i).Value <> "") Then
        If (Range("E" & i).Value <> "") Or (Range("F" & i).Value <> "") Or (Range("G" & i).Value <> "") Or (Range("H" & i).Value <> "") Or (Range("I" & i).Value <> "") Or (Range("K" & i).Value <> "") Then
            If (Range("E" & i).Value = "") Or (Range("F" & i).Value = "") Or (Range("G" & i).Value = "") Or (Range("H" & i).Value = "") Or (Range("I" & i).Value = "") Or (Range("K" & i).Value = "") Then
                flag_inc_acierml = True
                If (Range("E" & i).Value = "") Then
                    Range("E" & i).Interior.Color = RGB(255, 0, 0)
                End If
                If (Range("F" & i).Value = "") Then
                    Range("F" & i).Interior.Color = RGB(255, 0, 0)
                End If
                If (Range("G" & i).Value = "") Then
                    Range("G" & i).Interior.Color = RGB(255, 0, 0)
                End If
                If (Range("H" & i).Value = "") Then
                    Range("H" & i).Interior.Color = RGB(255, 0, 0)
                End If
                If (Range("I" & i).Value = "") Then
                    Range("I" & i).Interior.Color = RGB(255, 0, 0)
                End If
                If (Range("K" & i).Value = "") Then
                    Range("K" & i).Interior.Color = RGB(255, 0, 0)
                End If
            End If
        End If
        If (Range("K" & (CInt(bot_sectionfin(2)) + 1)).Value = "") Then
                flag_inc_acierchutes = True
                Range("K" & (CInt(bot_sectionfin(2)) + 1)).Interior.Color = RGB(255, 0, 0)
        End If
    End If
    i = i + 1
Loop Until i = CInt(bot_section(2)) - 1

Merci :)
 
Dernière édition:

Dull

XLDnaute Barbatruc
Re : Optimiser son code VBA

Salut Fmiste, le Forum

pas pu tester car avec juste un bout de code on ne peux pas faire grand chose

essaye quand même

For j = 5 To 11 'Boucle sur les colonne E à K
If j <> 10 Then ' ne prends pas en compte la colonne J
If Cells(i, j).Value = "" Then Cells(i, j).Interior.Color = RGB(255, 0, 0)
End If
Next j

à la place

If (Range("E" & i).Value = "") Then
Range("E" & i).Interior.Color = RGB(255, 0, 0)
End If
If (Range("F" & i).Value = "") Then
Range("F" & i).Interior.Color = RGB(255, 0, 0)
End If
If (Range("G" & i).Value = "") Then
Range("G" & i).Interior.Color = RGB(255, 0, 0)
End If
If (Range("H" & i).Value = "") Then
Range("H" & i).Interior.Color = RGB(255, 0, 0)
End If
If (Range("I" & i).Value = "") Then
Range("I" & i).Interior.Color = RGB(255, 0, 0)
End If
If (Range("K" & i).Value = "") Then
Range("K" & i).Interior.Color = RGB(255, 0, 0)
End If

Pas sur que cela fonctionne mais au moins tu aura une base pour faire ta boucle

Bonne Journée
 
Dernière édition: