XL 2016 VBA > Réactualisation d'un TCD à l'ouverture du fichier qui ne passe pas

mam721

XLDnaute Junior
Bonjour à tous,

J'ai un petit soucis de réactualisation de TCD et de graphique associés.

J'ai 2 onglets PowerQuery qui vont chercher les données. "PQuery"
Un onglet PowerQuery qui fusionne les 2 précédents "BaseGlobale"
3 onglets d'analyse qui donnent des TCD en fonction de critères pertinents à afficher.
1 onglet synthèse qui contient les graphiques.

Je souhaite réactualiser tous les tableaux à l'ouverture du fichier.
Pour cela, j'ai fait une macro dont voici le code :
VB:
Private Sub Workbook_Open()
    rep = MsgBox("MAJ en cours", vbOKOnly, "MAJ")
    
    If rep = vbOK Then
    
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("PQuery Cartes").Select
    ActiveWorkbook.RefreshAll
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("PQuery CKABLE").Select
    ActiveWorkbook.RefreshAll
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("BaseGlobale").Select
    ActiveWorkbook.RefreshAll
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Analyse Cartes").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("CARTES NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Demandes / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Livraisons/Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("Cartes NBR Livraisons / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES Répartition / Activité 2020").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES Répartition / Activité 2021").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES LIVRAISONS").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Analyse CKABLE").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("CKABLE NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Demandes / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKQBLE NBR Livraisons / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Livraisons / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Analyse CARTES + CKABLE").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("Global NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Livraisons / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR demandes").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Synthèse").Select
    ActiveWorkbook.RefreshAll
    Application.Wait (Now + TimeValue("00:00:02"))
    
      
    Sheets("Analyse Cartes").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("CARTES NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Demandes / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Livraisons/Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("Cartes NBR Livraisons / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES Répartition / Activité 2020").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES Répartition / Activité 2021").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES LIVRAISONS").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CARTES NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Analyse CKABLE").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("CKABLE NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Demandes / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKQBLE NBR Livraisons / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Livraisons / Semaine").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("CKABLE NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    Sheets("Analyse CARTES + CKABLE").Select
    ActiveWorkbook.RefreshAll
    ActiveSheet.PivotTables("Global NBR Demandes / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Demandes / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Livraisons / An").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR Livraisons / Mois").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:01"))
    ActiveSheet.PivotTables("GLOBAL NBR demandes").PivotCache.Refresh
    Application.Wait (Now + TimeValue("00:00:02"))
    
    End If
    
    rep2 = MsgBox("MAJ faite", vbOKOnly, "MAJ")
    
    If rep2 = vbOK Then
    
    End If
    
End Sub

J'ai sélectionné 2 fois les tableaux pour une actualisation mais malgré cela, le resultat final n'est pas bon et m'oblige a aller dans l'onglet "Synthèse" et cliquer sur actualiser pour obtenir le bon résultat.

Dans ma source de données je modifie l'année de la demande d'une de mes lignes, j'enregistre.
j'ouvre le fichier de charge et voici ce qu'il se passe :
* PowerQuery Cartes se met bien à jour
* PowerQuery CKABLE se met bien à jour
* BaseGlobal se met bien à jour
* Analyse Cartes ne se met pas à jour
* Analyse CKBALE ne se met pas à jour
* Analyse Cartes + CKABLE ne se met pas à jour
* Synthese ne se met pas à jour

J'ai verifié que mes TCD sont bien avec l'option du TCD > Données > actualiser les données lors de l'ouverture du fichier coché.

Mais je me retrouve quand même à devoir réactualiser le fichier une fois ouvert.


Pouvez vous me dire ce qui ne va pas soit dans mon fichier soit dans la macro.

Je vous remercie.
 

Pièces jointes

  • Graphique charge activite Carte + CKABLE 2021222.xlsm
    608.8 KB · Affichages: 17

Phil69970

XLDnaute Barbatruc
Bonjour mam721

Une piste :
Si la feuille est protégée la MAJ du TCD se fait peut être alors que la feuille est encore protégée et échoue.
Solution provisoire enlever la protection de la feuille et enregistrer sans la protection et l'ouvrir sans protection pour voir si la MAJ se fait bien.

@Phil69970
 

Phil69970

XLDnaute Barbatruc
Bonjour mam721, le forum

==>Une piste ici
Des TCD basés sur la même source partagent le même cache (ou cube de données) l'actualisation de l'un des TCD actualise tous les TCD basés sur cette source : il faut donc déprotéger tous les onglets contenant des TCD liés à cette source...

Une autre piste que j'ai trouvé ici :
VB:
Public Sub RefreshPivotTables()
Dim ws As Worksheet, pt As PivotTable
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name = "Synthèse" Then
            For Each pt In ws.PivotTables
                pt.RefreshTable
                'ou
                'pt.PivotCache.Refresh
            Next pt
        End If
    Next ws
End Sub

Et encore une autre piste ici :
VB:
Private Sub Worksheet_Activate()
    ActiveSheet.PivotTables(1).PivotCache.Refresh
End Sub

@Phil69970
 
Dernière édition: