+ Reply to Thread
Results 1 to 6 of 6

Merge data from other Excell files by copying (only) data into Master matching to Headers.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-14-2018
    Location
    Saudi Arabia
    MS-Off Ver
    Office 2016, Office 2010
    Posts
    26

    Merge data from other Excell files by copying (only) data into Master matching to Headers.

    Hei to all

    I have the below code to merge the excel files. It works perfect.

    However I have small issue with this code.
    When I run, it copy both data and the header from other file, and replaces the headers in Master file. Thus I am loosing some header data in master file.

    Master file have all headers and I need to copy only the data from other files and paste in the master file matching to the headers.

    Other data files have limited headers with data. I need to pull only data from those files and paste to the column heads in the master file, which matches to the header.

    Please, any help is highly appreciable.

    Thank you very much.
    paulc

    Sub test()
        Dim fn As String, n As Long, x(), myRows As Long, i As Long
        Dim ws As Worksheet, AL As Object
        Application.ScreenUpdating = False
        fn = Dir(ThisWorkbook.Path & "\*.xls")
        Set AL = CreateObject("System.Collections.ArrayList")
        Do While fn <> ""
            If fn <> ThisWorkbook.Name Then
                With Workbooks.Open(ThisWorkbook.Path & "\" & fn)
                    For Each ws In .Worksheets
                        n = n + 1: ReDim Preserve x(1 To n)
                        x(n) = ws.Range("a4", ws.Cells.SpecialCells(11)).Value
                        For i = 1 To UBound(x(n), 2)
                            If (x(n)(1, i) <> "") * (Not AL.Contains(x(n)(1, i))) Then AL.Add x(n)(1, i)
                        Next
                        myRows = myRows + UBound(x(n), 1)
                    Next
                    .Close False
                End With
            End If
            fn = Dir
        Loop
        Application.ScreenUpdating = True
        If n = 0 Then Exit Sub
        AL.Sort: GetData x, AL, myRows
    End Sub
    
    Private Sub GetData(x, AL, myRows)
        Dim a, i As Long, ii As Long, iii As Long, n As Long, flg As Boolean
        ReDim a(1 To myRows, 1 To AL.Count): n = 1
        For i = 0 To AL.Count - 1
            a(n, i + 1) = AL(i)
        Next
        For i = 1 To UBound(x)
            For ii = 2 + IIf(flg, 1, 0) To UBound(x(i), 1)
                If x(i)(ii, 1) <> "" Then
                    n = n + 1
                    For iii = 1 To UBound(x(i), 2)
                        a(n, AL.IndexOf_3(x(i)(1, iii)) + 1) = x(i)(ii, iii)
                    Next
                End If
            Next
            flg = True
        Next
        With Sheets("Summary").[a4].Resize(n, AL.Count)
            .CurrentRegion.ClearContents
            .Value = a
        End With
    End Sub
    Attached Files Attached Files
    Last edited by paulc869; 01-03-2019 at 03:57 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Merge data from other Excell files by copying (only) data into Master matching to Head

    Here is my shot at it.

    On the control panel sheet you identify two directories:
    - Cell B1 is the "in Basket" - enter the path name of the folder containing the files to be processed.
    - Cell B2 is the "Out Basket" - enter the path name of the folder where you want the files to be placed after they are processed.

    Clear the data on the summary sheet rows 7 and below if you wish and click the button. Data will be appended unless it is cleared manually.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    11-14-2018
    Location
    Saudi Arabia
    MS-Off Ver
    Office 2016, Office 2010
    Posts
    26

    Re: Merge data from other Excell files by copying (only) data into Master matching to Head

    Than you very much for your help.

    However there is message box showing me the ID number which is not available. There is thousands of data to merge.
    So this message is annoying for long time.

    How can I skip this message.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,641

    Re: Merge data from other Excell files by copying (only) data into Master matching to Head

    1) Mark as "Solved" your original thread.
    Merging four or more Excel files into one, which has unique column heads

    2)
    Sub test()
        Dim fn As String, n As Long, x(), i As Long, ws As Worksheet
        Application.ScreenUpdating = False
        fn = Dir(ThisWorkbook.Path & "\*.xls")
        Do While fn <> ""
            If fn <> ThisWorkbook.Name Then
                With Workbooks.Open(ThisWorkbook.Path & "\" & fn)
                    For Each ws In .Worksheets
                        n = n + 1: ReDim Preserve x(1 To n)
                        x(n) = ws.Range("a4", ws.Cells.SpecialCells(11)).Value
                    Next
                    .Close False
                End With
            End If
            fn = Dir
        Loop
        Application.ScreenUpdating = True
        If n = 0 Then Exit Sub
        GetData x
    End Sub
    
    Private Sub GetData(x)
        Dim a, i As Long, ii As Long, iii As Long, iv As Long, n As Long, myMax As Long
        a = Range("a4", Cells.SpecialCells(11)).Resize(1000000).Value
        For ii = 1 To UBound(a, 2)
            myMax = Application.Max(myMax, n + 2): n = 2
            For i = 1 To UBound(x)
                For iii = 1 To UBound(x(i), 2)
                    If a(1, ii) = x(i)(1, iii) Then
                        For iv = 3 To UBound(x(i), 1)
                            If x(i)(iv, 1) <> "" Then
                                n = n + 1
                                a(n, ii) = x(i)(iv, iii)
                            End If
                        Next
                        Exit For
                    End If
                Next
            Next
        Next
        With Sheets("Summary").[a4].Resize(myMax, UBound(a, 2))
            .CurrentRegion.Offset(2).ClearContents
            .Value = a
        End With
    End Sub

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Merge data from other Excell files by copying (only) data into Master matching to Head

    Here is the same file modified (not tested because I no longer had the test data), Instead of popping up a dialog box on not found, it writes them to a log sheet called "Not Found."
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    11-14-2018
    Location
    Saudi Arabia
    MS-Off Ver
    Office 2016, Office 2010
    Posts
    26

    Re: Merge data from other Excell files by copying (only) data into Master matching to Head

    Thank you very much helping in this case. Great help.

+ 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. Copying/Pasting Data from All Files in Folder to Master Sheet According to Headers
    By sdwhiteh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-22-2015, 01:23 PM
  2. Move data based on their headers to mater
    By Tortus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-08-2015, 02:40 PM
  3. Replies: 4
    Last Post: 02-08-2014, 02:31 PM
  4. Pull data from CSV files to Excel Mater
    By dellboy232000 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2013, 09:52 PM
  5. Help with copying data between 2 files based on 2 unique matching conditions
    By cowboy713 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-22-2010, 04:51 PM
  6. Replies: 0
    Last Post: 04-21-2006, 03:40 PM
  7. [SOLVED] merge excell data
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2006, 05:20 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