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 :

Regrouper les colones de plusieurs fichiers dans une meme feuille


Sujet :

Macros et VBA Excel

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Analyse système
    Inscrit en
    Août 2016
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Analyse système
    Secteur : Finance

    Informations forums :
    Inscription : Août 2016
    Messages : 1
    Points : 0
    Points
    0
    Par défaut Regrouper les colones de plusieurs fichiers dans une meme feuille
    Bonjour,

    Je suis debutant en VBA et j'aimerais coder une macro pour un projet qui necessite une action repetitive.
    Situation: Dans un document, j'ai plusieur fichier excel de performance de societes par annees. (Fichier 1= 2000 a 2004, F2= 2004-8, F3= 2009-12, F4=2013-15)
    Objectif: Regrouper tous ces fichiers en un seul en copiant toutes ces colones a la suite.

    Voila ce que j'ai pour le moment reussi a faire, mais mon module affiche constament des erreurs

    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
    Sub MergeAllWorkbooks()
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim NRow As Long
        Dim Lc As Long
        Dim FileName As String
        Dim WorkBk As Workbook
        Dim SourceRange As Range
        Dim DestRange As Range
        Dim Lastcol As Range
     
     
        ' Create a new workbook and set a variable to the first sheet.
        Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
     
        ' Modify this folder path to point to the files you want to use.
        FolderPath = "C:\Users\abcdefghij"
     
        ' NRow keeps track of where to insert new rows in the destination workbook.
        NRow = 1
     
        ' Call Dir the first time, pointing it to all Excel files in the folder path.
        FileName = Dir(FolderPath & "*.xl*")
     
        ' Loop until Dir returns an empty string.
        Do While FileName <> ""
            ' Open a workbook in the folder
            Set WorkBk = Workbooks.Open(FolderPath & FileName)
     
            ' Set the source range to be A9 through C9.
            ' Modify this range for your workbooks.
            ' It can span multiple rows.
            Set SourceRange = WorkBk.Worksheets(1).Range("B2:K61")
     
            ' Set the destination range to start at column B and
            ' be the same size as the source range.
            Lc = Lastcol(DestRange)
            Set DestRange = SummarySheet.Range("B" & NRow)
            With SourceRange
            DestRange = DestRange.Cells(1, Lc + 1) _
                        .Resize(.Rows.Count, .Columns.Count)
     
     
            ' Copy over the values from the source to the destination.
            DestRange.Value = SourceRange.Value
     
            ' Increase NRow so that we know where to copy data next.
            NRow = NRow + DestRange.Rows.Count
     
            ' Close the source workbook without saving changes.
            WorkBk.Close savechanges:=False
     
            ' Use Dir to get the next file name.
            FileName = Dir()
     
        Loop
     
        ' Call AutoFit on the destination sheet so that all
        ' data is readable.
        SummarySheet.Columns.AutoFit
    End Sub
    Est ce aue auelau'un pourrait m'aider a corriger ce morceau de code stp?

    Merci d'avance

  2. #2
    Membre émérite Avatar de Thautheme
    Homme Profil pro
    salarié
    Inscrit en
    Août 2014
    Messages
    1 373
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : salarié

    Informations forums :
    Inscription : Août 2014
    Messages : 1 373
    Points : 2 594
    Points
    2 594
    Par défaut
    Bonjour, bonjour le forum,

    Il maque un Set et un End With à ton code ou, il manque un Set et il y a un With SourceRange en trop !... Et il me semble qu'il y ait confusion entre DestRange et SourceRange
    dans la deuxième hypothèse le code serait :

    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
    Sub MergeAllWorkbooks()
    Dim SummarySheet As Worksheet
    Dim FolderPath As String
    Dim NRow As Long
    Dim Lc As Long
    Dim FileName As String
    Dim WorkBk As Workbook
    Dim SourceRange As Range
    Dim DestRange As Range
    Dim Lastcol As Range
     
     
    ' Create a new workbook and set a variable to the first sheet.
    Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    ' Modify this folder path to point to the files you want to use.
    FolderPath = "C:\Users\abcdefghij"
    ' NRow keeps track of where to insert new rows in the destination workbook.
    NRow = 1
    ' Call Dir the first time, pointing it to all Excel files in the folder path.
    FileName = Dir(FolderPath & "*.xl*")
    ' Loop until Dir returns an empty string.
    Do While FileName <> ""
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)
        ' Set the source range to be A9 through C9.
        ' Modify this range for your workbooks.
        ' It can span multiple rows.
        Set SourceRange = WorkBk.Worksheets(1).Range("B2:K61")
        ' Set the destination range to start at column B and
        ' be the same size as the source range.
        Lc = Lastcol(DestRange)
        Set DestRange = SummarySheet.Range("B" & NRow)
        Set DestRange = DestRange.Cells(1, Lc + 1).Resize(.Rows.Count, .Columns.Count)
        ' Copy over the values from the source to the destination.
        DestRange.Value = SourceRange.Value
        ' Increase NRow so that we know where to copy data next.
        NRow = NRow + DestRange.Rows.Count
        ' Close the source workbook without saving changes.
        WorkBk.Close savechanges:=False
        ' Use Dir to get the next file name.
        FileName = Dir()
    Loop
    ' Call AutoFit on the destination sheet so that all
    ' data is readable.
    SummarySheet.Columns.AutoFit
    End Sub
    À plus,

    Thauthème

    Je suis Charlie

  3. #3
    Rédacteur
    Avatar de Philippe Tulliez
    Homme Profil pro
    Formateur, développeur et consultant Excel, Access, Word et VBA
    Inscrit en
    Janvier 2010
    Messages
    12 773
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Formateur, développeur et consultant Excel, Access, Word et VBA

    Informations forums :
    Inscription : Janvier 2010
    Messages : 12 773
    Points : 28 637
    Points
    28 637
    Billets dans le blog
    53
    Philippe Tulliez
    Ce que l'on conçoit bien s'énonce clairement, et les mots pour le dire arrivent aisément. (Nicolas Boileau)
    Lorsque vous avez la réponse à votre question, n'oubliez pas de cliquer sur et si celle-ci est pertinente pensez à voter
    Mes tutoriels : Utilisation de l'assistant « Insertion de fonction », Les filtres avancés ou élaborés dans Excel
    Mon dernier billet : Utilisation de la fonction Dir en VBA pour vérifier l'existence d'un fichier

Discussions similaires

  1. [XL-2007] Copier plusieurs feuilles de plusieurs fichier dans une seule feuille
    Par QcSylvanio dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 03/10/2012, 22h02
  2. [SQL2005] Regrouper les données de plusieurs bases dans une seule
    Par oliviera63 dans le forum Développement
    Réponses: 7
    Dernier message: 07/02/2012, 10h40
  3. Copier les données de plusieurs colonnes dans une nouvelle feuille
    Par lolonico1974 dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 07/07/2010, 07h51
  4. Intégrer les données de plusieurs fichiers dans une table
    Par soad029 dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 25/11/2007, 03h57
  5. Réponses: 9
    Dernier message: 22/06/2007, 08h36

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