+ Reply to Thread
Results 1 to 8 of 8

Macro to copy rows to new workbook & separate worksheets

  1. #1
    Registered User
    Join Date
    10-18-2010
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    4

    Macro to copy rows to new workbook & separate worksheets

    Hello

    Can anyone help me with a macro for the following problem.

    I have a master sheet with hundreds of rows. When the category in column A (department name) changes, I want to export these rows into a new workbook which will be called after that department.

    eg. SALES.XLS

    I have seen some great posts already on this site which shows me how to do this.(e.g. http://www.excelforum.com/excel-prog...e-in-data.html)

    However I have another requirement which complicates this.

    There is another column in the master sheet which lists dates in dd/mm/yy format. Once the rows are exported to the new workbooks, I need these rows to further separate into four different worksheets, one for each quarter.

    Q1,Q2,Q3,Q4

    There are perhaps a dozen differnt departments in the master sheet (HR, IT, Sales, etc)


    Once the macro is run, the master sheet should still exist, but now there will be 12 new workbooks named after the department they contain, all with four worksheets, one for each quarter.

    Finally, if possible, they should all be saved to a new location within a folder called after the department

    e.g. S:\\MIS data\Sales\Sales.xls

    Any help would be much appreciated.

    Martin
    Last edited by malachi; 10-27-2010 at 08:50 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to copy rows to new workbook & separate worksheets

    Hello malachi,

    Welcome to the Forum!

    It would be a big help if you could two things: Post a copy of the master workbook and define the dates or months for each quarter. Once you have done that, it will be easier to provide you with a solution.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: Macro to copy rows to new workbook & separate worksheets

    'SHEET1 TO MANY WORKBOOKS

    I have a macro that may be "ready to use" for parsing rows of data from one sheet to many workbooks named for the same values.I've edited the SAVE part to use your naming convention, just make sure those directories already exist and they will save into there. The original code on the website added today's date to the named file, you can add that back in if you wish.


    Since this macro is good for getting the one worksheet out to many workbooks, I wrote a small routine that would then split out that new workbook to multiple worksheets, it's called ParseQuarters.

    Here are the two macros:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 10-21-2010 at 10:52 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!)

  4. #4
    Registered User
    Join Date
    10-18-2010
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro to copy rows to new workbook & separate worksheets

    Leith - thank you for the welcome and the guidelines. I will follow these in future.

    Jerry - thanks a million for posting the code. I look forward to trying this out. Really appreciate the help.

  5. #5
    Registered User
    Join Date
    10-18-2010
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro to copy rows to new workbook & separate worksheets

    Hi Jerry,

    I tried the macro you posted without success (due to my own lack of experience with VB). I get a Type Mismatch error, though I suspect it's just a small adjustment that is required. I attach a copy of sample data - would you be able to have a look?

    I've named the worksheet "MIS" and I've updated the code to reflect this. Also, I've updated the code to send the new workbooks & worksheets to the destination H:\2010\
    (This folder does exist).

    Can you advise what the problem is?

    Thanks again.

    Martin
    Attached Files Attached Files

  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: Macro to copy rows to new workbook & separate worksheets

    You didn't indicate to me where the type mismatch occurred for you, and when I ran the main macro it worked OK.

    The second macro you failed to edit the DateCol variable at the top to the proper column with dates for the parsing.

    I decided to restructure the new macro a little so you send the DateCol a value in the CALL command from the main macro, that should make it a little more obvious what is needed. I also corrected some filtering errors I spotted in the second macro now that I had an actual dataset to test it on.

    Please remove all code from the ThisWorkbook module. These macros do not belong in there.

    Click on Insert > Module and put these into the standard module that appears.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-18-2010
    Location
    Dublin
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Macro to copy rows to new workbook & separate worksheets

    Thank you again. I see what you mean about the Datecol value. I'm slowly getting to grips with it.

  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: Macro to copy rows to new workbook & separate worksheets

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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