Forum Discussion

adambc's avatar
adambc
Frequent Visitor
1 year ago
Solved

Automate creating new queries ...

Forgive me if I don't go into the detail of why I need to do the following, just trust me that there is a good reason!   I have 78 .xlsx files stored in a single folder OneDrive environment ... - ...
  • slorin's avatar
    1 year ago

    Bonjour adambc 

    Je réponds en français, c'est plus simple pour moi

    C'est possible avec une requête Power Query chargée dans le modèle de données (Power Pivot) et une petite macro VBA pour modifier chaque table en DAX

    La requête Power Query doit regrouper vos 78 fichiers et la macro va filtrer sur le nom de chaque fichier

    A adapter bien évidemment

     

    code de la macro

    Sub Duplique()
    For Each Nom In Range("Tableau1[Column1]")
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Name = Nom

    Set Data = ActiveSheet.ListObjects.Add(SourceType:=4, Source:=ActiveWorkbook.Connections("Requête - Tableau1"), Destination:=Range("$A$1"))
    Data.TableObject.WorkbookConnection.OLEDBConnection.CommandText = Array("EVALUATE FILTER(Tableau1, [Column1]=""" & Nom & """)")
    Data.TableObject.WorkbookConnection.OLEDBConnection.CommandType = xlCmdDAX
    Data.Refresh
    Next Nom
    End Sub

    Voir la vidéo en pièce jointe

    Stéphane