+ Reply to Thread
Results 1 to 9 of 9

Would like a Macro to Combine data from the same worksheet in multiple files into one

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2024
    Location
    Atlanta, GA
    MS-Off Ver
    Microsoft 365
    Posts
    1

    Would like a Macro to Combine data from the same worksheet in multiple files into one

    I have 10 different files in 1 folder. Each file has multiple worksheets and each worksheet is named. All the files have a worksheet named "Out". All the columns in this worksheet named "Out" are the same with different data in each column. I would like to see the VBA code for a macro that would take the data from the same worksheet in all the files and combine the data into one file.

    Thanks,

    Mike

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,081

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    Hi Mike...

    Welcome to the Forum...
    If it is as simple as per your explanation then this will work...
    Place code in a blank workbook and change red snippet to Path of the Folder...
    Untested as I have no sample of these 10 files...
    Sub J3v16()
    Dim Data, File As Object, Path As String, i As Long
    Path = "D:\Steven\Desktop\Folder\"
    With CreateObject("Scripting.FileSystemObject").GetFolder(Path)
        For Each File In .Files
            With Workbooks.Open(File)
                '! Assumes data starts in A1 ...
                Data = Sheets("Out").Cells(1).CurrentRegion
                .Close False
            End With
            ActiveSheet.Range("A" & Rows.Count).End(xlUp)(2).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
        Next File
    End With
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,479

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    Sintek:

    The macro functions as desired but at the end there is a warning message that the main workbook is already open and do you want to reopen it ?

    How to make that go away ?

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,081

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    @Logit...

    Please explain your testing process...
    Last edited by Sintek; 11-12-2024 at 11:17 AM.

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,479

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    I simply ran the macro. No other testing.

    Have to step away for a few hours.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,081

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    Code goes in blank book...loops the 10 different files in 1 folder extracting, then closing, and combines data in blank book...No errors, runs perfectly for me...

  7. #7
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,479

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    I'm thinking there is a setting in Excel that I am overlooking. But I don't know what or where.

    Here is another macro that does the same thing of copy/paste and then reopens the workbook as read only. I can't figure that out either.
    There are a number of code lines that are commented out during my testing for different solutions.

    Sub CopyDataFromWorkbooks6()
        Dim wb As Workbook
        Dim masterWb As Workbook
        Dim sourceWb As Workbook
        Dim sourceSheet As Worksheet
        Dim masterSheet As Worksheet
        Dim folderPath As String
        Dim fileName As String
        Dim lastRow As Long
        'Dim FullName As String
    
        ' Set the folder path
        folderPath = ThisWorkbook.Path & "\" ' Ensure the path ends with a backslash
        fileName = Dir(folderPath & "*.xls*")
        
        ' Create or set the Master Workbook
        Set masterWb = ThisWorkbook
        Set masterSheet = masterWb.Sheets("Sheet1")
        
        ' Clear existing data in Master Sheet
        'masterSheet.Cells.Clear
        
        ' Disable alerts
        Application.DisplayAlerts = False
        
        ' Loop through each workbook in the folder
        Do While fileName <> ""
        
            Set sourceWb = Workbooks.Open(folderPath & fileName, ReadOnly:=True)
            On Error Resume Next
            Set sourceSheet = sourceWb.Sheets("Out")
            On Error GoTo 0
            
            If Not sourceSheet Is Nothing Then
             
                ' Find the last row in Master Sheet
                lastRow = masterSheet.Cells(masterSheet.Rows.Count, 1).End(xlUp).Row
                
                ' Copy UsedRange from source sheet and paste to Master Sheet
                sourceSheet.UsedRange.Copy
                masterSheet.Cells(lastRow + 2, 1).PasteSpecial Paste:=xlPasteAll
                
                ' Close the source workbook
                sourceWb.Close False
                masterWb.Save
             
            End If
            
            
            fileName = Dir
    
            
        Loop
        
    'ThisWorkbook.Close
    
        Application.DisplayAlerts = True
    
    'ThisWorkbook.Save
    'Application.Workbooks.Open (ThisWorkbook.FullName)
        
    End Sub

  8. #8
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,479

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    Sintek :

    After much more research I located the issue with my macro.

    Change this line of code : Set sourceWb = Workbooks.Open(folderPath & fileName, ReadOnly:=True)

    To this : Set sourceWb = Workbooks.Open(folderPath & fileName, ReadOnly:=False)

    Problem solved.



    With your macro I was able to solve my issue with these changes :

    Option Explicit
    
    Sub J3v16()
    Dim Data, File As Object, Path As String, i As Long
    Path = "C:\Users\logit\OneDrive\Desktop\New Folder"
    
    Application.DisplayAlerts = False
    With CreateObject("Scripting.FileSystemObject").GetFolder(Path)
        For Each File In .Files
            
            With Workbooks.Open((File), ReadOnly:=False)
                '! Assumes data starts in A1 ...
                Data = Sheets("Out").Cells(1).CurrentRegion
                .Close False
                
            End With
            ActiveSheet.Range("A" & Rows.Count).End(xlUp)(2).Resize(UBound(Data, 1), UBound(Data, 2)) = Data
            
        Next File
       ActiveWorkbook.Save
       ActiveWorkbook.Close
       Application.Quit
    End With
    
    End Sub

    Thank you for your time.

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,081

    Re: Would like a Macro to Combine data from the same worksheet in multiple files into one

    there is a warning message that the main workbook is already open and do you want to reopen it ?
    Cannot understand this as the code does not effect the main book in any way...
    Thank you for your time.
    Where is Mike in all of this...
    Last edited by Sintek; 11-13-2024 at 01:09 AM.

+ 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: 8
    Last Post: 03-14-2017, 03:49 PM
  2. [SOLVED] New to Excel programming. In need of a macro to combine multiple files with multiple tabs.
    By nflores0303 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-12-2016, 11:03 AM
  3. Macro to combine and clean multiple files
    By micahjb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2013, 07:16 PM
  4. [SOLVED] Combine multiple (100+) excel files w/ one worksheet each into one giant worksheet
    By justin11 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-03-2013, 01:05 PM
  5. Macro to combine many excel files into one workbook/worksheet
    By excel1983 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-19-2012, 02:32 PM

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