+ Reply to Thread
Results 1 to 9 of 9

collect data from multiple files and sheets

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Arrow collect data from multiple files and sheets

    Hello
    I would like to request that I have multiple excel files and I want to copy all data from every sheet of each file into one Excel File (on one sheet)
    each file contains the different number of sheets and also blank cells in data.

    I want to append them to only on Main sheet of Master Data workbook. VBA code is the preference for this task.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    05-15-2017
    Location
    US
    MS-Off Ver
    365
    Posts
    901

    Re: collect data from multiple files and sheets

    The first thing you would want to do is create a loop to go thru each file.
    Then a loop to go thru each sheet in each file.
    Based on the provided files, can you update your Master data file with the expected results?

    I believe it pretty straight forward in the sense that you are wanting to copy each sheet from east, west, north and south and just "append" each one to the Main sheet in the master.

    Is this the expected results for the Main sheet? if not please upload a version with the expected results.
    Attachment 738393
    Attached Images Attached Images
    If you find the suggestion or solution helpful, please consider adding reputation to the post.

  3. #3
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: collect data from multiple files and sheets

    Quote Originally Posted by cubangt View Post
    Is this the expected results for the Main sheet? if not please upload a version with the expected results.
    Attachment 738393
    yes it is the expected result . and one change is that
    from all workbooks their sheets copied into Master file with the same sheet name.
    e,g from East workbook copy all sheets into Master Workbook (macro file) in Sheet1 and Sheet2 and so on
    but the every sheet data append into the same sheet.
    Expected result is attached in this post
    Attached Files Attached Files

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

    Re: collect data from multiple files and sheets

    For the files uploaded.
    Assuming all the files(including "Master Data.xlsm") are in the same folder.
    Sub test()
        Dim myDir As String, e, temp, x, i As Long
        Dim cn As Object, rs As Object
        myDir = ThisWorkbook.Path & "\"
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then temp = e: Exit For
        Next
        If temp = "" Then MsgBox "No matched files in """ & myDir & """": Exit Sub
        Application.ScreenUpdating = False
        Sheets("main").Cells(1).CurrentRegion.Offset(1).ClearContents
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HRD=Yes"
            .Open myDir & temp & ".xlsx"
        End With
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then
                For i = 1 To 10
                    x = ExecuteExcel4Macro("'" & myDir & "[" & e & ".xlsx]sheet" & i & "'!r1c1")
                    If Not IsError(x) Then
                        rs.Open "Select * From `Sheet" & i & "$` In '" & myDir & e & ".xlsx" & _
                        "' 'Excel 12.0;''HDR=Yes;'''", cn, 3
                        Sheets("Main").Range("a" & Rows.Count).End(xlUp)(2).CopyFromRecordset rs
                        rs.Close
                    End If
                Next
            End If
        Next
        Application.ScreenUpdating = True
        Set rs = Nothing: Set cn = Nothing
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: collect data from multiple files and sheets

    Quote Originally Posted by jindon View Post
    For the files uploaded.
    Assuming all the files(including "Master Data.xlsm") are in the same folder.
    Perfect as always
    one change is that
    from all workbooks their sheets copied into Master file with the same sheet name.
    e,g from East workbook copy all sheets into Master Workbook (macro file) in Sheet1 and Sheet2 and so on
    but the every sheet data append into the same sheet.
    Expected result is attached in this post
    Attached Files Attached Files

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

    Re: collect data from multiple files and sheets

    Sub test()
        Dim myDir As String, e, temp, x, i As Long, ii As Long
        Dim cn As Object, rs As Object
        myDir = ThisWorkbook.Path & "\"
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then temp = e: Exit For
        Next
        If temp = "" Then MsgBox "No matched files in """ & myDir & """": Exit Sub
        Application.ScreenUpdating = False
        Sheets("main").Cells(1).CurrentRegion.Offset(1).ClearContents
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HRD=Yes"
            .Open myDir & temp & ".xlsx"
        End With
        Sheets("main").Cells(1).CurrentRegion.Offset(1).ClearContents
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then
                For i = 1 To 10
                    x = ExecuteExcel4Macro("'" & myDir & "[" & e & ".xlsx]sheet" & i & "'!r1c1")
                    If Not IsError(x) Then
                        If Not Evaluate("isref('sheet" & i & "'!a1)") Then Sheets.Add(, Sheets(Sheets.Count)).Name = "Sheet" & i
                        With Sheets("sheet" & i)
                            .Cells(1).CurrentRegion.Offset(1).ClearContents
                            rs.Open "Select * From `Sheet" & i & "$` In '" & myDir & e & ".xlsx" & _
                                    "' 'Excel 12.0;''HDR=Yes;'''", cn, 3
                            For ii = 0 To rs.Fields.Count - 1
                                .Cells(1, ii + 1) = rs.Fields(ii).Name
                            Next
                            With .Range("a" & Rows.Count).End(xlUp)(2)
                                .CopyFromRecordset rs
                                .Resize(rs.RecordCount).EntireRow.Copy Sheets("Main").Range("a" & Rows.Count).End(xlUp)(2)
                            End With
                            rs.Close
                        End With
                    End If
                Next
            End If
        Next
        Application.ScreenUpdating = True
        Set rs = Nothing: Set cn = Nothing
    End Sub

  7. #7
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: collect data from multiple files and sheets

    Quote Originally Posted by jindon View Post
    Thanks but it is not working properly . not fetching all data . please refer to result file.

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

    Re: collect data from multiple files and sheets

    Change to
    Sub test()
        Dim myDir As String, e, temp, x, i As Long, ii As Long
        Dim cn As Object, rs As Object, ws As Worksheet
        myDir = ThisWorkbook.Path & "\"
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then temp = e: Exit For
        Next
        For Each ws In Worksheets
            ws.Cells(1).CurrentRegion.Offset(1).ClearContents
        Next
        If temp = "" Then MsgBox "No matched files in """ & myDir & """": Exit Sub
        Application.ScreenUpdating = False
        Sheets("main").Cells(1).CurrentRegion.Offset(1).ClearContents
        Set cn = CreateObject("ADODB.Connection")
        Set rs = CreateObject("ADODB.Recordset")
        With cn
            .Provider = "Microsoft.Ace.OLEDB.12.0"
            .Properties("Extended Properties") = "Excel 12.0;HRD=Yes"
            .Open myDir & temp & ".xlsx"
        End With
        Sheets("main").Cells(1).CurrentRegion.Offset(1).ClearContents
        For Each e In Array("East", "North", "South", "West")
            If Dir(myDir & e & ".xlsx") <> "" Then
                For i = 1 To 10
                    x = ExecuteExcel4Macro("'" & myDir & "[" & e & ".xlsx]sheet" & i & "'!r1c1")
                    If Not IsError(x) Then
                        If Not Evaluate("isref('sheet" & i & "'!a1)") Then Sheets.Add(, Sheets(Sheets.Count)).Name = "Sheet" & i
                        With Sheets("sheet" & i)
                            rs.Open "Select * From `Sheet" & i & "$` In '" & myDir & e & ".xlsx" & _
                                    "' 'Excel 12.0;''HDR=Yes;'''", cn, 3
                            For ii = 0 To rs.Fields.Count - 1
                                .Cells(1, ii + 1) = rs.Fields(ii).Name
                            Next
                            With .Range("a" & Rows.Count).End(xlUp)(2)
                                .CopyFromRecordset rs
                                .Resize(rs.RecordCount).EntireRow.Copy Sheets("Main").Range("a" & Rows.Count).End(xlUp)(2)
                            End With
                            rs.Close
                        End With
                    End If
                Next
            End If
        Next
        Application.ScreenUpdating = True
        Set rs = Nothing: Set cn = Nothing
    End Sub

  9. #9
    Forum Contributor
    Join Date
    05-26-2016
    Location
    India
    MS-Off Ver
    Microsoft 2021
    Posts
    324

    Re: collect data from multiple files and sheets

    Thanks Jindon

+ 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. Replies: 6
    Last Post: 01-31-2019, 03:32 PM
  2. Replies: 2
    Last Post: 01-17-2019, 05:31 AM
  3. Collect data from multiple sheets
    By xe-dingo in forum Excel General
    Replies: 2
    Last Post: 09-22-2014, 02:38 PM
  4. Replies: 2
    Last Post: 12-17-2013, 08:21 AM
  5. Replies: 4
    Last Post: 05-02-2013, 11:16 AM
  6. Macro to collect data from multiple closed spread sheets.
    By Awangkualizul in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-27-2013, 08:27 AM
  7. [SOLVED] Collect data from multiple sheets into a Summary Sheet
    By Fuhgawz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-10-2012, 12:56 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