Results 1 to 4 of 4

Thread: Consolidate Excel Workbooks With Different Table Structures To A New Master File

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Question Consolidate Excel Workbooks With Different Table Structures To A New Master File

    Hello all,

    first of all I'm a beginner in VBA and I really could need some help with a VBA code.

    I want to consolidate 4 Excel Project Lists (Workbooks) to a Master Workbook. The Project Lists has a different structure and almost different content. The relevant information is always on Sheet1 but it has completely different ranges. The only constant is the Project Number, which should be used to sort the information. Every Project should be listed only once with all the existing information.

    At the moment I'm working with Windows XP, Excel 2010 and the following VBA code:


    Code:
    Sub MergeSelectedWorkbooks()
        Dim SummarySheet As Worksheet
        Dim FolderPath As String
        Dim SelectedFiles() As Variant
        Dim NRow As Long
        Dim FileName As String
        Dim NFile As Long
        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:\Documents and Settings\xyz\My Documents\Project lists"
        
        ' Set the current directory to the the folder path.
        ChDrive FolderPath
        ChDir FolderPath
        
        ' Open the file dialog box and filter on Excel files, allowing multiple files
        ' to be selected.
        SelectedFiles = Application.GetOpenFilename( _
            filefilter:="Excel Files (*.xls*), *.xls*", MultiSelect:=True)
        
        ' NRow keeps track of where to insert new rows in the destination workbook.
        NRow = 1
        
        ' Loop through the list of returned file names
        For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
            ' Set FileName to be the current workbook file name to open.
            FileName = SelectedFiles(NFile)
            
            ' Open the current workbook.
            Set WorkBk = Workbooks.Open(FileName)
            
            ' Set the source range to be A1 through AZ55000.
            ' Modify this range for your workbooks. It can span multiple rows.
            Set SourceRange = WorkBk.Worksheets(1).Range("A1:AZ55000")
            
            ' Set the destination range to start at column A and be the same size as the source range.
            Set DestRange = SummarySheet.Range("A" & 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
        Next NFile
        
        ' Call AutoFit on the destination sheet so that all data is readable.
        SummarySheet.Columns.AutoFit
    End Sub

    The Master Project List should has the headers in Row1 and the information listed below. The Macro should automatically places the correct information to the correct column. Some of the information are in 2 or more of the lists but they should be listed only once in the Master List.

    Project Number Project Description ...
    1234A.00000001 Wheels
    1234A.00000002 Boards
    .
    .
    .


    Unfortunately I have some problems with defining and setting the headers. So one big step would be setting the headers and copying the correct information to the right column.


    It would be better if there is a variable range instead of a defined. Like the Macro searches the last row and starts at this row and column. Is that possible?

    I hope I explained my conception comprehensible. I really appreciate any kind of help!

    Thank you in advance!
    Daniel

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Daniel, if your data is 'all over the place' if you like, how do you think one can have a logic to pick relevant information? Does the position of the ranges have some logic to it? Also, to make things clearer, it would be best if you can upload these files (without any confidential information) on a file share site, and post the link here. If they aren't too heavy, you can upload them here itself.
    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

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0
    Hi Excel Fox,

    please excuse my bad explanation. Unfortunately there is no logic of the position of the ranges as you can see in the attached Example file. The Example file contains a short (like really really short) overview of the structures of 3 of the 4 lists. I hope that already makes it a little bit clearer, because unfortunately I'm not allowed to upload the original files.

    Thanks for your help!

    Cheers Daniel
    Attached Files Attached Files

  4. #4
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    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. Merge Workbooks to Master Workbook
    By donb1337 in forum Excel Help
    Replies: 6
    Last Post: 09-26-2013, 09:16 PM
  2. Replies: 9
    Last Post: 08-23-2013, 04:25 PM
  3. Replies: 1
    Last Post: 06-07-2013, 10:32 AM
  4. 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
  •