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 :

Private Sub Workbook_BeforeClose - comment exécuter la procédure plusieurs fois ?


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Août 2012
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2012
    Messages : 187
    Par défaut Private Sub Workbook_BeforeClose - comment exécuter la procédure plusieurs fois ?
    Bonjour au forum,

    J'ai une macro Private Sub Workbook_BeforeClose(Cancel As Boolean) qui exécute une série de vérifications dans la feuille avant fermeture.
    Si certaines choses sont incorrectes, l'utilisateur annule la proposition d'enregistrement du fichier, corrige ce qui doit l'être et ensuite referme la feuille.

    Idéalement, j'aimerais que la macro réexécute la vérification avant de fermer le fichier mais ce n'est pas le cas.
    Quelle instruction pourrait faire en sorte que cette macro s'exécute avant chaque fermeture ?

    Bonne journée !

    Henri

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Formateur et développeur chez EXCELLEZ.net
    Inscrit en
    Novembre 2003
    Messages
    19 125
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : Belgique

    Informations professionnelles :
    Activité : Formateur et développeur chez EXCELLEZ.net
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2003
    Messages : 19 125
    Billets dans le blog
    131
    Par défaut
    Salut

    Dans la mesure ou BeforeClose s'exécute à chaque fermeture*, il faudrait que tu donnes le code utilisé.


    *
    Place le code suivant puis essaie de fermer ton ficher...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Cancel = True
    End Sub
    "Plus les hommes seront éclairés, plus ils seront libres" (Voltaire)
    ---------------
    Mes billets de blog sur DVP
    Mes remarques et critiques sont purement techniques. Ne les prenez jamais pour des attaques personnelles...
    Pensez à utiliser les tableaux structurés. Ils vous simplifieront la vie, tant en Excel qu'en VBA ==> mon tuto
    Le VBA ne palliera jamais une mauvaise conception de classeur ou un manque de connaissances des outils natifs d'Excel...
    Ce ne sont pas des bonnes pratiques parce que ce sont les miennes, ce sont les miennes parce que ce sont des bonnes pratiques
    VBA pour Excel? Pensez D'ABORD en EXCEL avant de penser en VBA...
    ---------------

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Août 2012
    Messages
    187
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Août 2012
    Messages : 187
    Par défaut
    Bonjour Pierre,

    Voici code complet:
    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
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
     
    Function TriTableau(Valeur)
        Dim Debut As Integer, Fin As Integer
        Dim i As Integer, j As Integer
        Dim temp
        Debut = LBound(Valeur)
        Fin = UBound(Valeur)
        For i = Debut To Fin
            For j = i To Fin
                If Valeur(i) > Valeur(j) Then
                    temp = Valeur(j)
                    Valeur(j) = Valeur(i)
                    Valeur(i) = temp
                End If
            Next j
        Next i
        TriTableau = Valeur
    End Function
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
     
    ' Cette macro sert à vérifier que toutes les questions ont leur réponse afin que l'utilisateur soit certain de n'avoir pas passé une seule question.
     
        Application.ScreenUpdating = False
     
    '    Dim OkPourSauvegarde As Boolean
        Dim Msg As String
        Dim Manquants As String
    '    Dim Empl As Integer
     
    '    OkPourSauvegarde = False
        MsgFr = "Lignes ou cellules : "
        MsgNl = "Lijnen of cellen:    "
        '        123456789012345678901
        If Range("B12") = "FR" Then Msg = MsgFr Else Msg = MsgNl
     
        Worksheets("Checklist").Select
        ActiveSheet.Unprotect ("30082013")
     
        Application.EnableEvents = False
     
        If Range("D4") = "" Then Msg = Msg & "04|"
        If Range("G4") = "" Then Msg = Msg & "04|"
        If Range("J4") = "" Then Msg = Msg & "04|"
        If Range("F6") = "" Then Msg = Msg & "06|"
        If Range("F8") = "" Then Msg = Msg & "08|"
        If Range("F10") = "" Then Msg = Msg & "10|"
        If Range("F12") = "" Then Msg = Msg & "12|"
     
    '===================================================
    ' Début de la vérification de l'encadré Nieuweklant
    '===================================================
        If (Blad1.NieuweJa.Value = False And Blad1.NieuweNeen.Value = False) Then
            Msg = Msg & Blad1.Shapes("NieuweNeen").TopLeftCell.Row & "|" ' On modifie le msg si l'utilisateur n'a pas répondu à la question d'en-tête
        ElseIf Blad1.NieuweJa.Value = True Then ' et on vérifie si on a toutes les réponses lorsqu'il a répondu "oui" à la question d'en-tête. S'il a répondu "non", on ne fait rien.
            If Blad1.VakbondJa.Value = False And Blad1.VakbondNeen.Value = False Then Msg = Msg & Blad1.Shapes("VakbondNeen").TopLeftCell.Row & "|"
            If Blad1.VasteFeeNA.Value = False And Blad1.VasteFeeWB.Value = False And Blad1.VasteFeeGV.Value = False Then Msg = Msg & Blad1.Shapes("VasteFeeGV").TopLeftCell.Row & "|"
            If Blad1.ADVB.Value = False And Blad1.ADVNA.Value = False And Blad1.ADVO.Value = False Then Msg = Msg & Blad1.Shapes("ADVO").TopLeftCell.Row & "|"
            If Blad1.StatuutA.Value = False And Blad1.StatuutB.Value = False And Blad1.StatuutSA.Value = False And Blad1.StatuutSB.Value = False Then Msg = Msg & Blad1.Shapes("StatuutA").TopLeftCell.Row & "|"
            If Blad1.InlezenPrestatiesJa.Value = False And Blad1.InlezenPrestatiesNeen.Value = False Then Msg = Msg & Blad1.Shapes("InlezenPrestatiesNeen").TopLeftCell.Row & "|"
            If Blad1.UurroosterJa.Value = False And Blad1.UurroosterNeen.Value = False Then Msg = Msg & Blad1.Shapes("UurroosterNeen").TopLeftCell.Row & "|"
            If Blad1.KostenplaatsJa.Value = False And Blad1.KostenplaatsNeen.Value = False Then Msg = Msg & Blad1.Shapes("KostenPlaatsNeen").TopLeftCell.Row & "|"
            If Blad1.DagContractJa.Value = False And Blad1.DagContractNeen.Value = False Then Msg = Msg & Blad1.Shapes("DagContractenNeen").TopLeftCell.Row & "|"
            If Blad1.PloegenPremiesJa.Value = False And Blad1.PloegenPremiesNeen.Value = False Then Msg = Msg & Blad1.Shapes("PloegenPremiesNeen").TopLeftCell.Row & "|"
            If Blad1.PremiesJa.Value = False And Blad1.PremiesNeen.Value = False Then Msg = Msg & Blad1.Shapes("PremiesNeen").TopLeftCell.Row & "|"
            If Blad1.MaaltijdchequesJa.Value = False And Blad1.MaaltijdchequesNeen.Value = False And Blad1.MaaltijdchequesNA.Value = False And Range("L" & Blad1.Shapes("MaaltijdchequesNeen").TopLeftCell.Row) = "" Then Msg = Msg & Blad1.Shapes("MaaltijdchequesNeen").TopLeftCell.Row & "|"
            If Blad1.KoopKrachtPremieB.Value = False And Blad1.KoopKrachtPremieNA.Value = False And Blad1.KoopKrachtPremieGV.Value = False Then Msg = Msg & Blad1.Shapes("KoopKrachtPremieGV").TopLeftCell.Row & "|"
            If Blad1.ContactPersoonFactuurInOrde.Value = False Then Msg = Msg & Blad1.Shapes("ContactPersoonFactuurInOrde").TopLeftCell.Row & "|"
            If Blad1.ContactPersoonSelfServInOrde.Value = False Then Msg = Msg & Blad1.Shapes("ContactPersoonSelfServInOrde").TopLeftCell.Row & "|"
            If Blad1.NieuweASRVakPlanInOrde.Value = False Then Msg = Msg & Blad1.Shapes("NieuweASRVakplanInOrde").TopLeftCell.Row & "|"
            If Blad1.ContactPersoonSelfContrInOrde.Value = False And Blad1.ContactPersoonSelfContrNA.Value = False Then Msg = Msg & Blad1.Shapes("ContactPersoonSelfContrInOrde").TopLeftCell.Row & "|"
            If Blad1.dagcontractenJa.Value = False And Blad1.dagcontractenNeen.Value = False Then Msg = Msg & Blad1.Shapes("dagcontractenNeen").TopLeftCell.Row & "|"
            If Blad1.DocOpDagContracten.Value = False Then Msg = Msg & Blad1.Shapes("DocOpDagContracten").TopLeftCell.Row & "|"
     
    ' Vérification des données des cellules contenant des valeurs
            LigneCrt = ActiveSheet.Shapes("NieuweRAZ").TopLeftCell.Row
            If Range("J" & LigneCrt) = "" Then Msg = Msg & LigneCrt & "|"
            LigneCrt = ActiveSheet.Shapes("UrenVoltijdsRAZ").TopLeftCell.Row
            If Range("J" & LigneCrt) = "" Then Msg = Msg & LigneCrt & "|"
            LigneCrt = ActiveSheet.Shapes("UrenCAORAZ").TopLeftCell.Row
            If Range("J" & LigneCrt) = "" Then Msg = Msg & LigneCrt & "|"
            LigneCrt = ActiveSheet.Shapes("PCRAZ").TopLeftCell.Row
            If Range("I" & LigneCrt) = "" Then Msg = Msg & LigneCrt & "|"
        End If
     
    '=================================================
    ' Fin de la vérification de l'encadré Nieuweklant
    '=================================================
     
    '===================================================
    ' Début de la vérification de l'encadré Vreemdeling
    '===================================================
     
        LigneCrt = ActiveSheet.Shapes("VreemdelingRAZ").TopLeftCell.Row
        If Range("H" & LigneCrt) = "" Then
            Msg = Msg & LigneCrt & "|"
     
        ElseIf UCase(Range("H" & LigneCrt)) <> "BE" Then
     
            If Blad1.VreemIDGelinktinEPInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemIDGelinktinEPInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemIDKaartOpEUInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemIDKaartOpEUInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemIDVervaldatumInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemIDVervaldatumInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemIDNummerInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemIDNummerInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemArbeidskaartVrijgInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemArbeidskaartVrijgInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemArbeidsVerguningInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemArbeidsVerguningInOrde").TopLeftCell.Row & "|"
            If Blad1.VreemIDVervaldatum2InOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemIDVervaldatum2InOrde").TopLeftCell.Row & "|"
            If Range("F" & LigneCrt) = "Hors Europe" Or Range("F" & LigneCrt) = "Buiten Europa" Then
                If Blad1.VreemFeedbackLoondinOrde.Value = False Then Msg = Msg & Blad1.Shapes("VreemFeedbackLoondinOrde").TopLeftCell.Row & "|"
            End If
        End If
     
    '=================================================
    ' Fin de la vérification de l'encadré Vreemdeling
    '=================================================
     
    '===============================================
    ' Début de la vérification de l'encadré Student
    '===============================================
     
        If (Blad1.StudentJa.Value = False And Blad1.StudentNeen.Value = False) Then
            Msg = Msg & Blad1.Shapes("StudentJa").TopLeftCell.Row & "|"
        ElseIf Blad1.StudentJa.Value = True Then
            If Blad1.Minder18.Value = False And Blad1.Meer18.Value = False Then Msg = Msg & Blad1.Shapes("Meer18").TopLeftCell.Row & "|"
            If Blad1.Contingent.Value = False Then Msg = Msg & Blad1.Shapes("Contingent").TopLeftCell.Row & "|"
        End If
     
    '=============================================
    ' Fin de la vérification de l'encadré Student
    '=============================================
     
    '=============================================
    ' Début de la vérification de l'encadré Flexi
    '=============================================
     
        If (Blad1.FlexiJa.Value = False And Blad1.FlexiNeen.Value = False) Then
            Msg = Msg & Blad1.Shapes("FlexiJa").TopLeftCell.Row & "|"
        ElseIf Blad1.FlexiJa.Value = True Then
            If Blad1.IntentieVerkVerstInOrde.Value = False Then Msg = Msg & Blad1.Shapes("IntentieVerkVerstInOrde").TopLeftCell.Row & "|"
            If Blad1.FlexiRaamOverVerstInOrde.Value = False Then Msg = Msg & Blad1.Shapes("FlexiRaamOverVerstInOrde").TopLeftCell.Row & "|"
        End If
     
    '===========================================
    ' Fin de la vérification de l'encadré Flexi
    '===========================================
     
    '==========================================================
    ' Début de la vérification de l'encadré WPF van toepassing
    '==========================================================
     
    ' MODULE INCORRECT EN L'ETAT. DES CONDITIONS DOIVENT ETRE MISES AUX VERIFICATIONS CAR TOUTES LES QUESTIONS NE SONT PAS TOUJOURS POSEES
     
        If Blad1.WPFVanToepassingJa.Value = False And Blad1.WPFVanToepassingNeen.Value = False Then
            Msg = Msg & Blad1.Shapes("WPFVanToepassingJa").TopLeftCell.Row & "|"
        Else
            If Blad1.WPFVanToepassingNeen.Value = True Then
                If Blad1.WPFMailKlantGeenRisicoJa.Value = False And Blad1.WPFMailKlantGeenRisicoToDo.Value = False Then Msg = Msg & Blad1.Shapes("WPFMailKlantGeenRisicoJa").TopLeftCell.Row & "|"
            End If
            If Blad1.WPFAanwezigJa.Value = False And Blad1.WPFAanwezigNeen.Value = False Then Msg = Msg & Blad1.Shapes("WPFAanwezigJa").TopLeftCell.Row & "|"
            If Blad1.WPFVeiligheidFunctieJa.Value = False And Blad1.WPFVeiligheidFunctieNeen.Value = False Then Msg = Msg & Blad1.Shapes("WPFVeiligheidFunctieJa").TopLeftCell.Row & "|"
            If Blad1.WPFAanwezigNeen.Value = True And Blad1.WPFVeiligheidFunctieNeen.Value = True Then
                If Blad1.WPFMailHrbmDmInOrde.Value = False Then Msg = Msg & Blad1.Shapes("WPFMailHrbmDmInOrde").TopLeftCell.Row & "|"
            End If
            If Blad1.WPFAanwezigJa.Value = True And Blad1.WPFVeiligheidFunctieJa.Value = True Then
                If Blad1.WPFMedischGekeurdJa.Value = False And Blad1.WPFMedischGekeurdNeen.Value = False Then Msg = Msg & Blad1.Shapes("WPFMedischGekeurdJa").TopLeftCell.Row & "|"
            End If
            If (Blad1.WPFAanwezigNeen.Value = True And Blad1.WPFVeiligheidFunctieJa.Value = True) Or Blad1.WPFMedischGekeurdNeen.Value = True Then
                If Blad1.WPFMailKlGeenVeiligJa.Value = False And Blad1.WPFMailKlGeenVeiligNeen.Value = False Then Msg = Msg & Blad1.Shapes("WPFMailKlGeenVeiligJa").TopLeftCell.Row & "|"
            End If
        End If
     
    '========================================================
    ' Fin de la vérification de l'encadré WPF van toepassing
    '========================================================
     
    '===========================================
    ' Début de la vérification de l'encadré VCU
    '===========================================
     
        If Blad1.VCUVanToepassingJa.Value = False And Blad1.VCUVanToepassingNeen.Value = False Then
            Msg = Msg & Blad1.Shapes("VCUVanToepassingJa").TopLeftCell.Row & "|"
        ElseIf Blad1.VCUVanToepassingJa.Value = True Then
            If Blad1.VCUUzkVcuGecertifJa.Value = False And Blad1.VCUUzkVcuGecertifNeen.Value = False Then Msg = Msg & Blad1.Shapes("VCUUzkVcuGecertifJa").TopLeftCell.Row & "|"
            If Blad1.VCUUzkVcuGecertifJa.Value = True Then
                If Range("K" & Blad1.Shapes("VCUUzkVcuGecertifRAZ").TopLeftCell.Row) = "" Then Msg = Msg & Blad1.Shapes("VCUUzkVcuGecertifRAZ").TopLeftCell.Row & "|"
            End If
            If Blad1.VCUTijdstipVCAVO.Value = False And Blad1.VCUTijdstipVCAB3.Value = False Then Msg = Msg & Blad1.Shapes("VCUTijdstipVCAVO").TopLeftCell.Row & "|"
            If Blad1.VCUUzkVcuGecertifJa.Value = True And Blad1.VCUTijdstipVCAVO.Value = True Then
                If Blad1.VCUMeldingKantoorInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VCUMeldingKantoorInOrde").TopLeftCell.Row & "|"
                If Blad1.VCUEPKantoorBeheerVCUInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VCUEPKantoorBeheerVCUInOrde").TopLeftCell.Row & "|"
            End If
            If Blad1.VCUTijdstipVCAB3.Value = True Then
                If Blad1.VCUEPKantoorBeheerVCUInOrde.Value = False Then Msg = Msg & Blad1.Shapes("VCUEPKantoorBeheerVCUInOrde").TopLeftCell.Row & "|"
            End If
     
        End If
     
    '=========================================
    ' Fin de la vérification de l'encadré VCU
    '=========================================
     
    '================================================
    ' Début de la vérification de l'encadré Contract
    '================================================
     
        If (Blad1.ContractJa.Value = False And Blad1.ContractNeen.Value = False) Then
            Msg = Msg & Blad1.Shapes("ContractJa").TopLeftCell.Row & "|"
        ElseIf Blad1.ContractJa.Value = True Then
            If Blad1.FunctiekwalificatieJa.Value = False And Blad1.FunctiekwalificatieNeen.Value = False Then Msg = Msg & Blad1.Shapes("FunctiekwalificatieJa").TopLeftCell.Row & "|"
     
            If Blad1.Datum.Value = False Then Msg = Msg & Blad1.Shapes("Datum").TopLeftCell.Row & "|"
            If Blad1.ContractStatuut.Value = False Then Msg = Msg & Blad1.Shapes("ContractStatuut").TopLeftCell.Row & "|"
            If Blad1.Reden.Value = False Then Msg = Msg & Blad1.Shapes("Reden").TopLeftCell.Row & "|"
            If Blad1.KantoorBeheer.Value = False Then Msg = Msg & Blad1.Shapes("KantoorBeheer").TopLeftCell.Row & "|"
            If Blad1.KantoorUitvoering.Value = False Then Msg = Msg & Blad1.Shapes("KantoorUitvoering").TopLeftCell.Row & "|"
            If Blad1.uurloon.Value = False Then Msg = Msg & Blad1.Shapes("uurloon").TopLeftCell.Row & "|"
            If Blad1.Coefficient.Value = False Then Msg = Msg & Blad1.Shapes("Coefficient").TopLeftCell.Row & "|"
            If Blad1.TypeVerplaatsing.Value = False Then Msg = Msg & Blad1.Shapes("TypeVerplaatsing").TopLeftCell.Row & "|"
            If Blad1.ReiskostenPerDag.Value = False Then Msg = Msg & Blad1.Shapes("ReiskostenPerDag").TopLeftCell.Row & "|"
            If Blad1.LoonKoopkrachtpremieJa.Value = False And Blad1.LoonKoopkrachtpremieNeen.Value = False And Blad1.LoonKoopkrachtpremieNA.Value = False Then Msg = Msg & Blad1.Shapes("LoonKoopkrachtpremieNA").TopLeftCell.Row & "|"
            If Blad1.ContractVakAttestGevrJa.Value = False And Blad1.ContractVakAttestGevrNeen.Value = False And Blad1.ContractVakAttestGevrNA.Value = False Then Msg = Msg & Blad1.Shapes("ContractVakAttestGevrNA").TopLeftCell.Row & "|"
            If Blad1.FeeUurJa.Value = False And Blad1.FeeUurNeen.Value = False And Blad1.FeeUurNA.Value = False Then Msg = Msg & Blad1.Shapes("FeeUurNA").TopLeftCell.Row & "|"
            If Blad1.VolcontinuJa.Value = False And Blad1.VolcontinuNeen.Value = False And Blad1.VolcontinuNA.Value = False Then Msg = Msg & Blad1.Shapes("VolcontinuJa").TopLeftCell.Row & "|"
            If Blad1.VolcontinuJa.Value = True Then
                If Blad1.BVVermindering.Value = False Then Msg = Msg & Blad1.Shapes("BVVermindering").TopLeftCell.Row & "|"
            End If
            If Blad1.LoonMaaltijdchequesJa.Value = False And Blad1.LoonMaaltijdchequesNeen.Value = False And Range("L" & Blad1.Shapes("LoonMaaltijdchequesRAZ").TopLeftCell.Row) = "" Then Msg = Msg & Blad1.Shapes("LoonMaaltijdchequesJa").TopLeftCell.Row & "|"
            If Blad1.TewerkstellingKostenplaatsJa.Value = False And Blad1.TewerkstellingKostenplaatsNeen.Value = False Then Msg = Msg & Blad1.Shapes("TewerkstellingKostenplaatsJa").TopLeftCell.Row & "|"
            If Blad1.PlaatsTewerkstelling.Value = False Then Msg = Msg & Blad1.Shapes("PlaatsTewerkstelling").TopLeftCell.Row & "|"
            If Blad1.AfdrukTijdsregeling.Value = False Then Msg = Msg & Blad1.Shapes("AfdrukTijdsregeling").TopLeftCell.Row & "|"
            If Blad1.ContractUurrooster.Value = False Then Msg = Msg & Blad1.Shapes("ContractUurrooster").TopLeftCell.Row & "|"
            If Blad1.ContractSjabloonNA.Value = False And Blad1.ContractSjabloonInOrde.Value = False Then Msg = Msg & Blad1.Shapes("ContractSjabloonNA").TopLeftCell.Row & "|"
            If Blad1.ASRJa.Value = False And Blad1.ASRNeen.Value = False Then Msg = Msg & Blad1.Shapes("ASRJa").TopLeftCell.Row & "|"
        End If
     
    '==============================================
    ' Fin de la vérification de l'encadré Contract
    '==============================================
     
        ActiveWindow.ScrollRow = 14
        Application.ScreenUpdating = True
     
        If Len(Msg) > 21 Then
            Manquants = Mid(Msg, 22, Len(Msg))
            L = 4
            C = 18
            Empl = Split(Manquants, "|") ' Empl : emplacements
            OrdAlpha = TriTableau(Empl) ' Tri des lignes par ordre numérique
     
            NbCol = Range(Cells(1, 1), Cells(4, 16384).End(xlToLeft)).Columns.Count
            If NbCol < C Then NbCol = C
            Range(Cells(L, C), Cells(12, NbCol)).ClearContents
            Range("R2").ClearContents
     
            If Range("B12") = "FR" Then Range("R2").Value = "Réponses manquantes" Else Range("R2").Value = "Ontbrekende antwoorden"
            Range("R2").Font.Bold = True
            For n = 1 To UBound(OrdAlpha)
                Range(Cells(L, C), Cells(L, C)).Value = OrdAlpha(n)
                If L = 12 Then
                    L = 4
                    C = C + 1
                ElseIf Blad1.Rows(L + 1 & ":" & L + 1).RowHeight = 4.2 Then
                    L = L + 2
                Else
                    L = L + 1
                End If
            Next
     
            Col = Split(Cells(L, C).Address, "$")
            Range("R4:" & Col(1) & "12").Select
            With Selection ' centrage des valeurs entrées dans les cellules
                .HorizontalAlignment = xlCenter
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
        End If
     
        Call Verrouillage ' Verrouillage des cellules et des feuilles
     
        Range("F6").Select
     
    End Sub
    Est-ce que "Cancel = True" ne va pas simplement empêcher le fichier d'être sauvegardé ?

    Hors, je ne veux pas empêcher la sauvegarde, simplement exécuter la macro au moment de chaque fermeture.

    Merci pour l'examen du code, ton avis est bienvenu.

    Henri

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Formateur et développeur chez EXCELLEZ.net
    Inscrit en
    Novembre 2003
    Messages
    19 125
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : Belgique

    Informations professionnelles :
    Activité : Formateur et développeur chez EXCELLEZ.net
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2003
    Messages : 19 125
    Billets dans le blog
    131
    Par défaut
    Salut.

    Le code que tu as produit est impossible à tester, vu sa longueur (ce n'est pas une critique personnelle )

    Cancel = True utilisé dans une procédure événementielle qui reçoit Cancel en argument sert à annuler l'événement. Donc, si tu utilises Cancel = True dans un Private Sub Workbook_BeforeClose(Cancel As Boolean), tu vas annuler l'événement de fermeture. La sauvegarde éventuelle du fichier n'intervient pas ici.

    Normalement, une procédure événementielle ne fait rien, à part appeler une ou des procédures qui font quelque chose. De plus, une procédure de +/- 230 lignes, ça ne tient pas la route et c'est impossible à déboguer.

    Normalement, une procédure aussi longue est coupée en petits morceaux digests. Dans ton cas, une fonction, par exemple booléenne, pour chaque encadré à tester. Normalement, cette fonction ne doit envoyer aucun message à l'utilisateur, ce n'est pas son rôle, mais simplement une réponse qui sera traitée par la procédure qui aura appelé la fonction. On peut aussi envisager une fonction qui renvoie du texte (Ok, Encadré X non valide, ...) et concaténer les réponses, éventuellement avec un retour chariot, pour produire UN message de fin d'exécution après tous les tests. Tu peux ainsi tester chaque fonction de test de manière individuelle, et gérer le résultat final de tous tes tests. Ici, on laisse filer tous les tests et on stoppe éventuellement après, mais tu pourrais aussi stopper au premier test négatif. L'important, à mon avis, est de ne pas produire une succession de messages au fur et à mesure des résultats des tests. En procédant ainsi, tu pourrais aussi créer une table des logs dans le fichier ou un fichier txt à côté, que tu alimentes au fur et à mesure des tests réalisés.

    Voici un exemple pour illustrer mes propos, qui utilise des fonctions de test qui renvoie du texte (je ne suis pas fan, mais on va faire avec pour l'instant). Chaque fonction de test (dans ton cas, qui teste un encadré particulier) renvoie une chaine vide (l'encadrée est valide) ou un message (l'encadré n'est pas valide). La fonction générale de test appelle les fonctions particulières l'une après l'autre, concatène les résultats et renvoie une chaine à la macro événementielle (chaine vide => tout est ok et on peut fermer, chaine non vide => problème donc on ne ferme pas).
    Code vba : 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
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Dim Msg As String
     
      Msg = OkPourFermeture()
      If Msg <> "" Then
        MsgBox Msg
        Cancel = True
      End If
    End Sub
     
    Function OkPourFermeture() As String
      OkPourFermeture = TestA1()
      OkPourFermeture = OkPourFermeture & IIf(OkPourFermeture <> "", vbCr, "") & TestA2
    End Function
     
    Function TestA1() As String
      If Range("a1").Value <> "Bonjour" Then TestA1 = "Cellule A1 non valide"
    End Function
     
    Function TestA2() As String
      If Range("a2").Value <> "Au revoir" Then TestA2 = "Cellule A2 non valide"
    End Function
    "Plus les hommes seront éclairés, plus ils seront libres" (Voltaire)
    ---------------
    Mes billets de blog sur DVP
    Mes remarques et critiques sont purement techniques. Ne les prenez jamais pour des attaques personnelles...
    Pensez à utiliser les tableaux structurés. Ils vous simplifieront la vie, tant en Excel qu'en VBA ==> mon tuto
    Le VBA ne palliera jamais une mauvaise conception de classeur ou un manque de connaissances des outils natifs d'Excel...
    Ce ne sont pas des bonnes pratiques parce que ce sont les miennes, ce sont les miennes parce que ce sont des bonnes pratiques
    VBA pour Excel? Pensez D'ABORD en EXCEL avant de penser en VBA...
    ---------------

Discussions similaires

  1. Réponses: 3
    Dernier message: 30/03/2015, 11h48
  2. comment exécuter une procédure stocké en vb.net
    Par ratsmok dans le forum VB.NET
    Réponses: 3
    Dernier message: 06/06/2009, 19h40
  3. Réponses: 14
    Dernier message: 14/01/2009, 15h59
  4. Réponses: 2
    Dernier message: 20/03/2007, 17h00
  5. Réponses: 1
    Dernier message: 26/07/2006, 11h23

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