Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question consolidation of excel files for monthly reporting

    Hi All
    I am trying to write a macro that will collate (consolidate) all the excel files in a directory, in a master file...all must have the same layout. I need my project managers to be able to input and save their own documents, and once done, that I can just run a macro, to get all inputs in one sheet.
    From this master file, I would then create my charts / graphs to present to the company, but for right now, I have the following, which is not exactly doing what I need:
    ---------------
    Code:
    Sub MergeAllWorkbooks()
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim NRow As Long
        Dim FileName As String
        Dim WorkBk As Workbook
        Dim SourceRange As Range
        Dim DestRange 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\mpetrus\Desktop\board files\"
        
        ' 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 cell in column A to be the file name.
            SummarySheet.Range("A" & NRow).Value = 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("A4:J6")
            
            ' Set the destination range to start at column B and
            ' be the same size as the source range.
            Set DestRange = SummarySheet.Range("B" & NRow)
            Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
               SourceRange.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
    -----------
    This creates a blank file where everything is copied to from the range specified, but I need it more specifically to populate the master file, and be able to add to the master file next time I run the macro, not overwrite the existing master file content. I would like to be able to keep the history.
    Is this even possible?

    Hope someone can assist.

    Yav
    Last edited by RetiredGeek; 2016-02-05 at 10:04. Reason: Added Code Tags

  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,164
    Thanks
    47
    Thanked 977 Times in 907 Posts
    Do you want the data to be at the end of the existing, or on a new sheet?
    If at the end you can use this code to find the end of the data.
    LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

    cheers, Paul

  3. #3
    New Lounger
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Paul.
    Where would I copy this line into the code?

    How would I specify the master file, so that I can start gathering history?

  4. #4
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,164
    Thanks
    47
    Thanked 977 Times in 907 Posts
    Set the master workbook instead of creating a new one.
    MasterFile = "C:\Users\mpetrus\Desktop\masterfile.xls"
    Set SummarySheet = Workbooks.Open(MasterFile)


    To get the last row of data from the master it would be something like this.
    NRow = SummarySheet.Cells(Rows.Count, 1).End(xlUp).Row
    This sets the starting point.

    As you seem to copy the data into column B you will need to use a "2" in the "Cells" selection.
    NRow = SummarySheet.Cells(Rows.Count, 2).End(xlUp).Row

    Don't forget to use a copy of the master sheet to test.

    cheers, Paul
    Last edited by Paul T; 2016-02-03 at 08:24.

  5. #5
    New Lounger
    Join Date
    Feb 2016
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Paul
    Will let you know wen i get it right.

  6. #6
    WS Lounge VIP
    Join Date
    Mar 2002
    Location
    Newcazzle, UK
    Posts
    2,819
    Thanks
    133
    Thanked 480 Times in 457 Posts
    Hi Yav

    ..if you could supply say, two samples of the datafiles you want to merge, then it would be much easier for us to help you with your automated processing tool. You could put 'dummy' data to replace any sensitive data etc etc.

    zeddy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •