IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Simplifier une macro


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 300
    Par défaut Simplifier une macro
    Bonjour

    J'aimerais savoir s'il est possible de simplifier cette macro afin qu'elle s’exécute plus rapidement.

    merci.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    Private Sub CB_Valider_Click()
     
        ActiveCell.Offset(0, 1).Value = CDate(Plannings.CBH1)
    Plannings.CBH1 = Format(Plannings.CBH1, "hh:mm")
        ActiveCell.Offset(0, 2).Value = CDate(Plannings.CBH2)
    Plannings.CBH1 = Format(Plannings.CBH2, "hh:mm")
        ActiveCell.Offset(0, 3).Value = CDate(Plannings.CBH3)
    Plannings.CBH1 = Format(Plannings.CBH3, "hh:mm")
        ActiveCell.Offset(0, 4).Value = CDate(Plannings.CBH4)
    Plannings.CBH1 = Format(Plannings.CBH4, "hh:mm")
     
        ActiveCell.Offset(1, 1).Value = CDate(Plannings.CBH5)
    Plannings.CBH1 = Format(Plannings.CBH5, "hh:mm")
        ActiveCell.Offset(1, 2).Value = CDate(Plannings.CBH6)
    Plannings.CBH1 = Format(Plannings.CBH6, "hh:mm")
        ActiveCell.Offset(1, 3).Value = CDate(Plannings.CBH7)
    Plannings.CBH1 = Format(Plannings.CBH7, "hh:mm")
        ActiveCell.Offset(1, 4).Value = CDate(Plannings.CBH8)
    Plannings.CBH1 = Format(Plannings.CBH8, "hh:mm")
     
        ActiveCell.Offset(2, 1).Value = CDate(Plannings.CBH9)
    Plannings.CBH1 = Format(Plannings.CBH9, "hh:mm")
        ActiveCell.Offset(2, 2).Value = CDate(Plannings.CBH10)
    Plannings.CBH1 = Format(Plannings.CBH10, "hh:mm")
        ActiveCell.Offset(2, 3).Value = CDate(Plannings.CBH11)
    Plannings.CBH1 = Format(Plannings.CBH11, "hh:mm")
        ActiveCell.Offset(2, 4).Value = CDate(Plannings.CBH12)
    Plannings.CBH1 = Format(Plannings.CBH12, "hh:mm")
     
        ActiveCell.Offset(3, 1).Value = CDate(Plannings.CBH13)
    Plannings.CBH1 = Format(Plannings.CBH13, "hh:mm")
        ActiveCell.Offset(3, 2).Value = CDate(Plannings.CBH14)
    Plannings.CBH1 = Format(Plannings.CBH14, "hh:mm")
        ActiveCell.Offset(3, 3).Value = CDate(Plannings.CBH15)
    Plannings.CBH1 = Format(Plannings.CBH15, "hh:mm")
        ActiveCell.Offset(3, 4).Value = CDate(Plannings.CBH16)
    Plannings.CBH1 = Format(Plannings.CBH16, "hh:mm")
     
        ActiveCell.Offset(4, 1).Value = CDate(Plannings.CBH17)
    Plannings.CBH1 = Format(Plannings.CBH17, "hh:mm")
        ActiveCell.Offset(4, 2).Value = CDate(Plannings.CBH18)
    Plannings.CBH1 = Format(Plannings.CBH18, "hh:mm")
        ActiveCell.Offset(4, 3).Value = CDate(Plannings.CBH19)
    Plannings.CBH1 = Format(Plannings.CBH19, "hh:mm")
        ActiveCell.Offset(4, 4).Value = CDate(Plannings.CBH20)
    Plannings.CBH1 = Format(Plannings.CBH20, "hh:mm")
     
        ActiveCell.Offset(5, 1).Value = CDate(Plannings.CBH21)
    Plannings.CBH1 = Format(Plannings.CBH21, "hh:mm")
        ActiveCell.Offset(5, 2).Value = CDate(Plannings.CBH22)
    Plannings.CBH1 = Format(Plannings.CBH22, "hh:mm")
        ActiveCell.Offset(5, 3).Value = CDate(Plannings.CBH23)
    Plannings.CBH1 = Format(Plannings.CBH23, "hh:mm")
        ActiveCell.Offset(5, 4).Value = CDate(Plannings.CBH24)
    Plannings.CBH1 = Format(Plannings.CBH24, "hh:mm")
     
        ActiveCell.Offset(6, 1).Value = CDate(Plannings.CBH25)
    Plannings.CBH1 = Format(Plannings.CBH25, "hh:mm")
        ActiveCell.Offset(6, 2).Value = CDate(Plannings.CBH26)
    Plannings.CBH1 = Format(Plannings.CBH26, "hh:mm")
        ActiveCell.Offset(6, 3).Value = CDate(Plannings.CBH27)
    Plannings.CBH1 = Format(Plannings.CBH27, "hh:mm")
        ActiveCell.Offset(6, 4).Value = CDate(Plannings.CBH28)
    Plannings.CBH1 = Format(Plannings.CBH28, "hh:mm")
     
    End Sub

  2. #2
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2018
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 29
    Localisation : France, Bas Rhin (Alsace)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2018
    Messages : 2
    Par défaut
    Bonjour,

    Tu peux essayer d'ajouter en début de code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.ScreenUpdating = False
    Et en fin de code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.ScreenUpdating = True
    Cela permet de désactiver les animations pendant l'exécution de la macro.

    A+
    Wadaris

  3. #3
    Rédacteur/Modérateur

    Avatar de Jean-Philippe André
    Homme Profil pro
    Architecte Power Platform, ex-Développeur VBA/C#/VB.Net
    Inscrit en
    Juillet 2007
    Messages
    14 682
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Canada

    Informations professionnelles :
    Activité : Architecte Power Platform, ex-Développeur VBA/C#/VB.Net
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2007
    Messages : 14 682
    Par défaut
    Salut,
    Tu peux également passer par une boucle

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Dim x As Integer, y As Integer
    x=0
    y=0
    For i = 1 To 28
    y=y+1
    if y=5 Then
    x=x+1
    y=1
    End If
     ActiveCell.Offset(x,y).Value = CDate(Plannings.Controls("CBH"&i))
    Plannings.Controls("CBH"&i) = Format(Plannings.Controls("CBH"&i), "hh:mm")
    Next i
    Cycle de vie d'un bon programme :
    1/ ça fonctionne 2/ ça s'optimise 3/ ça se refactorise

    Pas de question technique par MP, je ne réponds pas

    Mes ouvrages :
    Migrer les applications VBA Access et VBA Excel vers la Power Platform
    Apprendre à programmer avec Access 2016, Access 2019 et 2021

    Apprendre à programmer avec VBA Excel
    Prise en main de Dynamics 365 Business Central

    Coffrets disponibles de mes ouvrages : https://www.editions-eni.fr/jean-philippe-andre
    Pensez à consulter la FAQ Excel et la FAQ Access

    Derniers tutos
    Excel et les paramètres régionaux
    Les fichiers Excel binaires : xlsb,

    Autres tutos

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    300
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 300
    Par défaut
    merci d'avoir répondu.

    j'ai essayé les deux manières, cela ne change pas grand chose, 1 seconde de différence (20 secondes au total pour exécuter la macro) encore un peu long.

  5. #5
    Membre Expert Avatar de Transitoire
    Homme Profil pro
    Auditeur informatique
    Inscrit en
    Décembre 2017
    Messages
    733
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 71
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Auditeur informatique

    Informations forums :
    Inscription : Décembre 2017
    Messages : 733
    Par défaut
    Bonjour
    Je ne sais pas pour la boucle, mais le non rafraichissement de l'écran par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Application.ScreenUpdating = False
    ................................
    Application.ScreenUpdating = True
    N'est efficace en gain de temps, qu'avec l'utilisation intempestive des .Select et .Activate. Sur un programme de mes débuts, j'avais divisé par 4 le temps d'éxécution d'une macro.
    Dans votre cas, ça ne pouvait rien faire gagner!
    Cordialement

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Février 2007
    Messages
    2 266
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 2 266
    Par défaut
    Bonjour,

    le non rafraichissement de l'écran par Application.ScreenUpdating = False N'est efficace en gain de temps, qu'avec l'utilisation intempestive des .Select et .Activate.
    Je ne suis pas d'accord. Sur une écriture sur feuille (sans Select ni Activate) ça fait gagner de 20 % à 25% de temps. S'il y a en a beaucoup ça n'est plus négligeable.

    Un essai, non testé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Private Sub CB_Valider_Click()
        Dim tabl(0 To 6, 1 To 4) As Date
        Dim i As Long, j As Long
        Application.ScreenUpdating = False
        For i = 0 To 6
            For j = 1 To 4
                tabl(i, j) = CDate(Plannings.Controls("CBH" & i * 4 + j)) ' merci Jean-Philippe :-)
                Plannings.Controls("CBH" & i * 4 + j) = Format(Plannings.Controls("CBH" & i * 4 + j), "hh:mm")
            Next j
        Next i
        ActiveCell.Resize(7, 4) = tabl
    End Sub
    Ecriture des cellules en une fois. Mais comme il y a les mise à jours des contrôles, dans le doute je préfère laisser ScreenUpdating = False
    Cette mise à jours des contrôles est-elle indispensable ?
    eric

Discussions similaires

  1. [XL-2007] Simplifier une macro
    Par INFINITY100 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 08/05/2015, 17h06
  2. Simplifier une macro
    Par chito79000 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 06/05/2015, 11h22
  3. [XL-2007] Je cherche à simplifier une macro
    Par INFINITY100 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 06/05/2015, 04h55
  4. [XL-2010] Simplifier une macro
    Par Ardiden31 dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 14/04/2015, 14h14
  5. {VBA Excel} Simplifier une macro avec une copie
    Par Thomas69 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 21/06/2007, 14h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo