+ Reply to Thread
Results 1 to 25 of 25

Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

  1. #1
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I have downloaded data which must be transferred onto a form (as shown on "template" tab) in a very specific way.
    I have color coded the fields from the data tab to cells on the template tab, indicating where the data needs to be copied to, and I have indicated the cell reference above each column heading on the data tab as to where it belongs on the templates. The template tab is simply there for reference. I have created 5 tabs so far in this example, from rows 11-17 of the data. The macro will need to create new tabs along the way, with the template layout. The color coding should not be kept when creating new tabs. Each row on the data tab will result in two rows of data on the tabs created by the template. Those two rows will be rows 14 & 15, or below, if there is more than one row of data being copied to the same tab. Each row on the data tab must be entered onto it's own tab, with the following exception:

    After entering data from a row on the data tab to a tab created by the template tab, you may continue using the same tab for the next row of data IF: both "name" (column A on data tab) AND "Pay End Date" (column D on data tab) are equal, from the row you just copied over and the next row, those two rows of data can go on the same tab. For example, on the data tab, name and pay end date are equal in rows 11 and 12. Those may go on the same tab. However, when comparing rows 12 and 13, even though name is the same, because pay end date isn't the same, that row of data must go on it's own tab.
    Every new tab will have data in rows 14 & 15. Row 15 will be copied from the data tab. Row 14 will be referencing the row below but be negative.

    I created the first 5 tabs as an example for data in rows 11-17 of the data tab. There could be a few hundred lines of data and that is why I want to write a macro to do this.
    Tabs should be named with their pay end date and name initials, if possible. If this is too difficult, then sheet1, sheet2 or anything else is fine.

    Any help would be greatly appreciated.
    Attached Files Attached Files
    Last edited by JudyF; 11-03-2017 at 01:54 PM.

  2. #2
    Forum Contributor
    Join Date
    01-09-2016
    Location
    Perth Western Australia
    MS-Off Ver
    Office 365
    Posts
    257

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Hi

    Have you contemplated the number of worksheets required ie 1 sheet per employee * 26 pays per year for 3 years, which could be upwards of 200. This could become unwieldy and maybe there is a more efficient way.
    In the meantime I will continue to find a solution for you.

  3. #3
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    There would never be that many. This is to make corrections on a very small scale. The one I'm doing now is the largest I'll probably ever do and it won't be 50 tabs. I can always break into two files if necessary. One tab for each pay period/person.
    I realized last night that there will be some instances where I will only need one line item and not two. I would just need the data with the "earnings account" and wouldn't need the 2nd line item with the "correct earnings account". Is there a way that you can build that part in so that I could block out the instructions for the 2nd line if necessary? I can't tell you how much I appreciate your help with this!!!! Thank you!

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    So I had a go at your problem. To test run macro "ArrData"

    The code is not very sophisticated, lots of copying and pasting. The best thing one can say about it that it works as far as I can tell on your uploaded file.

    So see if it fits your needs. A sheet can hold max 4 set of data for a person that is you can have 4 sets of data with the same date. If you wish for more you need to make a bigger template to hold more data.

    Alf
    Attached Files Attached Files
    Last edited by Alf; 11-04-2017 at 05:04 PM.

  5. #5
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Hi Alf,
    Thank you so much - works great except for 1 thing.... It's putting each line item on it's own page when the name AND pay end date are the same (like rows 17 & 18, 20 & 21, 2 3& 24), they should go onto the same form. We could add a column on the data tab to make a formula to test that if the name and pay end date don't change from the row above then it should go on the same form. Do you think you can add the logic to make that happen? I really appreciate it. Thank you!!!!!

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    It's putting each line item on it's own page when the name AND pay end date are the same (like rows 17 & 18, 20 & 21, 2 3& 24),
    Not sure I understand that problem. Looking at the data in your uploaded file I see

    Row 15 smith, John 67891 1 2017-08-12 PHN
    Row 16 smith, John 67891 1 2017-08-26 PHN
    Row 17 smith, John 67891 1 2017-08-26 Reg

    so as far as I see row 15 should generate a a sheet and since row 16 and 17 contains the same name and date both these rows should be added to the same sheet which my macro does.

    Row 18 Smith, Susie 98765 0 2017-10-21 Reg
    Row 19 Smith, Susie 98765 0 2017-08-31 OVT
    Row 20 Smith, Susie 98765 0 2017-08-31 PHN

    again as I see it you should have two sheets for Smith, Susie. One with the data from 2017-10-21 and the other containing the two sets of data from the 2017-08-31. This is the result the macro produces.

    The only thing that is a bit strange is the format setting on the template sheet cell A5 as this shows date in an unusual format i.e. 07-31-17 (mm-dd-yy) but in the formula field it is shown as 2017-07-31
    (Scandinavian date format) but as macro makes a copy of the template file all settings there are transferred to created sheets.

    As for the logic in sheet adding macro first copies all names from data sheet to an new sheet called "analyze", the list of names are then edited by excluding duplicates.

    This edited list is then used at a criteria (one name at a time) setting an autofilter. The autofiltered result are copied to the "analyze" sheet and then the macro builds a string with the date + "_" + name.

    Macro will then try to activate a sheet with that string name. If sheet exist result are copied to activated sheet and if sheet don't exist macro will add one with that string name and result copied to it.

    I'm sorry I can't help you but as I don't see you problem I don't know what to suggest.

    Why don't you upload a file showing the results after running the macro and adding comments to show where things have gone wrong.

    Alf

  7. #7
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I think I referenced the wrong row numbers in the last message I sent you (I may have been looking at a different version of the file). I've uploaded two filed here. "PCT download report_mod after macro has run" shows you what I got after I executed the macro. As you can see, I only see one line item from the data sheet per sheet generated. I also uploaded a "manual example of what macro should produce". In this workbook, I sorted the data by name, then pay end date, and then inserted two columns (A & B), where B puts the name and date together for a test name, and A decides whether the line item should go on the same sheet as the record before it, or a new sheet. In this care, there would be 12 sheets created, with 3 sets of line items (rows 14 & 15, rows 21 & 22, and 23 & 24) would be on sheets 4, 10 & 11, respectively. I created all the sheets, but didn't worry about having the correct data on the lower part of the sheets.
    Please let me know if this information is helpful.

    As far as the date is concerned... I am in the United States, and the date format I'm using is mm/dd/yyyy. I'm guessing you're using the European format which is dd/mm/yyyy maybe??? Maybe you see my dates in your format? But the format I've intended for everywhere on my worksheets is
    dd/mm/yyyy. Hope that helps.... Thanks again.

  8. #8
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    As far as I've understood your problem you wanted to create separate sheets from the data based on name and pay end date.

    If there was a name with the same date twice then all information this information should be put on the same sheet.

    For instance Doe, Jane has pay end date 31-07-2017 twice and as each line generate two lines in the sheet one for the 12000289-2200-91299 earnings account and one for the
    12000287-3002-90200 correct earnings account there will be four lines of information under Earnings Code and Account.

    This is what the macro is set up to do and as far as I can see this it does. The sheet naming convention is based on the fact that this information is found in the data sheet and if a name and a date pops up father down then it will be placed in the same sheet as previous result if it has the same name as well as the same date.

    Take the example with Doe, Jane date 31-07-2017. It will not matter if you separate them or where you place these two lines in the data sheet. Macro will find them and add both to the same sheet (31-07-2017_Doe, Jane)

    So out of four lines with data for Doe, Jane this will generate 3 sheets in the file
    named 31-07-2017_Doe, Jane. 30-09-2017_Doe, Jane and 31-10-2017_Doe, Jane

    The Scandinavia date format by the way is YYYY-MM-DD but as Excel adapts to the environment it is in this should be no problem.

    Have a last go and try running the macro again and see if this result makes more sense to you and that it is what you was looking for.

    I've changed the format on the template cell A5 so hopefully it will now give you DD-MM-YYY format.

    The other change in the macro is that in starts by clearing all the sheets in the file except "data" and "Template" before rebuilding all the sheets.

    Alf
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I really do appreciate your time in this.... So I've attached the results after I've downloaded your file and run it. I'm guessing it is something to do with the date format, not sure, but when I run your macro, it creates 15 sheets (ending with template (16)), and none of the sheets have more than 2 lines of data.
    Could you write the macro where it decides whether it goes on the same sheet as another by the number in column A? I can add that column to my data and basically assign a sheet number based on the name & date. For some reason, on your end, it works, but on my end, it puts each line on it's own sheet. . Thoughts for my suggestion or another to work around that? Any suggestion/solution is so appreciated! Thank you again for all your
    efforts. Thanks again!
    Last edited by JudyF; 11-06-2017 at 06:00 PM.

  10. #10
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I'm stumped, cheeking the result you get I see that Doe, Jane 31-07-2017 get split on two sheets for you but when I run it in an Scandinavian environment I get the result on one sheet.

    See sheet 2017-07-31_Doe, Jane (Scandinavian date format YYYY-MM-DD)

    I also get smith, John 26-08-2017 and Smith, Sussie 31-08-2017 on one sheet each (2017-08-26_smith, John and 2017-08-31_Smith, Sussie) wheras you get two sheets for these dates.

    By the way how do you get the sheet names Template(2), Template(3) ....?? Do you set these manually after a run?

    If they where set by macro in US date format the should be like 26-08-2017_smith, John and then macro should add those with same name and date but if sheet name is sett as Template(2), Template(3) and so forth when macro is run then the macro will not know that some values belongs to the same sheet. Only when a sheet name repeat itself will that data be added to an existing sheet.

    At the moment I do not have any clue to this behavior. Will check and see if I can change regional setting to US format for date.

    Uploaded file the last file you uploaded run i Scandinavian environment.

    Alf
    Last edited by Alf; 11-06-2017 at 04:31 PM.

  11. #11
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    The sheet names are automatically generated - what I uploaded is exactly what I got after running the macro. Here is what I got when I ran it again. Same problem. I wonder why it acts so differently in the different environments.
    Would you be willing to try the logic based on the sheet # in column A like in the file "manual example of what macro should produce"? Look at the data tab in columns A & B. I can easily add that to my data sheet. See attached file. Thank you!!!!

  12. #12
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Ok I think I solved the problem. The normal format for US date is 8/31/2017 but in sheet name "/" is a forbidden character so the macro makes this Template(2), Template(3) and so forth. Even changing it to 31-08-2017 will not fix this problem as result is still Template(2), Template(3) and so forth.

    What I did was to change datum format in the D column (data sheet) to numbers so 8/31/2017 was transformed to 42978.00 and then the macro did run ok.

    So you got funny sheet name i.e. "42978_Black, John" but if you check cell A5 in that sheet you see the date as 8/31/2017 so you know the proper date.

    Will have a think about it, perhaps if date is changed to 31-08-2017 and defined as text then macro will work.

    So now I'll try to change back my computer to the Scandinavian setting.

    Alf

  13. #13
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Judy pretty please with sugar on !!!!!! Delete you mail address as soon as possible!!!!!

    Never ever ever put you email address on an open forum you may be drowned in spam.

    If you wish to contact me, not recommended as problems should be handled in forum for the benefit of all you may send me a PM through the forum.

    Alf

  14. #14
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I deleted my email from the post above. Thank you for that advise. I'm relatively new to the forums.
    I don't see that you attached a new file after you tried changing the date format. Can you please try again? Thanks!

  15. #15
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Yes I did forget that. Just euphoric after solving this problem so I forgot. But there must be a way to use date as a sheet name. Have tried different settings but most of the time
    these change the visual layout of the date. Looking at the formula field it still shows mm/dd/yyyy even if visual (cell value) shows dd-mm-yyyy

    Alf

  16. #16
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Getting closer!!!!! Looks like it's working properly except for the way the date is acting. What about before the macro creates the tabs another column is added with a copy of the dates in the dd/mm/yyyy format. After the macro runs that column can be copied back over the column showing the dates not as a date. If we can't get the date on the sheet name to present properly can it be left off? Or what about creating a column with the date hard coded as mm.dd.yyyy so there are no "/" ?
    Again, thank you so much!

  17. #17
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Hi Judy

    Let me try to sum it up: No problem modifying macro to set date to numbers at the start.

    The good: Macro works as expected
    The bad: Strange sheet names not easy to identify.

    Then at the end of the run macro sets numbers back to date, still this will not change sheet name.

    As you can see from uploaded image change date setting only changes how it's shown in the cell. In the formula window D11 is shown as 8/31/2017 as opposed to the value 31-Aug-17 shown in cell D11.

    a_us_date.jpg

    I also tried to build a string "31-08-2017" so I tested this formula in a cell
    "=Mid(D11,2)" where starting at position 1 formula should cut two characters from D11.

    I of course expected 31 as the result but I got 42!! Why? Well just because the numerical for of the date 8/31/2017 is 42978 so I got the two first characters of that i.e. 42.

    So to me it seems the date format is like taxes and death, impossible to avoid!!!

    Still I refuse to believe there is no work-around this problem. I'll post a question about this in the forum and see if somebody else has some useful idea.

    By the way my last uploaded file has my last version of the macro and changes are as follows:

    At start macro deletes all sheets except "data" and "Template"

    It also checks number of last used rows in an added sheet. If this is 18 or greater macro adds two more empty rows so information will be placed in proper form by expanding the sheet when needed.

    If you think it's worthwhile I can also write a proper walk-through of this macro.

    Alf
    Last edited by Alf; 11-07-2017 at 02:34 AM.

  18. #18
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Like professor Higgins I feel I can now say "My God I got it!"

    Well the quote is not really like that but it was the best I could think of. File is also tested in an US environment and runs fine. The crux of the matter was the formula I added to the
    macro i.e. =TEXT(cell,"mm-dd-yyyy")

    This will give you date in the format mm-dd-yyyy, and if you wish for another format just change the line
    Please Login or Register  to view this content.
    to the format you wish for as long as there are no forbidden characters in it.

    So just click on the macro "ArrData" and do a rerun to test and you don't need to delete all the result sheets, the macro will do that.

    Alf
    Attached Files Attached Files

  19. #19
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Alf - you are the best! Thank you so so much!!! Is there anywhere I can write a review about you?
    Thanks again and take care.

  20. #20
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Forgot to mention... I think a walkthrough would be great if you could do that.

  21. #21
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Hi Judy

    You are welcome and thanks for feedback

    Is there anywhere I can write a review about you?
    No not really, you can click on the small star in one of my posts (bottom, left) and that will increase my rep points and probably my status among my peers in the forum. But really the best part is to know that I solved a tricky problem and helped somebody who was grateful about it and told me so.

    You to take care too Judy.

    As this has solved your problem could you please mark thread "Solved" as per forum rules. But even if it's marked "Solved" I still have my subscription to this thread so if you have a problem with this macro you could post it here and I would get a mail about any posting in this thread.

    I'll do a walk-through sometime during this week and post it in this thread

    Regards

    Alf

    Ps

    To mark your thread solved do the following:

    -Go to the top of the first post
    -Select Thread Tools
    -Select Mark thread as Solved
    Last edited by Alf; 11-07-2017 at 01:34 PM.

  22. #22
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    I am not seeing where I mark this "solved".
    Thank you again!

  23. #23
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Page 1, top right "Thread Tools", drop-down box. See image

    Judy.jpg

    Alf

  24. #24
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    So here is the promised walk-through of the macro used in your file, hopefully this makes sense to you or you could be more confused but now on a much higher level

    Alf
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    05-30-2017
    Location
    St Louis, MO
    MS-Off Ver
    2013
    Posts
    43

    Re: Need macro to copy data from 1 sheet to multiple sheets based on certain criteria

    Thanks again. Take care.

+ 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. Copy Data from multiple sheets based on criteria in a column and paste to master log.
    By Jsonic5280 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2016, 03:55 PM
  2. Replies: 2
    Last Post: 01-23-2016, 01:16 PM
  3. [SOLVED] Macro to Copy Rows to new sheet based on Multiple Criteria
    By ecubersax in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-07-2015, 01:07 AM
  4. [SOLVED] VBA Code to copy different data from one sheet to multiple sheets based on criteria
    By eharwood in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-08-2014, 02:22 PM
  5. [SOLVED] Need Macro that arranges data from one sheet onto other sheets based on criteria
    By sephus730 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-12-2013, 07:22 PM
  6. Search multiple sheets for value based on criteria and copy data accordingly
    By Zagadka in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-31-2012, 08:46 AM
  7. Copy data from multiple sheets based on certain criteria!!
    By missyk77 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-24-2010, 12:05 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