Marco tres lente

Bonjour

J’ai un petit problème pour une macro qui est simple mais qui s’exécute très lentement

Est-ce que j’ai mis trop de boucle « For – next » ?

Merci d’avance pour me guider sur la bonne voie

Option Explicit

Private Sub CommandButton1_Click()

Dim ligne, colone, i, b As Integer

Application.ScreenUpdating = False

Sheets("INDEX").Range("A1:s48").Interior.ColorIndex = 4

Sheets("INDEX").Range("t1:t39").Interior.ColorIndex = 4

For ligne = 1 To 48

For colone = 1 To 20

For b = 0 To 500

If Sheets("INDEX").Cells(ligne, colone).Value = Sheets("IMPORT").Range("a" & b + 1).Value And Sheets("INDEX").Range("y1").Value = Sheets("IMPORT").Range("p" & b + 1).Value Then

Sheets("INDEX").Cells(ligne, colone).Interior.ColorIndex = 6

End If

Next

Next

Next

Application.ScreenUpdating = True

End Sub

Bonjour,

Bah... Tu examines quand même quelque 500 000 fois la condition If... And...

A ce stade l'utilisation d'Array est préférable.

La même optimisée :

Private Sub Test()
Dim ligne, colone, i, b As Integer, ArrIND, ArrIMP, Cond
Cond = Sheets("INDEX").Range("Y1").Value
ArrIND = Sheets("INDEX").Range("A1:T48")
ArrIMP = Sheets("IMPORT").Range("A1:P500")

Sheets("INDEX").Range("A1:S48,T1:T39").Interior.ColorIndex = 4
Application.ScreenUpdating = False
For ligne = 1 To 48
   For colone = 1 To 20
      For b = 1 To 500
         If ArrIMP(b, 16) = Cond And ArrIND(ligne, colone) = ArrIMP(b, 1) Then
               Sheets("INDEX").Cells(ligne, colone).Interior.ColorIndex = 6
         End If
      Next
   Next
Next
Application.ScreenUpdating = True
End Sub

A+

Bonjour

Ça marche super bien

Merci

Bonjour @vlad92,

Pourrais tu mettre à dispo un bout de fichier pour voir tourner en détail la macro de @galopin01 avec l'utilisation de tableaux .

Cordialement.

Rechercher des sujets similaires à "marco tres lente"