Script qui prend beaucoup de temps à exécuter

Résolu
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 - 30 août 2007 à 22:02
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 - 31 août 2007 à 00:17
Bonjour à tous,


Dans mon script, je trouve qu'il prend beaucoup de temps à s'exécuter, environ
15 à 20 secondes et je ne sais pas pourquoi.


Y a-t-il moyen d'accélérer l'application reliée au script?



Voici le script:

<hr size="2" width="100%" />Private Sub CommandButton3_Click()
                                                                 
                              
 'RECHERCHE D'INFO DANS LE FICHIER EXCEL

msg = "VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?"
Style = vbYesNo + vbDefaultButton1
Réponse = MsgBox(msg, Style, Title)
If Réponse = vbYes Then
Application.ScreenUpdating = False
ComboBox2 = ComboBox1
Sheets(TextBox2.Text).Select
Dim Recherche As Range, Ligne As Long
'colonne C = numéro de commande
    Set Recherche = Columns("B:B").Find(ComboBox1)
    If Not Recherche Is Nothing Then
       Ligne = Recherche.Row
       ComboBox10 = Range("E" & Ligne)
       ComboBox11 = Range("F" & Ligne)
       ComboBox12 = Range("G" & Ligne)
       ComboBox13 = Range("H" & Ligne)
       ComboBox14 = Range("I" & Ligne)
       ComboBox15 = Range("J" & Ligne)
       ComboBox16 = Range("K" & Ligne)
      
       ComboBox3 = Range("D" & Ligne)
       ComboBox4 = Range("D" & Ligne)
       ComboBox5 = Range("D" & Ligne)
       ComboBox6 = Range("D" & Ligne)
       ComboBox7 = Range("D" & Ligne)
       ComboBox8 = Range("D" & Ligne)
       ComboBox9 = Range("D" & Ligne)
      

       If ComboBox10 = vbNullString Then
       ComboBox3 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox11 = vbNullString Then
       ComboBox4 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox12 = vbNullString Then
       ComboBox5 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox13 = vbNullString Then
       ComboBox6 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox14 = vbNullString Then
       ComboBox7 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox15 = vbNullString Then
       ComboBox8 = vbNullString
       ActiveWorkbook.Save
          End If
         
          If ComboBox16 = vbNullString Then
       ComboBox9 = vbNullString
       ActiveWorkbook.Save
          End If
         
Sheets(TextBox1.Text).Select

End If

                                    'ENTRER LES INFO DANS LE FICHIER EXCEL

'colonne C = numéro de commande
    Set Recherche = Columns("A:A").Find(ComboBox1)
    If Not Recherche Is Nothing Then
       Ligne = Recherche.Row
       Range("H" & Ligne) = ComboBox3
       Range("J" & Ligne) = ComboBox4
       Range("L" & Ligne) = ComboBox5
       Range("N" & Ligne) = ComboBox6
       Range("P" & Ligne) = ComboBox7
       Range("R" & Ligne) = ComboBox8
       Range("T" & Ligne) = ComboBox9
       Range("H" & Ligne + 1) = ComboBox10
       Range("J" & Ligne + 1) = ComboBox11
       Range("L" & Ligne + 1) = ComboBox12
       Range("N" & Ligne + 1) = ComboBox13
       Range("P" & Ligne + 1) = ComboBox14
       Range("R" & Ligne + 1) = ComboBox15
       Range("T" & Ligne + 1) = ComboBox16
       Range("V" & Ligne + 1) = ComboBox2
       ActiveWorkbook.Save
          End If
       End If
Application.ScreenUpdating = True
End Sub

<hr size="2" width="100%" />Merci de vôtre aide! 

9 réponses

mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
31 août 2007 à 00:03
oui, autant pour moi, remplace i - 5 PAR j

@++

<hr width="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
3
relax62 Messages postés 82 Date d'inscription vendredi 2 avril 2004 Statut Membre Dernière intervention 8 juillet 2012 1
30 août 2007 à 23:18
Bonsoir,

Difficile à dire,

1)Déjà commencer par déclarer TOUTES les variables .
En effet chaque variable non déclaré est considéré comme Variant (qui prend énormément de mémoire).
ex : msg = "VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?"

Comme nous savons déjà que c'est du texte autant lui dire en déclarant :
dim msg as string
msg = "VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?"

Peut-être que tu ne gagneras pas en vitesse mais en mémoire c'est sur !

Tu peux meme compresser pour aller plus vite:
If  MsgBox("VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?", vbYesNo + vbDefaultButton1, "Titre") = vbYes Then

2)Apparemment dans ton code selon les conditions, tu fais appel plusieurs fois à ActiveWorkbook.save
à mon avis, ça doit prendre du temps.

Si tu veux sauvegarder selon certaines conditions, crée une variable du genre:
dim Sauver as boolean
Sauver=False

...
if then
    Sauver=true
endif
...
et à la fin
if Sauver then activeworkbook.save

Je n'ai pas excel sur le pc d'ou je te repond donc ne prend pas garde si le code n'est pas tout à fait exact.

A+

Relx62
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
30 août 2007 à 23:35
salut Avyrex,

je t'ai quelques petites modifs, mais dans l'ensemble, c'est pas mal  ;)
Je ne sais pas si ça va changer le temps d'exécution (si oui, de très peu). En cas, tiens moi au courant :

Private Sub CommandButton3_Click()
    Dim bSave As Boolean, i As Long, j As Long
                               
'RECHERCHE D'INFO DANS LE FICHIER EXCEL

'msg = "VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?"
'Style = vbYesNo + vbDefaultButton1
'Réponse = MsgBox("VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?", vbYesNo + vbDefaultButton1, Title)
If MsgBox("VOULEZ-VOUS VRAIMENT ENREGISTER LES CHANGEMENTS?", vbYesNo + vbDefaultButton1, Title) = vbYes Then
Application.ScreenUpdating = False
ComboBox2 = ComboBox1
Sheets(TextBox2.Text).Select

Dim Recherche As Range, Ligne As Long
'colonne C = numéro de commande
    Set Recherche = Columns("B:B").Find(ComboBox1)
    If Not Recherche Is Nothing Then
        Ligne = Recherche.Row
       
        For i = 3 To 9
            Me.Controls("Combobox" & i) = Range("D" & Ligne)
        Next i
        
        For i = 10 To 16
            Me.Controls("Combobox" & i) = Cells(Ligne, i - 5)
        Next i
       
        If ComboBox10 = vbNullString Then
            ComboBox3 = vbNullString
            bSave = True
        End If
          
        If ComboBox11 = vbNullString Then
            ComboBox4 = vbNullString
            bSave = True
        End If
          
        If ComboBox12 = vbNullString Then
            ComboBox5 = vbNullString
            bSave = True
        End If
          
        If ComboBox13 = vbNullString Then
            ComboBox6 = vbNullString
            bSave = True
        End If
          
        If ComboBox14 = vbNullString Then
            ComboBox7 = vbNullString
            bSave = True
        End If
          
        If ComboBox15 = vbNullString Then
            ComboBox8 = vbNullString
            bSave = True
        End If
          
        If ComboBox16 = vbNullString Then
            ComboBox9 = vbNullString
            bSave = True
        End If
        
If bSave Then ActiveWorkbook.Save
          
Sheets(TextBox1.Text).Select

End If

                                    'ENTRER LES INFO DANS LE FICHIER EXCEL

'colonne C = numéro de commande
    Set Recherche = Columns("A:A").Find(ComboBox1)
    If Not Recherche Is Nothing Then
        Ligne = Recherche.Row
        j = 8
        For i = 3 To 9
            Me.Controls("Combobox" & i) = Cells(Ligne, j)
            j = j + 2
        Next i
        
        j = 8
        For i = 10 To 16
            Me.Controls("Combobox" & i) = Cells(Ligne, i - 5)
            j = j + 2
        Next i

        Range("V" & Ligne + 1) = ComboBox2
       
        ActiveWorkbook.Save
    End If
Application.ScreenUpdating = True
End Sub

~ <small> Mortalino ~ Colorisation automatique </small>

@++

<hr width ="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
30 août 2007 à 23:36
Bien vu Relax62 (posté sans avoir vu ton message).
Je vois que les idées se rejoingnent 

@++

<hr width="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
30 août 2007 à 23:39
Attention, dans ma dernière boucle (for i = 10 to 16), j'ai omis le + 1 à la variable Ligne

@++

<hr width ="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
0
relax62 Messages postés 82 Date d'inscription vendredi 2 avril 2004 Statut Membre Dernière intervention 8 juillet 2012 1
30 août 2007 à 23:51
Oui Mortalino, mais j'ai été plus fainéant que toi pour répondre.

Comme quoi, il peut y avoir 36 façons de programmer différemment, la philosophie reste la même.

oula faut que j'aille me coucher la (trop de vba aujourd'hui!)

++
0
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 3
30 août 2007 à 23:56
Merci beaucoup,

Tous fonctionne sauf pour cette section:

Me.Controls("Combobox" & i) = Cells(Ligne + 1, i - 5)
0
avyrex1926 Messages postés 360 Date d'inscription dimanche 3 décembre 2006 Statut Membre Dernière intervention 3 janvier 2012 3
31 août 2007 à 00:12
Super, je viens de baisser à 9-10 secondes au lieu de 20 seconde d'exécution.

Merci
0
mortalino Messages postés 6786 Date d'inscription vendredi 16 décembre 2005 Statut Membre Dernière intervention 21 décembre 2011 18
31 août 2007 à 00:17
Aahhhh beh ça me fait plaisir, car je n'étais pas sûr d'une si forte amélioration.

Bonne continuation

@++

<hr width="100%" size="2" />( Nouveau forum : Exclusivement Office & VBA/STRONG>
0
Rejoignez-nous