Bonjour à tous,
Grâce a ce forum, je commence de plus en plus à coder en VBA pour faciliter l'utilisation d'excel.
J'ai réalisé un code qui me permet de chercher une valeur se trouvant dans un autre onglet et d'y recopier les colonnes correspondantes.
Le probleme est que la copie de colonnes prend environ 1 seconde, et cela pour les 2500 lignes du fichier.
Je souhaiterai diminuer le plus possible ce temps d'execution, et je pense que ca soit possible vu l'aspect de mon code qui ne m'a pas l'air optimiser du tout.
voici le code
Sub RechercheCopie()
Dim Nom As String, i As Long, j As Long
Sheets(1).Select
i = 2
With Sheets(2)
Range("W2:AQ2623").Select
Selection.ClearContents
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Do While Cells(i, 1) <> ""
Nom = Cells(i, 1)
For j = 2 To .Range("A65536").End(xlUp).Row
If (Nom = .Cells(j, 1) And .Cells(j, 2) <> "") Then
Cells(i, 23) = .Cells(j, 2)
Cells(i, 24) = .Cells(j, 3)
Cells(i, 25) = .Cells(j, 4)
Cells(i, 26) = .Cells(j, 5)
Cells(i, 27) = .Cells(j, 6)
Cells(i, 28) = .Cells(j, 7)
Cells(i, 29) = .Cells(j, 8)
Cells(i, 30) = .Cells(j, 9)
Cells(i, 31) = .Cells(j, 10)
Cells(i, 32) = .Cells(j, 11)
Cells(i, 33) = .Cells(j, 17)
Cells(i, 34) = .Cells(j, 18)
Cells(i, 35) = .Cells(j, 35)
If .Cells(j, 35) = .Cells(j, 13) Then
Cells(i, 35).Font.Color = -16776961
End If
Cells(i, 36) = .Cells(j, 37)
If .Cells(j, 37) = .Cells(j, 15) Then
Cells(i, 36).Font.Color = -16776961
End If
Cells(i, 37) = .Cells(j, 49)
If .Cells(j, 49) = .Cells(j, 20) Then
Cells(i, 37).Font.Color = -16776961
End If
Cells(i, 38) = .Cells(j, 51)
If .Cells(j, 51) = .Cells(j, 22) Then
Cells(i, 38).Font.Color = -16776961
End If
Cells(i, 39) = .Cells(j, 39)
If .Cells(j, 39) = .Cells(j, 24) Then
Cells(i, 39).Font.Color = -16776961
End If
Cells(i, 40) = .Cells(j, 41)
If .Cells(j, 41) = .Cells(j, 27) Then
Cells(i, 40).Font.Color = -16776961
End If
Cells(i, 41) = .Cells(j, 43)
If .Cells(j, 43) = .Cells(j, 29) Then
Cells(i, 41).Font.Color = -16776961
End If
Cells(i, 42) = .Cells(j, 45)
If .Cells(j, 45) = .Cells(j, 31) Then
Cells(i, 42).Font.Color = -16776961
End If
Cells(i, 43) = .Cells(j, 47)
If .Cells(j, 47) = .Cells(j, 33) Then
Cells(i, 43).Font.Color = -16776961
End If
Exit For
End If
Next
i = i + 1
Loop
End With
End Sub
Merci d'avance pour vos suggestions.
Grâce a ce forum, je commence de plus en plus à coder en VBA pour faciliter l'utilisation d'excel.
J'ai réalisé un code qui me permet de chercher une valeur se trouvant dans un autre onglet et d'y recopier les colonnes correspondantes.
Le probleme est que la copie de colonnes prend environ 1 seconde, et cela pour les 2500 lignes du fichier.
Je souhaiterai diminuer le plus possible ce temps d'execution, et je pense que ca soit possible vu l'aspect de mon code qui ne m'a pas l'air optimiser du tout.
voici le code
Code:
Sub RechercheCopie()
Dim Nom As String, i As Long, j As Long
Sheets(1).Select
i = 2
With Sheets(2)
Range("W2:AQ2623").Select
Selection.ClearContents
With Selection.Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Do While Cells(i, 1) <> ""
Nom = Cells(i, 1)
For j = 2 To .Range("A65536").End(xlUp).Row
If (Nom = .Cells(j, 1) And .Cells(j, 2) <> "") Then
Cells(i, 23) = .Cells(j, 2)
Cells(i, 24) = .Cells(j, 3)
Cells(i, 25) = .Cells(j, 4)
Cells(i, 26) = .Cells(j, 5)
Cells(i, 27) = .Cells(j, 6)
Cells(i, 28) = .Cells(j, 7)
Cells(i, 29) = .Cells(j, 8)
Cells(i, 30) = .Cells(j, 9)
Cells(i, 31) = .Cells(j, 10)
Cells(i, 32) = .Cells(j, 11)
Cells(i, 33) = .Cells(j, 17)
Cells(i, 34) = .Cells(j, 18)
Cells(i, 35) = .Cells(j, 35)
If .Cells(j, 35) = .Cells(j, 13) Then
Cells(i, 35).Font.Color = -16776961
End If
Cells(i, 36) = .Cells(j, 37)
If .Cells(j, 37) = .Cells(j, 15) Then
Cells(i, 36).Font.Color = -16776961
End If
Cells(i, 37) = .Cells(j, 49)
If .Cells(j, 49) = .Cells(j, 20) Then
Cells(i, 37).Font.Color = -16776961
End If
Cells(i, 38) = .Cells(j, 51)
If .Cells(j, 51) = .Cells(j, 22) Then
Cells(i, 38).Font.Color = -16776961
End If
Cells(i, 39) = .Cells(j, 39)
If .Cells(j, 39) = .Cells(j, 24) Then
Cells(i, 39).Font.Color = -16776961
End If
Cells(i, 40) = .Cells(j, 41)
If .Cells(j, 41) = .Cells(j, 27) Then
Cells(i, 40).Font.Color = -16776961
End If
Cells(i, 41) = .Cells(j, 43)
If .Cells(j, 43) = .Cells(j, 29) Then
Cells(i, 41).Font.Color = -16776961
End If
Cells(i, 42) = .Cells(j, 45)
If .Cells(j, 45) = .Cells(j, 31) Then
Cells(i, 42).Font.Color = -16776961
End If
Cells(i, 43) = .Cells(j, 47)
If .Cells(j, 47) = .Cells(j, 33) Then
Cells(i, 43).Font.Color = -16776961
End If
Exit For
End If
Next
i = i + 1
Loop
End With
End Sub
Code:
Merci d'avance pour vos suggestions.