Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Excel > Macros et VBA Excel
Macros et VBA Excel Vos questions relatives aux macros Excel, à l'utilisation de VBA et à l'automatisation de vos classeurs Excel.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 02/09/2011, 15h10   #1
Membre du Club
 
Inscription : décembre 2005
Messages : 90
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 90
Points : 47
Points : 47
Par défaut [problème optimisation] Colorier lignes

Bonjour,

Je souhaite griser les lignes qui contiennent le meme numéro de facture (soit la meme valeur dans la colonne Z).

J'ai globalement réussie a faire ce que je souhaitais seulement pour plus de 34 000 enregistrement mon code fait planter Excel.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
Sub test()
 
    Dim state As Boolean
    Dim lastColo As Integer
    Dim nbLigne As Long
    Dim nbColonne As Long
 
    'nbLigne = Range("B65536").End(xlUp).Row
    'nbColonne = Cells(1, Columns.Count).End(xlToLeft).Column
 
    'nbLigne = 1000
    'nbColonne = 96
 
    'MsgBox nbColonne
 
    state = False
    lastColor = 15
 
    For i = 1 To nbLigne
 
        If Range("Z1").Offset(i, 0).Value = Range("Z1").Offset(i + 1, 0).Value Then
 
            For n = 0 To nbColonne
 
                If state = False And lastColor = 15 Then
                    Range("A1").Offset(i, n).Interior.Color = RGB(200, 200, 200)
                    Range("A1").Offset(i + 1, n).Interior.Color = RGB(200, 200, 200)
                    lastColor = 0
                    state = True
                ElseIf state = False And lastColor = 0 Then
                    lastColor = 15
                    Range("A1").Offset(i, n).Interior.Color = RGB(200 + lastColor, 200 + lastColor, 200 + lastColor)
                    Range("A1").Offset(i + 1, n).Interior.Color = RGB(200 + lastColor, 200 + lastColor, 200 + lastColor)
                    lastColor = 15
                    state = True
                Else
                    Range("A1").Offset(i, n).Interior.Color = RGB(200 + lastColor, 200 + lastColor, 200 + lastColor)
                    Range("A1").Offset(i + 1, n).Interior.Color = RGB(200 + lastColor, 200 + lastColor, 200 + lastColor)
                End If
 
            Next n
 
            state = True
        Else
            state = False
 
        End If
 
    Next i
 
End Sub
Avez vous une idée pour optimiser mon code ?

Merci beaucoup de votre aide
iMaTh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/09/2011, 16h13   #2
Membre du Club
 
Inscription : décembre 2005
Messages : 90
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 90
Points : 47
Points : 47
Aller je m'auto répond, voici une solution qui est plutot rapide pour 34 000 lignes : environs 3 secondes.

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 
Sub regroupLines()
 
    Dim lastColo As Integer
    Dim nbLigne As Long
    Dim nbColonne As Long
    Dim state As Boolean
 
    state = False
    lastColor = 200
 
    nbLigne = Range("B65536").End(xlUp).Row
    nbColonne = Cells(1, Columns.Count).End(xlToLeft).Column
 
    For i = 1 To nbLigne
 
        If Range("Z1").Offset(i, 0).Value = Range("Z1").Offset(i + 1, 0).Value Then
 
            state = False
 
            Range("A1:CR1").Offset(i, 0).Interior.Color = RGB(lastColor, lastColor, lastColor)
            Range("A1:CR1").Offset(i + 1, 0).Interior.Color = RGB(lastColor, lastColor, lastColor)
 
        Else
 
            If state = True Then
 
                Range("A1:CR1").Offset(i, 0).Interior.Color = RGB(lastColor, lastColor, lastColor)
                Range("A1:CR1").Offset(i + 1, 0).Interior.Color = RGB(lastColor, lastColor, lastColor)
 
            End If
 
            state = True
 
            If lastColor = 200 Then
                lastColor = 215
            Else
                lastColor = 200
            End If
 
        End If
 
    Next i
 
End Sub
iMaTh est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/09/2011, 16h55   #3
Expert Confirmé Sénior
 
Avatar de mercatog
 
Inscription : juillet 2008
Messages : 5 848
Détails du profil
Informations forums :
Inscription : juillet 2008
Messages : 5 848
Points : 13 907
Points : 13 907
Bonjour
Une proposition
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sub Test()
Dim nbLigne As Long, i As Long, Clr As Long
Dim State As Boolean
Dim Plage As Range
 
Application.ScreenUpdating = False
With Worksheets("Feuil1")                                  'A ADAPTER AU NOM DE TA FEUILLE
    Set Plage = .UsedRange
    nbLigne = Plage.Rows.Count
    For i = 1 To nbLigne - 1
        If .Range("Z" & i) <> .Range("Z" & i + 1) Then State = Not State
        Clr = 200 - 15 * State
        Intersect(Plage, .Rows(i + 1)).Interior.Color = RGB(Clr, Clr, Clr)
    Next i
    Set Plage = Nothing
End With
End Sub
__________________
Cordialement.
mercatog est déconnecté   Envoyer un message privé Réponse avec citation 30
Vieux 05/09/2011, 10h05   #4
Membre du Club
 
Inscription : décembre 2005
Messages : 90
Détails du profil
Informations forums :
Inscription : décembre 2005
Messages : 90
Points : 47
Points : 47
Ok je rivalise pas, ton code est carrément plus light pour un résultat performant.

Un grand merci
iMaTh est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 23h27.


 
 
 
 
Partenaires

Hébergement Web