Temps d'exécution macro

Bonjour,

Je trouve ma macro trop longue : 2.25... secondes

Sub essai()
Dim Ws As Worksheet
Dim Wsh As Worksheet
Dim MoisEnCours As Integer
Dim AnneeEnCours As Integer
Dim Ligne As Integer
Dim Col As Integer
Dim Cel As Range
Dim x As Range
Dim start As Single

start = Timer
Application.ScreenUpdating = False

Set Ws = Worksheets("Machin")
Set Wsh = Worksheets("Truc")

MoisEnCours = Month(Date)
AnneeEnCours = Year(Date)
NomDuMois = MonthName(MoisEnCours)

Set x = Ws.Range("B1:M1").Find(MoisEnCours, , xlValues, xlWhole, , , False) 'initialise la variable objet x de la plage B1:M1 qui contient le numéro du mois en cours

For Each Cel In Ws.Range("A17:A28")
LigCel = Cel.Row
    For Each Cat In Wsh.Range("F2:F9999")
    ligcat = Cat.Row
        If Wsh.Cells(ligcat, 1) = AnneeEnCours And Wsh.Cells(ligcat, 2) = NomDuMois Then
            If Wsh.Cells(ligcat, 6) = Cel Or Wsh.Cells(ligcat, 7) = Cel Then
                Montant = Wsh.Cells(ligcat, 5)
                If Not x Is Nothing Then ' signifie est-ce que ma variable x représente la cellule correspondant au n° du mois en cours
'                    MsgBox x.Column
                    Col = x.Column ' définit la colonne contenant la variable x
                        If Ws.Cells(LigCel, 1) Like Cel Then
                            Cells(LigCel, 15).Clear
                            If Cells(LigCel, Col) = "" Then
                                Cells(LigCel, 15) = Montant
                            End If
                        End If
                End If
            End If
        End If
    Next Cat
Next Cel

Set x = Nothing
Set Ws = Nothing
Set Wsh = Nothing

MsgBox "durée du traitement: " & Timer - start & " secondes"
End Sub

y a-t-il moyen de faire moins long ?

Daniel

bonjour,

y a-t-il moyen de faire moins long ?

sûrement,

1) désactiver la gestion des événements et le recalcul automatique

2) adapter la fin de la boucle cat au nombre réel de lignes utilisé

3) vérifier si on ne peut pas sortir de la boucle cat dès qu'une concordance a été trouvée

4) travailler avec des array VBA , si nombre de lignes est important

pour avancer sur cette proposition merci de mettre un fichier test représentatif de tes données.

merci pour la réponse.

arpès avoir défini la dernière ligne, j'ai réduit la boucle Cat de la manière suivante

For Each Cat In Wsh.Range(Cells(2, 6), Cells(Derligne, 6))

pb : la méthode range de l'objet worksheet a échoué

bonjour,

la syntaxe correcte est contre-intuitive.

Range(wsh.Cells(2, 6), wsh.Cells(Derligne, 6))

ou

wsh.Range(wsh.Cells(2, 6), wsh.Cells(Derligne, 6))

Curieuse synthaxe

ça fonctionne, merci

Le temps d'exécution a passé à 1,16 sec

En modifiant les lignes

If Wsh.Cells(ligcat, 6) = Cel Or Wsh.Cells(ligcat, 7) = Cel Then
                Montant = Wsh.Cells(ligcat, 5)

par

If Cells(ligcat, 6) = Cel Or Cells(ligcat, 7) = Cel Then
                Montant = Cells(ligcat, 5)

le temps d'exécition passe à 1.95 E-02 secondes.

Super

Rechercher des sujets similaires à "temps execution macro"