+ Reply to Thread
Results 1 to 4 of 4

Macro for consolidating files back into one workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    12-06-2012
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2019
    Posts
    80

    Macro for consolidating files back into one workbook

    Hi All,

    In the attached spreadsheet, I have a macro that splits up the data based on category and creates 9 separate files within the same folder.

    These files are then looked at and updated by other employees, and what I'm looking for is a macro that will then take the updated 9 files and consolidate them back into the original workbook on a new Sheet, in the same format that they are originally (but with updated numbers of course).

    Let me know if you can help, or if you need any more information.

    Thanks!general 2015 percentages.xlsm

  2. #2
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Macro for consolidating files back into one workbook

    Hi Drexl27,
    . Here is a basic solution. Probably not too efficient as it interacts a lot with the spreadsheet and there are some extra bits to make to a bit more understandable, etc..
    .
    . Try it out, let me know how you get on, and if necessary I / we can improve / change it.
    .
    . To make this code work i modified your code which saves the Group Files Thus:

    Sub SaveGroupFiles()
        Dim x As Long
        Dim stFolder As String, stFName As String
        stFolder = ActiveWorkbook.Path
        Range("Data").AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("lst_group"), unique:=True
        Application.DisplayAlerts = False
        For x = 1 To Range("lst_group").Cells(1, 2)
            Range("crit").Cells(2, 1).Value = Range("lst_group").Cells(x + 1, 1).Value
            stFName = Range("lst_group").Cells(x + 1, 1).Value & Format(Date, "mmddyyyy") & Format(Time, "hhmm")
            Cells(1, Columns.Count).End(xlToLeft).Offset(x, 1).Value = stFName
            Range("Data").AdvancedFilter Action:=xlFilterCopy, copytorange:=Range("dataout"), criteriarange:=Range("crit")
            Sheet3.Copy
            ActiveWorkbook.SaveAs Filename:=stFolder & "\" & stFName
            ActiveWorkbook.Close
        Next x
    End Sub
    ... The modification gives a Range filled with the last made Group file names Thus:

    Using Excel 2007
    Row\Col
    AL
    AM
    AN
    1
    Group
    2
    AP Comm Acct080620152336
    3
    HR080620152336
    4
    Prop Acct080620152336
    5
    Office Services080620152336
    6
    Corp Acct080620152336
    7
    IT080620152336
    8
    Payroll080620152336
    9
    CFO080620152336
    10
    AP080620152336
    gen.percents

    .. This is needed for my following code to work.

    Code:

    '
    Sub MakeUpdatedSheet() 'http://www.excelforum.com/excel-programming-vba-macros/1097815-macro-for-consolidating-files-back-into-one-workbook.html
    Dim wbHere As Workbook: Set wbHere = ThisWorkbook
    Dim ws1 As Worksheet, wsSummary As Worksheet 'First ws in This workbook, new Summary ws
    Set ws1 = wbHere.Worksheets.Item(1)
    wbHere.Worksheets.Add(After:=Worksheets(wbHere.Worksheets.Count)).Name = "Summary at " & Format(Date, "mmddyyyy")
    Set wsSummary = ActiveSheet
    ws1.Rows(1).Copy 'Copy headings from original first sheet
    wsSummary.Rows(1).PasteSpecial Paste:=xlPasteAllUsingSourceTheme 'Paste headings in new sheet
    
    Dim myArr() As Variant: Let myArr() = ws1.Columns(ws1.Cells(2, Columns.Count).End(xlToLeft).Column).SpecialCells(xlCellTypeConstants).Value 'The cells of the last made sheets column with something in are the range assigned in the allowed "VBA one liner" to assign the values in a range to an Array.
    
    Dim ws As Worksheet, wb As Workbook, Cnt As Long
        For Cnt = LBound(myArr(), 1) To UBound(myArr(), 1)
                    'Workbooks.Open Filename:="F:\ExcelForum\Sorting\" & myArr(Cnt, 1) & ".xlsx"
        Workbooks.Open Filename:=wbHere.Path & "\" & myArr(Cnt, 1) & ".xlsx"
        Set wb = ActiveWorkbook: Set ws = wb.Worksheets("Sheet3")
        ws.Rows("2:" & ws.Cells(Rows.Count, 1).End(xlUp).Row & "").Copy
        wsSummary.Cells(Rows.Count, 1).End(3)(2).PasteSpecial Paste:=xlPasteAllUsingSourceTheme 'The Range (cell) thhat is next free in the Summary sheet is effectively used as the top left of where the range in the Clipbord is copied
        wb.Save: wb.Close 'Save, close the current wb
        Next Cnt
    End Sub
    ...
    . I have run the code a few times. It seems to work
    Alan
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  3. #3
    Registered User
    Join Date
    12-06-2012
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2019
    Posts
    80

    Re: Macro for consolidating files back into one workbook

    This seems to be working really well! I'm going to tinker with it a big, although I'll probably have more time to do so on Monday. So if I find any bugs or issues that I think could be made better I'll report back here. Thanks!

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Macro for consolidating files back into one workbook

    Quote Originally Posted by Drexl27 View Post
    This seems to be working really well! I'm going to tinker with it a big, although I'll probably have more time to do so on Monday. So if I find any bugs or issues that I think could be made better I'll report back here. Thanks!
    OK, thanks for the Feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Consolidating many xls data files with different formats into one using macro
    By usagi1134 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-21-2015, 04:11 AM
  2. Replies: 1
    Last Post: 11-26-2014, 04:28 AM
  3. [SOLVED] Need help batch running an excel macro on .csv files and saving back to the same .csv
    By OutKlast in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-31-2013, 09:16 PM
  4. [SOLVED] Macro to Import Multiple TXT Files into workbook - User to select files/directory
    By saber007 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-15-2013, 08:43 PM
  5. Switch back to original workbook within macro
    By theletterh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-11-2013, 09:40 AM
  6. Consolidating a single named sheet from multiple files into one workbook
    By Eastboston in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2012, 02:29 PM
  7. Replies: 0
    Last Post: 05-08-2006, 02:30 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1