+ Reply to Thread
Results 1 to 4 of 4

Consolidation Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    7

    Consolidation Macro

    Hi All,

    I have 7 different workbooks that all contain the same headings in Columns A:H (only one sheet per workbook). I need a macro to loop through all seven workbooks, copy the data from A1 to Column H of the final row in that sheet (it varies in length), and paste them into my workbook on the "Master Sheet" tab. However, the data must be pasted at the end of the data that is already contained in the document, so I don't copy over anything. Also, if a file is not found, I want it to skip over it.

    Can anyone help? I appreciate it.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidation Macro

    I have few codes on consol, so to help you, could you please attache a sample workbook with desired output?

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Consolidation Macro

    Are your 7 files in a folder?

    What do you mean by if the file is not found? Where will the file names be listed?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Consolidation Macro

    This is Jerry's code,not mine, but very flexible one.

    Summary: Merge files in a specific folder into one master sheet (stacked)
    ' Moves imported files into another folder
    Dim fName As String, fPath As String, fPathDone As String, LR As Long, NR As Long, wbData As Workbook, wsMaster As Worksheet
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.DisplayAlerts = False
    Set wsMaster = ThisWorkbook.Sheets("Master") 'sheet report is built into: You need to create a new sheet called "Master" before you 'run it 
    With wsMaster
    If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then
    .UsedRange.Offset(1).EntireRow.Clear
    NR = 2
    Else
    NR = .Range("a" & .Rows.Count).End(xlUp).Row + 1
    End If
     
    MsgBox "Please select a folder with files to consolidate"
    Do
    With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = "C:\Users\jerry\Documents\Excel\"   ' Change this in to where your excel files are stored
    .AllowMultiSelect = False
    .Show
    If .SelectedItems.Count > 0 Then
    fPath = .SelectedItems(1) & "\"
    Exit Do
    Else
    If MsgBox("No folder chose, do you wish to abort?", vbYesNo) = vbYes Then Exit Sub
    End If
    End With
    Loop
    fPathDone = fPath & "Imported\"
    On Error Resume Next
    MkDir fPathDone
    On Error GoTo 0
    fName = Dir(fPath & "*.xl*")
    'Import a sheet from found files
    Do While Len(fName) > 0
    If fName <> ThisWorkbook.Name Then 'don't reopen this file accidentally
    Set wbData = Workbooks.Open(fPath & fName) 'Open file
    'This is the section to customize, replace with your own action code as needed
    LR = Range("A" & Rows.Count).End(xlUp).Row 'Find last row
    Range("A1:A" & LR).EntireRow.Copy .Range("A" & NR)
    wbData.Close False 'close file
    NR = .Range("A" & .Rows.Count).End(xlUp).Row + 1 'Next row
    Name fPath & fName As fPathDone & fName 'move file to IMPORTED folder
    End If
    fName = Dir 'ready next filename
    Loop
    End With
    ErrorExit: 'Cleanup
    ActiveSheet.Columns.AutoFit
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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