+ Reply to Thread
Results 1 to 17 of 17

Need macro to merge data from external workbooks into one master

Hybrid View

  1. #1
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Need macro to merge data from external workbooks into one master

    I have been looking for a solution to a project I am working on. I want to copy all data contained on the second worksheet in each workbook located in the same directory into one master workbook. Each external workbook's data would be pasted into its own sheet inside the master workbook. I am then planning on having a master sheet inside the master workbook that will reference this data and manipulate into a common template I utilize.

    A problem I have been running into trying out other solutions in this forum is that the external worksheets I am cycling through have their own macros inside them. So I am assuming I will need a solution that either turns off macros when opening the external worksheets or does not open the external worksheets at all.

    Thoughts?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    'WORKBOOKS TO SHEETS
    Here's a macro for collecting data from all files in a specific folder. This version copies the sheet in as a whole.The parts of the code that need to be edited are colored to draw your attention.

    This macro disables other macro events near the top of the macro, this is the snippet that does that, FYI.
    Application.EnableEvents=False
    THis part will need to be edited to grab sheet(2) data instead of Sheet(1).
            'Rename sheet and copy to target workbook
                wbData.Sheets(1).Name = shtAdd
                wbData.Sheets(1).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
    Last edited by JBeaucaire; 12-27-2019 at 04:27 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    This is perfect - thank you so much! I appreciate your time and help.

    I did run into a runtime error when executing the macro:

    "Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and column than the source workbook. To move or copy the data to the destination workbook, you can select the data, and then use the Copy and Paste commands to insert it into the sheets of another workbook."

    My source workbooks have ~4400 rows and go into column DR.

    Here is my code:

    Option Explicit
    
    Sub ConsolidateWBsToSheets2()
    'Author:     Jerry Beaucaire'
    'Date:       6/23/2010     (2007 compatible)
    'Summary:    Open all Excel files in a specific folder and copy
    '            one sheet from the source files into this master workbook
    '            naming sheets for the names of the source workbooks
    '            Move imported files into another folder
    
    Dim fName As String, fPath As String, fPathDone As String
    Dim LR As Long, NR As Long, shtAdd As String, ShtName As Worksheet
    Dim wbData As Workbook, wbkNew As Workbook
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
      
        Set wbkNew = ThisWorkbook
        wbkNew.Activate
      
    'Path and filename (edit this section to suit)
        fPath = ThisWorkbook.Path & "\Test\" 'remember final \ in this string
        fPathDone = fPath & "Imported\"     'remember final \ in this string
        On Error Resume Next
            MkDir fPathDone                 'creates the completed folder if missing
        On Error GoTo 0
    
        fName = Dir(fPath & "*.xlsm")                'listing of desired files, edit filter as desired
    
    'Import data from each found file
        Do While Len(fName) > 0
        'make sure THIS file isn't accidentally reopened
            If fName <> wbkNew.Name Then
            
            'This is the section to customize, what to copy and to where
            'Get name of workbook without extension
                shtAdd = Left(fName, InStr(fName, ".") - 1)
            'Open file
                Set wbData = Workbooks.Open(fPath & fName)
                
            'Rename sheet and copy to target workbook
                wbData.Sheets(2).Name = shtAdd
                wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
               
            'close source file
                wbData.Close False
            'move file to IMPORTED folder
                Name fPath & fName As fPathDone & fName
            'ready next filename, reassert the list since a file was moved
                fName = Dir(fPath & "*.xlsm")
            End If
        Loop
    
    ErrorExit:    'Cleanup
        Application.DisplayAlerts = True         'turn system alerts back on
        Application.EnableEvents = True          'turn other macros back on
        Application.ScreenUpdating = True        'refreshes the screen
    End Sub
    Last edited by sirhacksalot; 07-02-2013 at 09:15 AM.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    You copying from .xlsm files into an .xls file? If you change your master workbook into an .xlsm file, too, you should be able to run it.


    Along with tweaking those two lines of code, I noticed a third edit, a bunch of text removed from above the DIMs at the top. (pout)

  5. #5
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    I edited my last post, my apologies.

    Thanks for the reply - it still has the same error though even after I saved the master workbook as an .xlsm before running the macro again... when I choose to debug it highlights the following line:

    wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
    Don't know if it matters or not but the master workbook has Sheet1, Sheet2, Sheet3, and Capacity_Summary.


    UPDATE: I closed out everything and reopened after saving the original master file as a .xlsm and it worked! Thanks again for your help!
    Last edited by sirhacksalot; 07-02-2013 at 09:42 AM.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    Glad it's working for you, another successful use of that macro, one of the most frequently used from my site. Cheers!

  7. #7
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    I came across an issue that I cannot seem to figure out. The macro has been working beautifully for only one of the types of files I need it to pull in to the master workbook. All I have added to the macro from the above are nested case statements for correctly identifying the type of spreadsheet I have and the manager it has been submitted for. I have gone through the macro step by step with the files that are not being copied in and no errors are displayed but the target sheet is never copied into the master workbook. However, it is being moved into the "Imported" folder as specified in the macro.


        Case InStr(1, shtAdd, name7Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name 7 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name 7 HC"
    
            End Select
    
    End Select
                
            'Open file
                Set wbData = Workbooks.Open(fPath & fName)
                
            'Rename sheet and copy to target workbook
                wbData.Sheets(2).Name = shtAdd
                wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
               
            'close source file
                wbData.Close False
            'move file to IMPORTED folder
                Name fPath & fName As fPathDone & fName
            'ready next filename, reassert the list since a file was moved
                fName = Dir(fPath & "*.xlsm")
            End If
    The case for searching for "BOW" in the name works for those spreadsheets. The case for "HC" evaluates to true for those type of spreadsheets and shtAdd is renamed as specified (verified during debug). The macro opens the target file, goes to the second worksheet, seemingly copies the data and pastes it in the master workbook and then closes the file. After I finish all the steps though, there aren't any new worksheets in the master workbook as there should be. Help!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    Heh, I'd need to see the whole macro.

  9. #9
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    Option Explicit
    
    Sub ConsolidateWBsToSheets2()
    'Author:     Jerry Beaucaire'
    'Date:       6/23/2010     (2007 compatible)
    'Summary:    Open all Excel files in a specific folder and copy
    '            one sheet from the source files into this master workbook
    '            naming sheets for the names of the source workbooks
    '            Move imported files into another folder
    
    Dim fName As String, fPath As String, fPathDone As String
    Dim LR As Long, NR As Long, shtAdd As String, ShtName As Worksheet
    Dim wbData As Workbook, wbkNew As Workbook
    Dim bowStr As String
        bowStr = "BOW"
    Dim hcStr As String
        hcStr = "HC"
    Dim name1Str As String
        name1Str = "Name1"
    Dim name2Str As String
        name2Str = "Name2"
    Dim name3Str As String
        name3Str = "Name3"
    Dim name4Str As String
        name4Str = "Name4"
    Dim name5Str As String
        name5Str = "Name5"
    Dim name6Str As String
        name6Str = "Name6"
    Dim name7Str As String
        name7Str = "Name7"
    Dim name8Str As String
        name8Str = "Name8"
    
    Dim pos As Integer
    
    'Setup
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        Application.DisplayAlerts = False
      
        Set wbkNew = ThisWorkbook
        wbkNew.Activate
      
    'Path and filename (edit this section to suit)
        fPath = ThisWorkbook.Path & "\Test\" 'remember final \ in this string
        fPathDone = fPath & "Imported\"     'remember final \ in this string
        On Error Resume Next
            MkDir fPathDone                 'creates the completed folder if missing
        On Error GoTo 0
    
        fName = Dir(fPath & "*.xlsm")                'listing of desired files, edit filter as desired
    
    'Import data from each found file
        Do While Len(fName) > 0
        'make sure THIS file isn't accidentally reopened
            If fName <> wbkNew.Name Then
            
            'This is the section to customize, what to copy and to where
            'Get name of workbook without extension
                shtAdd = Left(fName, InStr(fName, ".") - 1)
                    
    Select Case True
    
        Case InStr(1, shtAdd, name1Str, vbTextCompare) <> 0
        
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name1 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name1 HC"
    
            End Select
            
        Case InStr(1, shtAdd, name2Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name2 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name2 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name3Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name3 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name3 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name4Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name4 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name4 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name5Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name5 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name5 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name6Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name6 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name6 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name7Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name7 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name7 HC"
    
            End Select
    
        Case InStr(1, shtAdd, name8Str, vbTextCompare) <> 0
    
            Select Case True
                Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
                    shtAdd = "Name8 BOW"
                
                Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
                    shtAdd = "Name8 HC"
    
            End Select
    
    End Select
                
            'Open file
                Set wbData = Workbooks.Open(fPath & fName)
                
            'Rename sheet and copy to target workbook
                wbData.Sheets(2).Name = shtAdd
                wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
               
            'close source file
                wbData.Close False
            'move file to IMPORTED folder
                Name fPath & fName As fPathDone & fName
            'ready next filename, reassert the list since a file was moved
                fName = Dir(fPath & "*.xlsm")
            End If
            
        Loop
    
    ErrorExit:    'Cleanup
        Application.DisplayAlerts = True         'turn system alerts back on
        Application.EnableEvents = True          'turn other macros back on
        Application.ScreenUpdating = True        'refreshes the screen
    End Sub

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    Your edits look solid. I can't fault the technique.

    Further, since you have On Error Goto 0 active, then when the macro fails to copy the sheet over to the main workbook, it should give an error.


    I guess you could give me a few test files and your master, I can do a step by step F8 walk through and see if I can spot where it doesn't do what you want.

  11. #11
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    Thanks for the help.

    I am working on trying to get you example files. The ones I will be (and am) working with are protected workbooks and I am not able to edit them.

    However, I tried to manually copy the content from the problem workbook into the master workbook and the attached picture is the error I got. Do you know of any way to bypass or fix this error during runtime since these will all be protected workbooks I will be working with? My initial thought on the matter is that I will need to have the owner update the template being used so this error doesn't happen.

    macro-error.JPG

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    1) This macro will probably fall over on data sets that full of merged cells. I don't do that.

    2) You will most likely need to unprotect any sheet before you can copy from one workbook to another.

  13. #13
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    Quote Originally Posted by JBeaucaire View Post
    1) This macro will probably fall over on data sets that full of merged cells. I don't do that.

    2) You will most likely need to unprotect any sheet before you can copy from one workbook to another.

    It turned out to be an issue with the protections because the BOW sheets work fine but the HC ones do not. They're both protected workbooks and both have merged cells.

    The hurdle I'm testing through now is that when I manually copy the worksheet and then go to paste it in the master workbook, I get an alert saying there was already a named range in the worksheet that is the same as the one being copied into the worksheet - do you want to use the new one? I deleted the named range from the Name Manager in the master workbook and then am able to manually copy and paste the worksheet in but the macro still won't copy in the worksheet successfully. No errors thrown.

    Any suggestions? Is there code for accepting alert defaults? It looks like the range is for a dropdown in the HC file. The BOW file doesn't have any named ranges.
    Last edited by sirhacksalot; 08-21-2013 at 02:18 PM. Reason: Added question

  14. #14
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    OK, latest update (sorry I'm sort of live blogging this)...

    1) I realize you already have
    Application.DisplayAlerts = False
    so that was a stupid question in my last post, sorry

    2) I added the destination of the named range in the source workbook to the master file (a hidden tab) and went to hide it in the master file when I realized - the macro has been successfully adding the HC data in to the master file but hiding the worksheets! Dah! Why is it doing that though?

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    The macro given does not hide worksheets. These are all most likely side effects of copying from these protected sheets. My macro actually doesn't check the visibility state of any worksheet, it just renames the sheet and moves it into the master, then closes the original workbook without saving any changes.

    As such, whatever the characteristics of the sheet (hidden or visible, etc) should be reflected in the new workbook, too.

    Of course you can just add in a line to make sure each sheet is made visible right after it is moved, should take care of that when it happens.

  16. #16
    Registered User
    Join Date
    06-25-2013
    Location
    Jacksonville, FL
    MS-Off Ver
    Excel 2007
    Posts
    30

    Re: Need macro to merge data from external workbooks into one master

    I can't thank you enough for your help and the use of your macro! It has been invaluable.

    I did have one more question though - is it possible for the macro to find the worksheet with a specified name instead of an index of 2 from the slave workbook? I'm running into some inconsistencies from the submitted files I'm copying from so I need to set up an if statement and have it search by worksheet name for one of the file types.

    This is the Parr I'm talking about changing:
       'Rename sheet and copy to target workbook
                wbData.Sheets(2).Name = shtAdd
                wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)

  17. #17
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need macro to merge data from external workbooks into one master

    So try this:
       'Rename sheet and copy to target workbook
                wbData.Sheets("MySheet").Name = shtAdd
                wbData.Sheets(shtAdd).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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