Accélerer vitesse d'exécution d'un code VBA
Bonjour,
Désormais, mon tableur Excel est, a priori, tout à fait fonctionnel ; Grâce à l'aide de la communauté Excel-pratique, sans laquelle je serais encore derrière mon écran à m'arracher les cheveux.
Il reste des détails, notamment la vitesse d'exécution du code qui m’embête sur une des feuilles.
Voici cette partie de code :
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Column
Case 7, 8, 9, 10, 11, 12, 13 'colonne j, k, l et m
Dim w As Long
'Accélérer le remplissage des champs
Dim g1 As Long
Dim ws2c, ws3c As Worksheet
'Dim valeur As Integer
Set ws3c = Worksheets("2")
g1 = ws3c.Cells(Rows.Count, 1).End(xlUp).Row
For f = 4 To g1
If Cells(f, 7) = 1 Then Cells(f, 7) = "Direct"
If Cells(f, 7) = 2 Then Cells(f, 7) = "Indirect"
If Cells(f, 8) = 1 Then Cells(f, 8) = "Permanente"
If Cells(f, 8) = 2 Then Cells(f, 8) = "Temporaire"
If Cells(f, 9) = 1 Then Cells(f, 9) = "Locale"
If Cells(f, 9) = 2 Then Cells(f, 9) = "Régionale"
If Cells(f, 10) = 1 Then Cells(f, 10) = "- - -"
If Cells(f, 10) = 2 Then Cells(f, 10) = "- -"
If Cells(f, 10) = 3 Then Cells(f, 10) = "-"
If Cells(f, 10) = 4 Then Cells(f, 10) = "+"
If Cells(f, 11) = 1 Then Cells(f, 11) = "Très fort"
If Cells(f, 11) = 2 Then Cells(f, 11) = "Fort"
If Cells(f, 11) = 3 Then Cells(f, 11) = "Modéré"
If Cells(f, 11) = 4 Then Cells(f, 11) = "Faible"
If Cells(f, 11) = 5 Then Cells(f, 11) = "Très faible"
If Cells(f, 11) = 6 Then Cells(f, 11) = "Nul"
If Cells(f, 12) = 1 Then Cells(f, 12) = "Très fort"
If Cells(f, 12) = 2 Then Cells(f, 12) = "Fort"
If Cells(f, 12) = 3 Then Cells(f, 12) = "Modéré"
If Cells(f, 12) = 4 Then Cells(f, 12) = "Faible"
If Cells(f, 12) = 5 Then Cells(f, 12) = "Très faible"
If Cells(f, 12) = 6 Then Cells(f, 12) = "Nul"
If Cells(f, 13) = 1 Then Cells(f, 13) = "Très fort"
If Cells(f, 13) = 2 Then Cells(f, 13) = "Fort"
If Cells(f, 13) = 3 Then Cells(f, 13) = "Modéré"
If Cells(f, 13) = 4 Then Cells(f, 13) = "Faible"
If Cells(f, 13) = 5 Then Cells(f, 13) = "Très faible"
If Cells(f, 13) = 6 Then Cells(f, 13) = "Nul"
If Cells(f, 11) = "Très fort" Then
Range(Cells(f, 11), Cells(f, 11)).Interior.Color = RGB(192, 0, 0)
Range(Cells(f, 11), Cells(f, 11)).Font.Color = RGB(255, 255, 255)
Else
If Cells(f, 11) = "Fort" Then
Range(Cells(f, 11), Cells(f, 11)).Interior.Color = RGB(255, 0, 0)
Range(Cells(f, 11), Cells(f, 11)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 11) = "Modéré" Then
Range(Cells(f, 11), Cells(f, 11)).Interior.Color = RGB(255, 192, 0)
Range(Cells(f, 11), Cells(f, 11)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 11) = "Faible" Then
Range(Cells(f, 11), Cells(f, 11)).Interior.Color = RGB(255, 255, 153)
Range(Cells(f, 11), Cells(f, 11)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 11) <> "Très fort" Then 'Or "Fort" Or "Modéré" Or "Faible"
Range(Cells(f, 11), Cells(f, 11)).Interior.Color = RGB(255, 255, 255)
Range(Cells(f, 11), Cells(f, 11)).Font.Color = RGB(0, 0, 0)
Else
End If
End If
End If
End If
End If
If Cells(f, 12) = "Très fort" Then
Range(Cells(f, 12), Cells(f, 12)).Interior.Color = RGB(192, 0, 0)
Range(Cells(f, 12), Cells(f, 12)).Font.Color = RGB(255, 255, 255)
Else
If Cells(f, 12) = "Fort" Then
Range(Cells(f, 12), Cells(f, 12)).Interior.Color = RGB(255, 0, 0)
Range(Cells(f, 12), Cells(f, 12)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 12) = "Modéré" Then
Range(Cells(f, 12), Cells(f, 12)).Interior.Color = RGB(255, 192, 0)
Range(Cells(f, 12), Cells(f, 12)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 12) = "Faible" Then
Range(Cells(f, 12), Cells(f, 12)).Interior.Color = RGB(255, 255, 153)
Range(Cells(f, 12), Cells(f, 12)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 12) <> "Très fort" Then 'Or "Fort" Or "Modéré" Or "Faible"
Range(Cells(f, 12), Cells(f, 12)).Interior.Color = RGB(255, 255, 255)
Range(Cells(f, 12), Cells(f, 12)).Font.Color = RGB(0, 0, 0)
End If
End If
End If
End If
End If
If Cells(f, 13) = "Très fort" Then
Range(Cells(f, 13), Cells(f, 13)).Interior.Color = RGB(192, 0, 0)
Range(Cells(f, 13), Cells(f, 13)).Font.Color = RGB(255, 255, 255)
Else
If Cells(f, 13) = "Fort" Then
Range(Cells(f, 13), Cells(f, 13)).Interior.Color = RGB(255, 0, 0)
Range(Cells(f, 13), Cells(f, 13)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 13) = "Modéré" Then
Range(Cells(f, 13), Cells(f, 13)).Interior.Color = RGB(255, 192, 0)
Range(Cells(f, 13), Cells(f, 13)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 13) = "Faible" Then
Range(Cells(f, 13), Cells(f, 13)).Interior.Color = RGB(255, 255, 153)
Range(Cells(f, 13), Cells(f, 13)).Font.Color = RGB(0, 0, 0)
Else
If Cells(f, 13) <> "Très fort" Then 'Or "Fort" Or "Modéré" Or "Faible"
Range(Cells(f, 13), Cells(f, 13)).Interior.Color = RGB(255, 255, 255)
Range(Cells(f, 13), Cells(f, 13)).Font.Color = RGB(0, 0, 0)
End If
End If
End If
End If
End If
Next
End Select
End Sub
C'est un code qui permet de convertir automatiquement des chiffres (1, 2, 3, 4, 5 et 6) en valeurs sur 7 colonnes différentes puis qui colorent ces valeurs dans 3 colonnes différentes.
Lorsque je saisie des chiffres, tout s'exécute comme souhaité, mais il y a des micro-latences. Si on essai d'aller plus vite, les latences s'accentuent, évidemment.
Auriez-vous des solutions pour accélérer l'exécution d'un code aussi long ?
J'ai cogité sur quelques idées, qu'il faut que je teste prochainement.
J'ai mis mon document en pièce jointe si vous souhaitez voir comment s'exécute le code.
Merci de votre attention,
Bonne fin de journée !
Bonsoir Le Drosophile,
L'idée qui me vient dans l'immédiat: supprimer toute la partie "colorisation" du code et utiliser la "Mise en forme conditionnelle" EXCEL...
Bonsoir,
Je suis passé au VBA car ça me plaisait pas. L'exécution des autres codes faisait que les plages de données évoluaient sans cesse, créant des milliers de MFC.
Sinon, en effet, la MFC est probablement plus rapide.
Bonjour,
Pourquoi cette boucle For f = 4 To g1
?
Tu es dans
Worksheet_Change(ByVal Target As Range)
, si tu as un changement sur la ligne Target.row seule cette ligne est susceptible d'avoir une modif à faire. Voire 2 à cause de ta fusion (réellement utile elle aussi ?)
A mon avis tu refais toutes les lignes inutilement.
En plus tu lis jusqu'à 6 fois la même cellule, chronophage... Lis une seule fois dans une variable et teste cette variable.
eric
Bonjour,
A tester.
Cdlt.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastRow As Long, rng As Range
If Target.Count = 1 Then
lastRow = Me.Cells(Rows.Count, 1).End(xlUp).Row + 1
If lastRow > 3 Then
Set rng = Me.Cells(4, 1).Resize(lastRow - 3, 13)
If Not Intersect(Target, rng) Is Nothing Then
If Target.Column > 6 Then
Application.EnableEvents = False
Select Case Target.Column
Case 7:
Select Case Target.Value
Case 1: Target.Value = "Direct"
Case 2: Target.Value = "Indirect"
End Select
Case 8:
Select Case Target.Value
Case 1: Target.Value = "Permanente"
Case 2: Target.Value = "Temporaire"
End Select
Case 9:
Select Case Target.Value
Case 1: Target.Value = "Locale"
Case 2: Target.Value = "Régionale"
End Select
Case 10:
Select Case Target.Value
Case 1: Target.Value = "- - -"
Case 2: Target.Value = "- -"
Case 3: Target.Value = "-"
Case 4: Target.Value = "+"
End Select
Case 11 To 13:
Select Case Target.Value
Case 1:
With Target
.Value = "Très fort"
.Interior.Color = RGB(192, 0, 0)
.Font.Color = RGB(255, 255, 255)
End With
Case 2:
With Target
.Value = "Fort"
.Interior.Color = RGB(255, 0, 0)
.Font.Color = RGB(0, 0, 0)
End With
Case 3:
With Target
.Value = "Modéré"
.Interior.Color = RGB(255, 192, 0)
.Font.Color = RGB(0, 0, 0)
End With
Case 4:
With Target
.Value = "Faible"
.Interior.Color = RGB(255, 255, 153)
.Font.Color = RGB(0, 0, 0)
End With
Case 5:
With Target
.Value = "Très faible"
.Interior.Color = RGB(255, 255, 153)
.Font.Color = RGB(0, 0, 0)
End With
Case 6:
With Target
.Value = "Nul"
.Interior.Color = RGB(255, 255, 153)
.Font.Color = RGB(0, 0, 0)
End With
End Select
End Select
Application.EnableEvents = True
End If
End If
End If
End If
End Sub
Bonsoir,
Merci beaucoup pour cette proposition de code ! Il fonctionne à merveille et ne cause aucune latence.
Du coup, je vais certainement l'appliquer à d'autres documents.
Bonne soirée !
A plus tard.