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 :

Optimiser un code


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 8
    Points : 4
    Points
    4
    Par défaut Optimiser un code
    Bonjour à tous!

    J'ai le code suivant

    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
                    Select Main Sheet
     
                    For Each Row In Sheets("Main").Rows
     
                        'MsgBox Row.Cells(6)
     
                        If Row.Cells(6) = Account Then
                            Row.Cells(23).Value = DateDebutArriere
                            'MsgBox (Now - DateDebutArriere)
                            Row.Cells(24).Value = CInt(Now - DateDebutArriere)
                            'MsgBox "Found"
                        'Else
                        '    Exit For
                        End If
                    Next
    Bon il est en marche sauf que ça prends beaucoup de temps d’exécution

    on m'a demandé de l'optimiser donc j'ai écris ce code

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
                  Sheets(main).Select
                  Dim Found As Range, indice As Integer
                  Set Found = main.Range("F2:F4000").Find(Account)
                  indice = Found.Row
                  MsgBox indice
                  Cells(W, 1).Value2 = DateDebutArriere
    j'ai eu l'erreur #9: l'indice n'appartient pas à la sélection

    Qu'en pensez-vous?

  2. #2
    Expert éminent sénior Avatar de mercatog
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    9 435
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations forums :
    Inscription : Juillet 2008
    Messages : 9 435
    Points : 31 877
    Points
    31 877
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Dim Found As Range
    Dim Indice As Long
     
    With Worksheets("main")
        Set Found = .Range("F2:F4000").Find(Account, LookIn:=xlValues, LookAt:=xlWhole)
        If Not Found Is Nothing Then
            Indice = Found.Row
            MsgBox Indice
            .Cells(Indice, 1).Value2 = DateDebutArriere
        End If
    End With

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    Merci beaucoup pour le code mais j'ai eu d'autres problèmes.
    Pour la date d'arriéré dans la sheet "Main" il me donne des valeurs erronés
    Je vous donne tout le code pour mieux comprendre

    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    Sub Macro1()
    '
    ' Macro1 Macro
    '
     
    '
    ' Actions in List Al Ech Sheet
     
        Sheets("List Al Ech").Select
     
        ' Nombre de Lignes
     
        Dim nb_lignes As Long
     
        nb_lignes = WorksheetFunction.CountA(Range("A:A"))
     
        'MsgBox "Nombre d'enregistrement : " & nb_lignes
     
        Dim CIN As Long
        Dim Account As String
        Dim Period As Integer
        Dim RepaymentDate As Date
        Dim TotalDue As Double
        Dim TotalRbtCcp As Double
        Dim TotalRbtEsp As Double
        Dim TotalRBT As Double
        Dim CapitalRestant As Double
     
        Dim DateDebutArriere As Date
        Dim DateTraitement As Date
        DateTraitement = Now
     
        'MsgBox DateTraitement
     
        Dim EnArriere As Boolean
     
        Dim numero_ligne As Long
     
        ' Boucle sur toutes les lignes
     
        For i = 2 To nb_lignes
        'For i = 2 To 1000
        'If IsNumeric(Range("A2")) Then
     
            numero_ligne = i
     
            'MsgBox "numero ligne : " & numero_ligne
     
            If numero_ligne >= 2 And numero_ligne <= nb_lignes Then
                CIN = Cells(numero_ligne, 1)
                Account = Cells(numero_ligne, 2)
                Period = Cells(numero_ligne, 3)
                RepaymentDate = Cells(numero_ligne, 4)
                TotalDue = Cells(numero_ligne, 5)
                TotalRbtCcp = Cells(numero_ligne, 9)
                TotalRbtEsp = Cells(numero_ligne, 10)
                TotalRBT = Cells(numero_ligne, 11)
                CapitalRestant = Cells(numero_ligne, 12)
     
                ' Si Period
                If Period = 1 Then
                    ' Period = 1
                    Cells(numero_ligne, 12).Value = TotalRBT - TotalDue
                    EnArriere = False
                Else
                    ' Period <> 1
                    Cells(numero_ligne, 12).Value = Cells(numero_ligne - 1, 12) - TotalDue
                End If
     
                'MsgBox "Capital restant = " & CapitalRestant
     
                If CapitalRestant < -2 And EnArriere = False Then
     
                    'Recup Date
                    DateDebutArriere = Cells(numero_ligne, 4)
                    'MsgBox DateDebutArriere
                    Cells(numero_ligne, 13).Value = DateDebutArriere
                    EnArriere = Truev
     
             Dim Found As Range
             Dim Indice As Long
     
             With Worksheets("main")
                 Set Found = .Range("F2:F4000").Find(Account, LookIn:=xlValues, LookAt:=xlWhole)
                 If Not Found Is Nothing Then
                     Indice = Found.Row
                     MsgBox Indice
                     .Cells(Indice, 1).Value2 = DateDebutArriere
                 End If
            End With
     
     
                End If
     
                'MsgBox "CIN = " & CIN & ", " & "Account = " & Account & ", " & "Period = " & Period & ", " & "Repayment Date = " & RepaymentDate
            Else
                MsgBox "Error 1-1"
            End If
        'Else
        '    MsgBox "Error 1"
        'End If
     
        Next
     
        'For i = 2 To 5
        'For i = 1 To nb_lignes
     
     
            'CIN = Cells(i, 1)
     
            'MsgBox "CIN = " & CIN
        'Next
     
     
    End Sub

  4. #4
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 903
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 903
    Points : 10 166
    Points
    10 166
    Billets dans le blog
    36
    Par défaut
    Bonjour,

    C'est un code pour faire quoi ?

    Apparemment, on dirait un tableau d'amortissement d'emprunt. Mais ensuite ?

    Ton arriéré c'est quoi ? Un paiement en retard ou quoi d'autres ?

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2012
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    Oui exactement, c'est pour calculer les risques du retard de paiement
    Dés que le capital restant est au dessous de 2euros on note la date , à partir de laquelle, le client a un retard de paiement.

Discussions similaires

  1. optimiser le code d'une fonction
    Par yanis97 dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 15/07/2005, 08h41
  2. Optimiser mon code ASP/HTML
    Par ahage4x4 dans le forum ASP
    Réponses: 7
    Dernier message: 30/05/2005, 10h29
  3. optimiser le code
    Par bibi2607 dans le forum ASP
    Réponses: 3
    Dernier message: 03/02/2005, 14h30
  4. syntaxe et optimisation de codes
    Par elitol dans le forum Langage SQL
    Réponses: 18
    Dernier message: 12/08/2004, 11h54
  5. optimisation du code et var globales
    Par tigrou2405 dans le forum ASP
    Réponses: 2
    Dernier message: 23/01/2004, 10h59

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