+ Reply to Thread
Results 1 to 20 of 20

VBA to merge multiple Workbooks with multiple sheets retaining headers

  1. #1
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Red face VBA to merge multiple Workbooks with multiple sheets retaining headers

    VBA to merge multiple Workbooks with multiple sheets into one workbook with multiple sheets, each with a different header row.

    Hello

    I am using Excel 2007 and want to achieve the following.

    I have 75 Workbooks('xlsm) in a directory, with the same 4 worksheets in each.
    The names of the 4 worksheets are "Wins","Pitches","Awards","Promo"

    Each worksheet has header rows starting at A3 which are unique to that Sheet i.e the headers for "Wins" are different to the headers for "Pitches"etc.
    Data always commences at Row A4 regardless of which sheet it is.

    Also the data within each sheet contains references to "named Ranges" which need to be retained in the transfer if possible.,

    I have tried lots of different VBA's and the RDB Merge Macro, but I come up against any one of the following issues.

    a) Sometimes it asks me if I want to replace the Named Range or keep it as it is. (This is very tiresome as I have about 150 named ranges and it asks me over and over for each occurence within the sheets and the workbooks.)

    b) I can get pretty close by using RDB Merge, but that restricts me to converting to "values" and does not import the headers or the formats orginally used. Also this has to be repeated for each worksheet separately.

    I don't mind if the overall result converts to values as long as the headers and formats are retained for each worksheet.

    I would really appreciate any help here.
    Last edited by LauraN1; 08-19-2010 at 01:39 PM.

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    'WORKBOOKS W/MULTIPLE SHEETS MERGED INTO SHEETS
    I have a macro that may be pretty close to "ready to use" for collecting data from all files in a specific folder. (and/or subfolders). Published here:
    =========
    I've edited it for what I believe are your needs in terms of sheets, but you will need to edit the fPath string in line 22 This macro goes into the consolidation workbook, and that workbook should not be in the same folder as the files being imported.

    Please Login or Register  to view this content.
    _________________
    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
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Hi Jerry

    Thank you so much for your reply.

    I'm not sure if I am doing this right but I changed the fPath = "C:\2010\" to
    fPath = "C\countries\merging\" which is where all the spreadsheets are held

    I also changed fName = Dir("*.xls") to fName = Dir("*.xlsm") as these are the extensions to my files.

    I have copied the programming to a new empty file in a different directory. (Am I supposed to copy all the headers across first or not?)

    When I run the macro I get the message - "Compile Error" "Syntax Error" and then the degugger highlights the top line - Sub ConsolidateWBsToSheets() and goes no further.

    What am I doing wrong?

    Thanks for all your help

    Laura
    Last edited by shg; 08-18-2010 at 02:51 PM. Reason: deleted quote

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Post up your workbook with the misbehaving macro in it along with a copy of one of your import files. I'll take a look directly.

  5. #5
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Hi Jerry

    Thanks so much for your help.
    I am attaching the following files:
    Consolidated empty file with macro - "consolidate.xlsm"
    two examples of files to be merged - "australia.xls","belgium.xls"
    Empty workbook with headers and formatted columns. - "forum.xlsm"


    Look forward to your reply

    Laura

    consolidate.xlsm

    forum.xlsm

    australia.xlsx

    belgium.xlsx

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Remove everything after END SUB in the macro module. That's extra garbage and Excel can't find the END SUB.

    Also, the macro would be IN the workbook you are creating.

  7. #7
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Hi Jerry

    Have removed everything after the End Sub.
    Have put the macro in a blank template.
    Now when I run the macro I get the following:-
    "Run time error "91" Object variable or with block variable not set.
    The debugger then sends me to this line in the macro:-
    LR = ws.Cells.Find("*", Cells(ws.Rows.Count, ws.Columns.Count), _
    SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    I also notice that it has opened up one of the import files: "Australia" and is just replicating what is in the Australia Wins and Losses Line in all the other worksheets i.e current Pitches, Awards, Promotion.
    Each of these different worksheets within the workbook must not be amalgamated together only the same named worksheets from each of the other files i.e
    Australia Wins and Losses should be amalgamated with Belgium Win & Losses.
    Australia Current Pitches should be amalagamated with Belgium Current Pitches etc.

    Hope this makes sense.

    As always looking forward to your reply.

    Many thanks

    Laura

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Oops, one big booboo in the targeting code and I added the need to look and see if there is any data to copy in the first place.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    OK

    Have put the new coding into the empty spreadsheet.
    I now get: "Run-time error "9" Subscript out of range.
    The debugger points at
    ws.Range("A4:A" & LR).EntireRow.Copy _
    wbkNew.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

    Also the first imported file - Australia opens up with its own data in it.

    Am I right to have put this macro into an empty file with no headers?

    Looking forward to your response

    Laura

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    I put the macro into the Consolidation book you uploaded.

  11. #11
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Ok ,that is where I put it.
    I have set the fpath to the directory where australia.xlsx and belgium.xlsx are held for this test purpose (i.e in the excel test sub folder).Line reads as follows:
    fPath = "C:\Documents and Settings\NealL\My Documents\\excel test\"

    When the macro out of the consolidate workbook I still get the same errors as the last post.
    "run time errror "9" Subscript out of range
    ws.Range("A4:A" & LR).EntireRow.Copy _
    wbkNew.Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)

    As before Australia.xlsx opens in its original format.

    Am I doing anything else wrong?

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Yes, Australia workbook should be open at that moment, the macro is trying to copy FROM that workbook, so it should be open. The macro is bailing before it has a chance to copy from and close that workbook...moving on to Belgium next.

    There are variables in that line of code. When you DEBUG, what are the values in each of the variables? Hover your mouse over each to read them:

    LR =
    ws.Name =

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

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Ah, I didn't actually know about those hidden sheets (tsk-tsk).

    This version will NOT look at any other sheets. Try this:
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Sorry about the hidden sheets. I use those for the name ranges.

    I have copied the new coding and it fails again. Same error message

    I think these are the values you need from the debug

    LR =5
    ws.Range("A"&ws.Rows.Count).End(xlUp).Rows=5

    I don't know if this is of any relevance, but all the empty rows are formatted and most include drop down options and data validation, so in a sense are not exactly empty.

    I look forward to your response.

    I think here in the UK we are 8 hrs ahead of California, so if you don't mind, I will look at your post tomorrow. Look forward to it.

    Laura

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Quote Originally Posted by JBeaucaire View Post
    LR =
    ws.Name =
    The second variable you got wrong. What is the ws.name?

  16. #16
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Sorry - thanks for being patient with me.

    w.sName="Wins & Losses"

  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: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Subscript error 9 and the ws.Name = "Wins & Losses" leads me to believe the macro can't find the "Wins & Losses" sheet in your wbkNew workbook.

  18. #18
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    OK Now I get it

    This time I have put the macro into my blank spreadsheet which has all the worksheet names, the headers and the formatting and hey presto it works like a dream !
    You are a genius. Nobody else has managed to get anywhere close.

    I can't thank you enough for all your time and effort.

    Since I am new to this forum, how do I go about rating your postings .

    Laura

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

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    Use the BLUE SCALES icon in the upper right corner of one of my posts.

  20. #20
    Registered User
    Join Date
    08-16-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: VBA to merge multiple Workbooks with multiple sheets retaining headers

    That's Done

    Thanks once again

    Laura

+ 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