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 :

Ma macro fonctionne plusieurs jours puis plante


Sujet :

Macros et VBA Excel

  1. #1
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2014
    Messages : 5
    Points : 2
    Points
    2
    Par défaut Ma macro fonctionne plusieurs jours puis plante
    Bonjour,

    J'ai rédigé la macro ci-dessous qui me permet de récupérer des informations sur une page web, de les traiter puis de les charger dans une base de données.

    Mon problème est que cette macro fonctionne plusieurs jours puis plante sans que je n'arrive à comprendre pourquoi ...

    Je me suis formé sur le tas à VBA et à la programmation en général, il est donc possible de trouver des inepties dans ma macro.

    Merci d'avance pour tous vos conseils !

    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
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
     
    Dim DerniereCelluleRemplie As Integer
    Dim i As Integer
    Dim nom_station As String
    Dim numero_station As String
     
    Dim rs As ADODB.Recordset
    Dim oConn As ADODB.Connection
     
     
    'On retire les caractère "'" par "\'"
    Function esc(txt As String)
     esc = Trim(Replace(txt, "'", "\'"))
    End Function
     
     
    'Connection à MYSQL
    Sub ConnectDB()
        Set oConn = New ADODB.Connection
        oConn.Open "DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
            "SERVER=localhost;" & _
            "DATABASE=xxx;" & _
            "USER=xxx;" & _
            "PASSWORD=xxx;" & _
            "Option=3"
        oConn.MaintainConnection = False
    End Sub
     
     
    Sub lancement_macro_synop()
     
    Sheets("cv").Calculate
     
    heure = Sheets("cv").Range("F4")
     
    Application.OnTime TimeValue(heure), "a01_insert_data_station_all"
     
    End Sub
     
     
    Sub interval_synop()
     
    interval = Sheets("cv").Range("F5")
     
    Application.OnTime Now + TimeValue(interval), "a01_insert_data_station_all"
     
    End Sub
     
     
    Sub a01_insert_data_station_all()
     
    'Désactivation de la mise à jour visuelle
    Application.ScreenUpdating = False
     
    DerniereCelluleRemplie = Sheets("variable").Range("A1").End(xlDown).Row
     
    For i = 2 To DerniereCelluleRemplie
     
    nom_station = Sheets("variable").Range("A" & i)
    numero_station = Sheets("variable").Range("B" & i)
     
    date_du_jour = Format(Now, "dd")
    mois_du_jour = Format(Now, "mm")
    heure_now = Format(Now, "hh")
    annee = Format(Now, "yyyy")
     
    Sheets("d_a_st_ex").Select
    ActiveSheet.Range("A1:W300").Select
    Selection.ClearContents
     
    'Télécharger les données de la station
     
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://www.ogimet.com/cgi-bin/gsynres?ind=" & numero_station & "&lang=en&decoded=yes&ndays=10&ano=" & annee & "&mes=" & mois_du_jour & "&day=" & date_du_jour & "&hora=" & heure_now & "" _
            , Destination:=Range("$A$1"))
            .Name = _
            "gsynres?ind=" & numero_station & "&lang=en&decoded=yes&ndays=10&ano=" & annee & "&mes=" & mois_du_jour & "&day==" & date_du_jour & "&hora=" & heure_now & ""
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "4"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
     
    If Sheets("d_a_st_ex").Range("AA1").Value <> Sheets("d_a_st_ex").Range("AB1").Value Then
     
    'Suppression de la connexion
    ActiveWorkbook.Connections("Connexion").Delete
     
    GoTo Line1
     
    Else
     
    'Suppression de la connexion
    ActiveWorkbook.Connections("Connexion").Delete
     
    'Effacer les lignes vides
    Sheets("d_a_st_ex").Select
    x = Range("A250").End(xlUp).Row
    Cells.Range("A1:A" & x). _
    SpecialCells(xlCellTypeBlanks).EntireRow.Delete
     
    'Renseigner les cellules de d_b par celle de d_a
    Sheets("d_b_st_ex").Range("B1:Z250").Value = Sheets("d_a_st_ex").Range("B1:Z250").Value
     
    End If
     
    Sheets("d_t_g_st_ex").Range("CG7:CW7").Value = Sheets("variable").Range(("G" & i & ":W" & i)).Value
     
    'Insérer les données traités dans la base de données
     
    Set rs = New ADODB.Recordset
     
    ConnectDB
     
    With d_t_g_st_ex
     
    For rowCursor_a = 2 To 39
     
    strSQL = "INSERT IGNORE INTO archive_station_" & nom_station & " (" & _
    "date_heure_loc_ms_archive_station_" & nom_station & ", " & _
    "date_heure_loc_txt_archive_station_" & nom_station & ",  " & _
    "vitesse_vent_archive_station_" & nom_station & ",  " & _
    "vitesse_rafale_archive_station_" & nom_station & ",  " & _
    "press_atmo_archive_station_" & nom_station & ",  " & _
    "temp_air_archive_station_" & nom_station & ",  " & _
    "precipitation_archive_station_" & nom_station & ",  " & _
    "duree_vent_faible_archive_station_" & nom_station & ",  " & _
    "duree_vent_offshore_archive_station_" & nom_station & ",  " & _
    "dir_vent_archive_station_" & nom_station & ")  " & _
            "VALUES ('" & esc(.Cells(rowCursor_a, 1)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 2)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 3)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 4)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 5)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 6)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 7)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 8)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 9)) & "', " & _
                    "'" & esc(.Cells(rowCursor_a, 10)) & "')"
     
    rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
     
    Next
     
    End With
     
    'Insertion données vent graphique station
     
    Set rs = New ADODB.Recordset
     
    ConnectDB
     
    With d_t_g_st_ex
     
    For rowCursor_b = 2 To 11
     
    strSQL = "INSERT IGNORE INTO dir_vent_graph_station_" & nom_station & " (" & _
        "date_heure_loc_ms_dir_vent_graph_station_" & nom_station & "," & _
        "date_heure_loc_txt_dir_vent_graph_station_" & nom_station & "," & _
        "NO_16_dir_vent_graph_station_" & nom_station & "," & _
        "NNO_15_dir_vent_graph_station_" & nom_station & "," & _
        "N_14_dir_vent_graph_station_" & nom_station & "," & _
        "NNE_13_dir_vent_graph_station_" & nom_station & "," & _
        "NE_12_dir_vent_graph_station_" & nom_station & "," & _
        "ENE_11_dir_vent_graph_station_" & nom_station & "," & _
        "E_10_dir_vent_graph_station_" & nom_station & "," & _
        "ESE_9_dir_vent_graph_station_" & nom_station & ","
        strSQL = strSQL & _
        "SE_8_dir_vent_graph_station_" & nom_station & "," & _
        "SSE_7_dir_vent_graph_station_" & nom_station & "," & _
        "S_6_dir_vent_graph_station_" & nom_station & "," & _
        "SSO_5_dir_vent_graph_station_" & nom_station & "," & _
        "SO_4_dir_vent_graph_station_" & nom_station & "," & _
        "OSO_3_dir_vent_graph_station_" & nom_station & "," & _
        "O_2_dir_vent_graph_station_" & nom_station & "," & _
        "ONO_1_dir_vent_graph_station_" & nom_station & ")  " & _
            "VALUES ('" & esc(.Cells(rowCursor_b, 13)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 14)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 15)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 16)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 17)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 18)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 19)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 20)) & "', "
                    strSQL = strSQL & _
                    "'" & esc(.Cells(rowCursor_b, 21)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 22)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 23)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 24)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 25)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 26)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 27)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 28)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 29)) & "', " & _
                    "'" & esc(.Cells(rowCursor_b, 30)) & "')"
     
    rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
     
    Next
     
    End With
     
    Line1:
     
    Next
     
    'Active la mise à jour visuelle
    Application.ScreenUpdating = True
     
    Application.Run "interval_synop"
     
    End Sub

  2. #2
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut

    Bonjour, bonjour !

    Citation Envoyé par Erwin33 Voir le message
    […] puis plante
    ?

    Quelque chose ne va donc pas !

    Lire les règles du forum peut aider à présenter une problématique …

    _________________________________________________________________________________________________
    Question bien formulée, solution assurée ! (Robert Durupt)
    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

  3. #3
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2014
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Excuse moi je suis sous Excel 2010.

  4. #4
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut


    Merci mais on est bien loin d'en être là ! On peut juste te répondre : quelque chose a changé …

    Règles du forum : point III-C, III-D (III-E c'est compris, bien !), IV-H (voir plus bas),
    IV-I (avec ton message d'erreur dont Mme Irma la cartomancienne a pris connaissance !) …

    J'ai répondu car c'est ta première intervention sur ce forum.
    Un autre avec des messages déjà au compteur sans une présentation digne de ce nom, je le zappe ! Et je ne serais pas le seul …
    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

  5. #5
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2014
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Je n'ai pas de message d'erreur car Excel se bloque et je dois le fermer.

    Pour le titre j'ai pas trouvé mieux ...

    J'ai longtemps cherché sur plusieurs forum afin de ne pas faire perdre de temps aux gentils développeurs mais j'ai rien trouvé. Il est possible que je sois passé à côté, c'est possible ... D'autant plus que je ne suis pas développeur de formation mais par loisir. Si tu veux m'indiquer une discussion qui peut m'aider j'en serai ravi !

    Je me suis présenté ... mais après mon premier message, c'est vrai ... mea culpa ...

  6. #6
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut




    Dans ce cas, sans la base de données ni le classeur Excel, tu es alors le seul pour trouver la source de l'erreur !

    Suivre le code en mode pas à pas (via la touche F8) tout en vérifiant la fenêtre des Variables locales
    Tu pourras donc ensuite indiquer le numéro de la ligne déclenchant l'erreur (par rapport au code du post #1)
    et le contenu de chaque variable y figurant …
    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

  7. #7
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2014
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Quand j'utilise le mode pas à pas tout va bien, ma macro déroule tranquille !

    Quand je lance la macro cette dernière fonctionne entre 2 et 5 jours voir plus sans problème sachant qu'elle se lance toutes les 15 minutes (temps de traitement environ 3 minutes) et d'un coup d'un seul Excel se fige, je ne peux plus rien faire à par forcer la fermeture du logiciel. Je relance le logiciel en suivant, j'ouvre mon fichier et je relance ma macro qui va fonctionner pendant plusieurs jours avant un nouveau plantage.

    Ma première question est de savoir si la rédaction de ma macro respecte les règles de l'art ? Est ce que certaines améliorations peuvent être apportées ?

    A savoir que cette macro tourne sur un serveur (Online) et que je lance 10 fois Excel de manière indépendante afin d'ouvrir 10 fichiers avec des macros similaires (les sources d'informations provenant de site web différents) ce qui me permet d'avoir plusieurs macros qui tournent en parallèle. Je n'ai pas de problème de mémoire vive et certaines de ces macros qui ont la même architecture fonctionnement depuis plusieurs mois sans avoir subit un seul plantage...

  8. #8
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut



    Pour pouvoir arrêter manuellement une procédure, c'est soit avec la touche Echap soit via les icônes dans le VBE …

    Mais comme il y a des boucles dans le code, il faut laisser la main de temps en temps au système via DoEvents
    à placer par exemple dans les lignes n°72, 99, 158, 214 et 220 …

    Pour aussi tracer la progression de la procédure, utiliser Debug.Print envoyant du texte dans la fenêtre Exécution
    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

  9. #9
    Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Août 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2014
    Messages : 5
    Points : 2
    Points
    2
    Par défaut
    Merci Marc-L pour ces informations.

    Concernant DoEvents, quelle est la manière de l'intégrer dans mon code ? En rajoutant simplement DoEvents aux emplacements que tu m'as indiqué ?

  10. #10
    Expert éminent sénior
    Avatar de Marc-L
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Avril 2013
    Messages
    9 468
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2013
    Messages : 9 468
    Points : 18 677
    Points
    18 677
    Par défaut





    Oui !


    C'est parce que la vitesse de la lumière est plus rapide que celle du son que tant de gens paressent brillants avant d'avoir l'air con ! (Thomas Boishardy)

Discussions similaires

  1. Réponses: 4
    Dernier message: 05/05/2015, 16h22
  2. Réponses: 2
    Dernier message: 23/06/2010, 13h35
  3. Réponses: 10
    Dernier message: 28/08/2008, 09h15
  4. Réponses: 8
    Dernier message: 09/01/2007, 16h30
  5. Réponses: 2
    Dernier message: 18/11/2005, 21h40

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