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 :

Fermer fichier excel


Sujet :

Macros et VBA Excel

  1. #1
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mars 2018
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mars 2018
    Messages : 40
    Points : 24
    Points
    24
    Par défaut Fermer fichier excel
    Bonjour,

    Je suis autodidacte en VBA et je coince sur une macro.
    Elle à pour bute de compléter un tableau Excel
    Pour se faire elle ouvrir 3 fichiers Excel l'un à la suite de l'autre. J'ai ajouté un contrôle pour m'assurer que le répertoire existe avant d'ouvrir.
    J'ai volontairement laissé ".Visible = True" pour visualiser ce qu'il se passe.

    En l'état ma macro fonctionne sauf que lorsque je demande de fermer les 3 fichiers Excel quelque chose étrange se passe.
    Une fenêtre excel vide avec tous les boutons grisés reste à l'écran.

    J'ai déjà utilisé cette méthode mais c'est la première fois que je constate ce problème.

    Avez-vous une idée?

    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
    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
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    Sub BilanHeures()
     
    'Pour chaque intervenant (1 colonne par intervenant)
    'En fonction de l'année
    'On ouvre le fichier Pointage correspondant à l'intervenant et à l'année
    'Pour chaque semaine du tableau récpa d'heures on cherche le n°CI dans la bonne feuille et dans la bonne zone
    'Si on trouve le n° de CI, on inscrit dans le tableau récap le nb d'heures semaines
     
     
    Dim appxl As Excel.Application
    Dim Fichier As Window
    Dim feuille As Worksheet
    Set appxl = CreateObject("Excel.application")
    Dim FichierAouvrir As String
     
    Dim NoCI As String
    Dim Intervenant As String
    Dim Initiale As String
     
    Dim Ligne As Integer
    Dim Année As Integer
    Dim Année0 As Integer
     
    Dim i As Integer
    Dim j As Integer
    Dim NoColonne As Integer
     
     
    Dim DernLigne As Long
     
    Dim FichierXL As String
    Dim CheminFichierXL As String
    Dim NoSem As String
    Dim Zone As Integer
    Dim NomFeuille As String
     
     
    NoColonne = ActiveCell.Column
     
     
    NoCI = ThisWorkbook.Sheets("Heures").Range("A1")
    DernLigne = Worksheets("Heures").Range("C" & Rows.Count).End(xlUp).Row
     
     
    Initiale = Worksheets("Heures").Cells(2, NoColonne)   'Ligne 2 et Colonne...
     
     
    For j = 2 To 50
     
    If Initiale = ThisWorkbook.Sheets("Données").Range("D" & j) Then     'Recherche nom auteur par rapport aux initiales
     
    Intervenant = ThisWorkbook.Sheets("Données").Range("C" & j)
     
    Exit For
     
    End If
     
    Next j
     
    MsgBox "Récupérations des heures de: " & Intervenant
     
    Intervenant = Mid(Intervenant, InStr(Intervenant, " ") + 1)     'On ne garde que le nom de famille
     
     
     
    Sheets("Heures").Range(Cells(4, NoColonne), Cells(162, NoColonne)).ClearContents 'Effacer la colonne selectionné des lignes 4 à 162
     
     
     
     
    'Pour chaque semaines
    For Ligne = 4 To DernLigne
     
    'Gestion des années
    Année = ThisWorkbook.Sheets("Heures").Range("A4")
     
     
    If Ligne > 57 Then
    Année = Année + 1
    End If
     
    If Ligne > 110 Then
    Année = Année + 1
    End If
     
    'On Error GoTo ErrorHandler
     
     
        CheminFichierXL = "R:\COMMUN\01-ORIGINAUX LBM\Bureau\POINTAGE\"
     
        FichierXL = Intervenant & "_Pointage "
     
     
     
    If Dir(CheminFichierXL & Année, vbDirectory) <> vbNullString Then 'Pour tester si dossier existe
     
     
     
     
    ' Exit Sub
     
     
    'ErrorHandler:
     
       ' MsgBox "Il n'y a pas de fichier  ", vbInformation, "Pas de fichier"
     
     
     
    'Cancel = True
     
     
     
    'Application.EnableEvents = True 'Résactiver les évènements pendant la macro
     
     
    FichierAouvrir = FichierXL & Année & ".xls"
     
     
    With appxl
        .Workbooks.Open CheminFichierXL & Année & "\" & FichierAouvrir
       ' .Visible = False
        .Visible = True
    End With
     
     
     
    NoSem = Right(ThisWorkbook.Sheets("Heures").Range("C" & Ligne), 2)
     
     
    If NoSem = "01" Then
    NomFeuille = "01-04"
    Zone = 1
    End If
     
    If NoSem = "02" Then
    NomFeuille = "01-04"
    Zone = 2
    End If
     
    If NoSem = "03" Then
    NomFeuille = "01-04"
    Zone = 3
    End If
     
    If NoSem = "04" Then
    NomFeuille = "01-04"
    Zone = 4
    End If
     
    If NoSem = "05" Then
    NomFeuille = "05-08"
    Zone = 1
    End If
     
    If NoSem = "06" Then
    NomFeuille = "05-08"
    Zone = 2
    End If
     
    If NoSem = "07" Then
    NomFeuille = "05-08"
    Zone = 3
    End If
     
    If NoSem = "08" Then
    NomFeuille = "05-08"
    Zone = 4
    End If
     
    If NoSem = "09" Then
    NomFeuille = "09-12"
    Zone = 1
    End If
     
    If NoSem = "10" Then
    NomFeuille = "09-12"
    Zone = 2
    End If
     
    If NoSem = "11" Then
    NomFeuille = "09-12"
    Zone = 3
    End If
     
    If NoSem = "12" Then
    NomFeuille = "09-12"
    Zone = 4
    End If
     
    If NoSem = "13" Then
    NomFeuille = "13-16"
    Zone = 1
    End If
     
    If NoSem = "14" Then
    NomFeuille = "13-16"
    Zone = 2
    End If
     
    If NoSem = "15" Then
    NomFeuille = "13-16"
    Zone = 3
    End If
     
    If NoSem = "16" Then
    NomFeuille = "13-16"
    Zone = 4
    End If
     
    If NoSem = "17" Then
    NomFeuille = "17-20"
    Zone = 1
    End If
     
    If NoSem = "18" Then
    NomFeuille = "17-20"
    Zone = 2
    End If
     
    If NoSem = "19" Then
    NomFeuille = "17-20"
    Zone = 3
    End If
     
    If NoSem = "20" Then
    NomFeuille = "17-20"
    Zone = 4
    End If
     
    If NoSem = "21" Then
    NomFeuille = "21-24"
    Zone = 1
    End If
     
    If NoSem = "22" Then
    NomFeuille = "21-24"
    Zone = 2
    End If
     
    If NoSem = "23" Then
    NomFeuille = "21-24"
    Zone = 3
    End If
     
    If NoSem = "24" Then
    NomFeuille = "21-24"
    Zone = 4
    End If
     
    If NoSem = "25" Then
    NomFeuille = "25-28"
    Zone = 1
    End If
     
    If NoSem = "26" Then
    NomFeuille = "25-28"
    Zone = 2
    End If
     
    If NoSem = "27" Then
    NomFeuille = "25-28"
    Zone = 3
    End If
     
    If NoSem = "28" Then
    NomFeuille = "25-28"
    Zone = 4
    End If
     
    If NoSem = "29" Then
    NomFeuille = "29-32"
    Zone = 1
    End If
     
    If NoSem = "30" Then
    NomFeuille = "29-32"
    Zone = 2
    End If
     
    If NoSem = "31" Then
    NomFeuille = "29-32"
    Zone = 3
    End If
     
    If NoSem = "32" Then
    NomFeuille = "29-32"
    Zone = 4
    End If
     
    If NoSem = "33" Then
    NomFeuille = "33-36"
    Zone = 1
    End If
     
    If NoSem = "34" Then
    NomFeuille = "33-36"
    Zone = 2
    End If
     
    If NoSem = "35" Then
    NomFeuille = "33-36"
    Zone = 3
    End If
     
    If NoSem = "36" Then
    NomFeuille = "33-36"
    Zone = 4
    End If
     
    If NoSem = "37" Then
    NomFeuille = "37-40"
    Zone = 1
    End If
     
    If NoSem = "38" Then
    NomFeuille = "37-40"
    Zone = 2
    End If
     
    If NoSem = "39" Then
    NomFeuille = "37-40"
    Zone = 3
    End If
     
    If NoSem = "40" Then
    NomFeuille = "37-40"
    Zone = 4
    End If
     
    If NoSem = "41" Then
    NomFeuille = "41-44"
    Zone = 1
    End If
     
    If NoSem = "42" Then
    NomFeuille = "41-44"
    Zone = 2
    End If
     
    If NoSem = "43" Then
    NomFeuille = "41-44"
    Zone = 3
    End If
     
    If NoSem = "44" Then
    NomFeuille = "41-44"
    Zone = 4
    End If
     
    If NoSem = "45" Then
    NomFeuille = "45-48"
    Zone = 1
    End If
     
     
    If NoSem = "46" Then
    NomFeuille = "45-48"
    Zone = 2
    End If
     
    If NoSem = "47" Then
    NomFeuille = "45-48"
    Zone = 3
    End If
     
    If NoSem = "48" Then
    NomFeuille = "45-48"
    Zone = 4
    End If
     
    If NoSem = "49" Then
    NomFeuille = "49-52"
    Zone = 1
    End If
     
    If NoSem = "50" Then
    NomFeuille = "49-52"
    Zone = 2
    End If
     
    If NoSem = "51" Then
    NomFeuille = "49-52"
    Zone = 3
    End If
     
    If NoSem = "52" Then
    NomFeuille = "49-52"
    Zone = 4
    End If
     
                Set Fichier = appxl.Windows(FichierAouvrir)
                Fichier.Activate
                Set feuille = appxl.Sheets(NomFeuille)
     
     
    '---------------------------------Zone 1 ---------------
     If Zone = 1 Then
     
     For i = 7 To 17
     
        If feuille.Range("C" & i) = NoCI Then
        'Range(Colonne & Ligne) = feuille.Range("P" & i) 'NoColonne
        Cells(Ligne, NoColonne) = feuille.Range("P" & i)
        End If
     
     Next
     
     End If
    '---------------------------------Zone 2 ---------------
      If Zone = 2 Then
     
     For i = 7 To 17
     
        If feuille.Range("S" & i) = NoCI Then
        Cells(Ligne, NoColonne) = feuille.Range("AF" & i)
        End If
     
     Next
     
     End If
     
     
     '---------------------------------Zone 3 ---------------
     If Zone = 3 Then
     
     For i = 26 To 36
     
        If feuille.Range("C" & i) = NoCI Then
        Cells(Ligne, NoColonne) = feuille.Range("P" & i)
        End If
     
     Next
     
     End If
     
      '---------------------------------Zone 4 ---------------
     If Zone = 4 Then
     
     For i = 26 To 36
     
        If feuille.Range("S" & i) = NoCI Then
        Cells(Ligne, NoColonne) = feuille.Range("AF" & i)
        End If
     
     Next
     
     End If
     
     
     
    End If
     
     
     
    Next 'Ligne
     
     
     
        '-------------------Fermer le pointage
     
     
    Année = ThisWorkbook.Sheets("Heures").Range("A4")
     
    With appxl
     
        If Dir(CheminFichierXL & Année, vbDirectory) <> vbNullString Then 'Pour tester si dossier existe
     
           .Workbooks(FichierXL & Année & ".xls").Close SaveChanges:=False
     
        End If
     
     
        If Dir(CheminFichierXL & Année + 1, vbDirectory) <> vbNullString Then 'Pour tester si dossier existe
     
           .Workbooks(FichierXL & Année + 1 & ".xls").Close SaveChanges:=False
     
        End If
     
     
        If Dir(CheminFichierXL & Année + 2, vbDirectory) <> vbNullString Then 'Pour tester si dossier existe
     
            .Workbooks(FichierXL & Année + 2 & ".xls").Close SaveChanges:=False
     
        End If
     
     
    End With
     
     
     
     
     
    End Sub

  2. #2
    Membre éclairé Avatar de Nain porte koi
    Homme Profil pro
    peu importe
    Inscrit en
    Novembre 2023
    Messages
    513
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : peu importe

    Informations forums :
    Inscription : Novembre 2023
    Messages : 513
    Points : 824
    Points
    824
    Par défaut
    Hello,

    A priori, vous avez créé une instance Excel qui n'est pas refermée
    Set appxl = CreateObject("Excel.application")
    il manquerait
    appxl.Application.Quit
    voire
    Set appxl = Nothing
    Edit : pourquoi chercher si le fichier existe avant de le fermer vu que vous n'enregistrez pas les modifs ?
    JièL
    Membre des AMIS
    Anti Macro Inutilement Superfétatoire

  3. #3
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mars 2018
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mars 2018
    Messages : 40
    Points : 24
    Points
    24
    Par défaut
    appxl.Application.Quit

    C'est exactement ce qu'il fallait.


    Merci beaucoup!

    Excellente fin de journée

  4. #4
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mars 2018
    Messages
    40
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mars 2018
    Messages : 40
    Points : 24
    Points
    24
    Par défaut
    Citation Envoyé par Nain porte koi Voir le message
    Hello,

    A priori, vous avez créé une instance Excel qui n'est pas refermée

    il manquerait voire

    Edit : pourquoi chercher si le fichier existe avant de le fermer vu que vous n'enregistrez pas les modifs ?
    Tout compte fait, je m'aperçois qu'on moment d'éteindre l'ordinateur, Excel n'est pas fermé (dans le gestionnaire des tâches, Excel apparait dans "Processus en arrière-plan")

    Le fait d'ajouter la ligne "appxl.Application.Quit" permet de fermer la fenêtre excel mais en réalité Excel n'est pas vraiment fermé.


    Et comme dit plus haut c'est la première fois que j'ai ce problème.

    Jusque là, je n'ai jamais eu recours à la ligne "appxl.Application.Quit" et pourtant le fichier Excel se fermait très bien sans rester en arrière plan.

    Je suis preneur d'une autre idée, merci

  5. #5
    Rédacteur

    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Août 2013
    Messages
    977
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Administrateur de base de données
    Secteur : Finance

    Informations forums :
    Inscription : Août 2013
    Messages : 977
    Points : 4 102
    Points
    4 102
    Par défaut
    Bonjour,
    Extrait de la FAQ pour terminer un processus:

    Call KillProcess("Excel.Exe")

    Citation Envoyé par VBA
    '----------------------------------------------------------------------------------------
    Public Function KillProcess(ByVal ProcessName As String) As Boolean
    '----------------------------------------------------------------------------------------
    ' Supprime le processus d'une application.
    ' https://vb.developpez.com/faq/?page=Systeme#killprocess
    '----------------------------------------------------------------------------------------
    On Error GoTo Gest_Err
    Dim SVC As Object
    Dim sQuery As String
    Dim Oproc

    Set SVC = CreateObject("winmgmts:root\cimv2")
    sQuery = "select * from win32_process where name='" & ProcessName & "'"
    For Each Oproc In SVC.execquery(sQuery)
    Oproc.Terminate
    KillProcess = True
    Next

    Gest_Err:
    Set SVC = Nothing
    Err.Clear
    End Function
    '----------------------------------------------------------------------------------------
    Bonne continuation

Discussions similaires

  1. Fermer fichier Excel dans une seconde application
    Par Riton69 dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 24/07/2024, 10h39
  2. [AC-2007] Fermer fichier Excel spécifique
    Par Renard-fou dans le forum VBA Access
    Réponses: 2
    Dernier message: 26/11/2009, 09h21
  3. instruction: fermer fichier excel sans sauvegarder
    Par nynho dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 31/01/2008, 14h59
  4. Fermer fichier excel externe
    Par pascalouh dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 27/06/2007, 16h19
  5. fermer fichier excel via OLE
    Par richard038 dans le forum Delphi
    Réponses: 2
    Dernier message: 27/09/2006, 17h26

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