Results 1 to 7 of 7

Thread: Merge Workbooks to Master Workbook

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Don, took a while, but here's the code you need

    Code:
    Option Explicit
    
    Sub Consolidate()
    
        Const clngWorkbooksToConsolidate As Long = 4
        Dim var(1 To clngWorkbooksToConsolidate) As Variant
        Dim varColIndex(1 To clngWorkbooksToConsolidate) As Variant
        Dim varColHeader As Variant
        Dim lngLoop As Long, lngRow As Long, lngCol As Long, lngIndex As Long
        Dim objDic As Object
        Dim strFilesFolder As String
        Dim strFile As String
        
        With Application.FileDialog(msoFileDialogFilePicker)
            .AllowMultiSelect = True
            .Filters.Clear
            .Filters.Add "Excel Files", "*.xlsx", 1
            .Show
            Application.ScreenUpdating = False
            If .SelectedItems.Count = clngWorkbooksToConsolidate Then
                For lngLoop = 1 To clngWorkbooksToConsolidate
                    With Workbooks.Open(.SelectedItems(lngLoop), , False)
                        var(lngLoop) = .Sheets(1).Cells(1).CurrentRegion.Value2
                        .Close 0
                    End With
                Next lngLoop
            Else
                MsgBox clngWorkbooksToConsolidate & " workbooks were not selected. Program will now exit", vbOKOnly + vbInformation, ""
                GoTo EndSub
            End If
        End With
        Set objDic = CreateObject("Scripting.Dictionary")
        varColHeader = Array("Project Number", "Project Description 1", "Project Description 2", "Project Description 3", "Project Description 4", "Priority Status", "Process approval status", "Project Manager", "Planning responsible", "Customer", "Profit Center")
        For lngLoop = LBound(varColHeader, 1) To UBound(varColHeader, 1)
            objDic.Item(varColHeader(lngLoop)) = 0
        Next lngLoop
        For lngLoop = 1 To clngWorkbooksToConsolidate
            For lngCol = LBound(var(lngLoop), 2) To UBound(var(lngLoop), 2)
                objDic.Item(var(lngLoop)(1, lngCol)) = 0
            Next lngCol
        Next lngLoop
        varColHeader = objDic.keys
        objDic.RemoveAll
        For lngLoop = 1 To clngWorkbooksToConsolidate
            For lngRow = 1 + LBound(var(lngLoop)) To UBound(var(lngLoop))
                objDic.Item(var(lngLoop)(lngRow, 1)) = 0
            Next lngRow
        Next lngLoop
        With ThisWorkbook.Sheets("Master Workbook")
            .UsedRange.ClearContents
            .Cells(1).Resize(, 1 + UBound(varColHeader)).Value = varColHeader
            .Cells(2, 1).Resize(objDic.Count).Value = Application.Transpose(objDic.keys)
            For lngIndex = 1 To clngWorkbooksToConsolidate
                For lngCol = 1 + LBound(var(lngIndex), 2) To UBound(var(lngIndex), 2)
                    For lngLoop = 2 To 1 + UBound(varColHeader)
                        If var(lngIndex)(1, lngCol) = .Cells(1, lngLoop).Value Then
                            varColIndex(lngIndex) = varColIndex(lngIndex) & lngLoop & "|"
                        End If
                    Next lngLoop
                Next lngCol
            Next lngIndex
            For lngLoop = 1 To clngWorkbooksToConsolidate
                For lngIndex = 2 To objDic.Count
                    For lngRow = 2 To UBound(var(lngLoop))
                        If .Cells(lngIndex, 1).Value = var(lngLoop)(lngRow, 1) Then
                            For lngCol = LBound(Split(varColIndex(lngLoop), "|")) To UBound(Split(varColIndex(lngLoop), "|")) - 1
                                .Cells(lngRow, CLng(Split(varColIndex(lngLoop), "|")(lngCol))).Value = var(lngLoop)(lngRow, 2 + lngCol)
                            Next lngCol
                        End If
                    Next lngRow
                Next lngIndex
            Next lngLoop
            .UsedRange.Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlYes
        End With
        Erase var
        Erase varColHeader
        Erase varColIndex
    EndSub:
        Application.ScreenUpdating = True
        Set objDic = Nothing
        
    End Sub
    I've also attached the workbook with the code. Just press CTRL+SHIFT+R (I've assigned that shortcut to the macro). It will then ask for the 4 workbooks to be selected. Select all 4, and click OK.

    Let me know if you face any problem
    Attached Files Attached Files
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Replies: 3
    Last Post: 09-26-2013, 08:32 PM
  2. Replies: 4
    Last Post: 06-18-2013, 01:38 PM
  3. Replies: 1
    Last Post: 06-07-2013, 10:32 AM
  4. Replies: 2
    Last Post: 05-28-2013, 05:32 PM
  5. Consolidate multiple workbooks from a folder into one master file VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 02-26-2013, 09:00 PM

Posting Permissions

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