+ Reply to Thread
Results 1 to 9 of 9

merging specific worksheets from multiple workbooks into one new workbook

  1. #1
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    merging specific worksheets from multiple workbooks into one new workbook

    Hi,

    Can someone provide a code for this?

    Currently, there is a workbook for each location stored in one directory. Inside each workbook are different datasets separated by worksheets. The setup in the workbooks are the same. They all have about 7-10 different worksheets with the same name. For example, there's an "Overview" sheet, if there's credit card data, there will be a "Credit Card" sheet, and same goes with cash. I would like to create a macro to open all the workbooks and extract the credit card and cash sheets and compile it into a new workbook for further analysis.

    Thanks in advance for the much needed help!

  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: merging specific worksheets from multiple workbooks into one new workbook

    Attach some sample workbooks. A couple of the source samples, and a mockup of the expected results from those two workbooks in a consolidation sample. Make sure there is just enough data to demonstrate your need.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic
    _________________
    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
    05-23-2014
    Posts
    37

    Re: merging specific worksheets from multiple workbooks into one new workbook

    Attached are 3 samples of the spreadsheets' layout for each location and "Master" is the way I would like the results to be. I want to create a macro that opens the files from the directory (file name needs to be dynamic cause there's a date attached at the end) and then pick up the specific worksheets if there's data at that location and combine it all into the "Master". I hope that makes sense.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: merging specific worksheets from multiple workbooks into one new workbook

    I found your code below and tried to edit parts of it (in red) but got stuck with runtime error '438' at
    Please Login or Register  to view this content.

    Please Login or Register  to view this content.

    Am I approaching this correctly? I haven't attempted to add the two extra columns I want as headers in the Master Sheet. Hopefully I didn't confuse you more.

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

    Re: merging specific worksheets from multiple workbooks into one new workbook

    At first glance, an error on that line usually means one of the sheets you have listed in the array does not exist. Perhaps a missing "s" .... Check vs Checks or something like that, a hidden space in the sheet name...


    Second, these two lines of code do not insert two columns, it deletes top two rows:
    Please Login or Register  to view this content.
    You don't have to SELECT something to delete in VBA, only humans have to do that when working manually. To delete the top two rows the command would simply be:

    Please Login or Register  to view this content.
    To insert two columns:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-10-2014 at 07:16 PM.

  6. #6
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: merging specific worksheets from multiple workbooks into one new workbook

    Thanks for the cleanup tip!

    You are correct about the spelling of the sheet name error. Now I am getting runtime error '1004': Method 'Range of object'_Worksheet' failed at the line in red below
    Please Login or Register  to view this content.
    Is it because the title starts at row 3 and the subsequent spreadsheets should copy just the data starting at row 4? Is that correct? The current code deleted the first 2 rows on the first sheet (named "Overall"), which isn't one of the sheets I needed. It also ran into error when I tried to insert the two new columns.

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

    Re: merging specific worksheets from multiple workbooks into one new workbook

    When dealing with multiple moving objects (opening and closing workbooks, for instance), it's best to insure every command is attached to a parent object. This command is not, so always applies to the current sheet onscreen:
    Please Login or Register  to view this content.
    Those should be attached to the ws object so they are targeted correctly, is my guess:
    Please Login or Register  to view this content.
    Your initial supposition that the sheets were laid out the same isn't correct? Sheet names different, data starts on different rows in different workbooks or worksheets?

  8. #8
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: merging specific worksheets from multiple workbooks into one new workbook

    Each location has the same sheet names if there's data for it. The order may vary. Data on each sheet starts on row 3 (header). Row 1, cell 1 is the location; row 2 is empty. I will need to create two new columns in the Master workbook to differentiate: 'location' and 'method of payment'. I will probably put in the formula to input r1c1's value into the new location column, then a formula to input the tab's name in 'new method of payment' column before it gets consolidated into the Master.

    Example:
    Asia Workbook: 5 sheets: "Overall", "Cash", "Checks", "Wires", etc
    Europe Workbook: 7 sheets: "Overall", "Cash", "Checks", "Wires", "Credit Cards", etc
    America Workbook: 7 sheets: "Overall", "Cash", "Wires", "Checks", "Credit Cards", etc

  9. #9
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: merging specific worksheets from multiple workbooks into one new workbook

    Hi,

    Here is the entire code with my customized section. It just stops without opening the next workbook to repeat the steps. I don't think I set the array properly which is shown in red. The error I'm getting is type mismatch. Can you please tell me how to fix this for the looping? Also, since those specific worksheets in the array ("Cash", "Checks", etc.) are only there when there's data, do I need to use the boolean expression? Very much appreciated for all the help thus far!



    Please Login or Register  to view this content.
    Last edited by cwchan220; 07-16-2014 at 10:08 AM. Reason: other errors

+ 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. [SOLVED] Merging Data from Multiple Workbooks into 1 Workbook (Inc Subfolders)
    By CMR_Steve in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-02-2012, 08:51 AM
  2. Merging multiple workbooks on to one master workbook
    By inkandpaint in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-15-2012, 01:22 PM
  3. Merging multiple workbooks (with multiple worksheets) in to one master sheet
    By inkandpaint in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-22-2012, 09:46 AM
  4. Replies: 2
    Last Post: 05-26-2006, 01:35 AM
  5. Merging Workbooks with Multiple Worksheets
    By kronik in forum Excel General
    Replies: 1
    Last Post: 02-15-2006, 12:01 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