+ Reply to Thread
Results 1 to 17 of 17

Copy data from one worksheet to another based on month/year

  1. #1
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Copy data from one worksheet to another based on month/year

    Hello,

    I have a sheet named 'caseload' where data is entered into columns A,B,C and D. Column D is the date the booking was completed. This data will be entered by different people as they complete the 'booking'

    I have further worksheets by Month/Year where further information is added and calculations undertaken.
    I currently just link the cells in caseload to January_2020.

    As each row of cells B,C,D and E of 'caseload' are completed, I want to copy the information in B, C and D to the relevant sheet based on month.

    So in my document, data in caseload B1/2/5, C1/2/5 and D1/2/5 would copy to January_2020 B1/2/3, C1/2/3 and D1/2/3 respectively as the booking dates (E1,2,5) took place in January 2020 and data in caseload B3/4, C3/4 and D3/4 would copy to February_2020 B1/2, C1/2 and D1/2 respectively as those booking dates (E3/4) took place in February 2020.

    I have tried LOOKUP and VLOOKUP with MONTH and DAY but failed and I don't have FILTER on my Mac Excel 2019...

    thank you.
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by andynw1; 07-29-2020 at 03:11 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,901

    Re: Copy data from one worksheet to another based on month/year

    Remove any merged cells from your workbook and then apply this VBA code.

    Please Login or Register  to view this content.
    How to install your new code
    • Copy the Excel VBA code
    • Select the workbook in which you want to store the Excel VBA code
    • Press Alt+F11 to open the Visual Basic Editor
    • Choose Insert > Module
    • Edit > Paste the macro into the module that appeared
    • Close the VBEditor
    • Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


    To run the Excel VBA code:
    • Press Alt-F8 to open the macro list
    • Select a macro in the list
    • Click the Run button

    Read this link about merged cells. --> https://www.quora.com/Why-are-people...merge-in-excel

    https://contexturesblog.com/archives...merging-cells/

    https://www.excelarticles.com/videos...cast_1558.html
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Hi,

    Thank you for taking the time to look at this for me.
    I have followed the instructions and the code runs and says complete yet nothing appears in Jan or Feb. Nothing appears to happen at all.
    I have reattached the macro-enabled workbook with the code added.

    Andrew
    Attached Files Attached Files

  4. #4
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    You could do this with a few formulae - are you interested in a formula-based solution?

    Pete

  5. #5
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Hi Pete,

    I would try any solution so please let me know what you have in mind.
    Never used VBA before however Alan's solution wasn't too hard but I couldn't get it to work.
    I have tried VLOOKUP etc but no joy. I am completely stumped.

    thank you.

  6. #6
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    Okay, first of all, put this formula in E12 of the January sheet:

    =MID(CELL("Filename",A1),FIND("]",CELL("Filename",A1))+1,255)

    and then you can copy this formula into E12 of the February sheet. The formula will give you the sheet name, and so if you copy the Feb sheet and rename it as March_2020 that will be shown automatically, so it will make it easier for you to set up the other months.

    I've used a helper column in the Caseload sheet, by inserting a new column A and putting this formula in A12:

    =IF(F12="","-",TEXT(F12,"mmmyy")&"_"&COUNTIFS(F$12:F12,">="&EOMONTH(F12,-1),F$12:F12,"<="&EOMONTH(F12,0)))

    You can copy this down by double-clicking the fill handle (the small black square in the bottom right corner of the cursor). This formula will set up a unique code for each record, based on the booking date in column F. You can hide this column if you want the sheet to look the same as before.

    Then in the January sheet you can use this formula in cell B18:

    =IFERROR(VLOOKUP(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A:$E,COLUMNS($A:C),0),"")

    which can be copied across into C18 and D18. You might need to format D18 as a date if it shows just a number, and then you can copy those 3 formulae down as far as you need them. You can also copy them into the February sheet, and you should see that the January sheet contains 3 records and the Feb sheet shows 2 records, as expected.

    You can use the February sheet as a template to be copied for the other months - just Ctrl-drag the sheet tab along the bottom of the file window, then rename it as appropriate.

    Hope this helps.

    Pete
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Hi Pete,

    Thank you for that, it worked like a charm! I really appreciate your time helping me to get this done.

    Andrew

  8. #8
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Hi Pete,

    Sorry to bother you again, your fix has worked great but I have now created myself another problem...Previously as I had all the women on caseload and all of them also pulling through to the one month, I had set up formulas to count cells depending on the contents with names (or lack of them) in a row of cells cells. Now the women are on different monthly sheets depending when they were booked so my formulas are useless. Actually, they work (sort of but not safely) for aug_2020 and in Sept_20 I can't specify the location or identifier anymore which I presume I would also need for any month. I have been looking to use an IF with the result of your formula in column A of caseload to identify which row to use in my original formulas but I can't get it to work.

    On the monthly sheets, I am capturing in column H, the total number of contacts by adding up cells K:X on caseload where there is a name in the cell ignoring blanks. In column I, capturing the number of times the lead (caseload column D/ month column C) has her name in cells K:X on caseload. Finally in column J, capturing the number of times anyone of the other 7 (not the lead) have their names in cells K:X on caseload again ignoring blanks.

    I have uploaded the current file...thank you
    Attached Files Attached Files

  9. #9
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    Hi Andrew,

    I've only just seen this, but as it is getting a bit late (and football is on TV later on), I won't be able to look at it tonight.

    I'll take a look tomorrow.

    Hope this helps.

    Pete

    P.S. Thanks for the rep, by the way.

  10. #10
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14
    Quote Originally Posted by Pete_UK View Post
    Hi Andrew,

    I've only just seen this, but as it is getting a bit late (and football is on TV later on), I won't be able to look at it tonight.

    I'll take a look tomorrow.

    Hope this helps.

    Pete
    There is no urgency whatsoever. Enjoy the football and whenever you get a chance to look that would be great!

    Thanks, Andrew

  11. #11
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2506 Win 11
    Posts
    24,901

    Re: Copy data from one worksheet to another based on month/year

    My code works, but you have values in cells through row 192. Delete the formulas in Column B of the target sheets. Your formulas in that column is causing the code to look at the last row with data which is 192. Scroll down to see the results.

  12. #12
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    Hello again Andrew.

    Instead of using a fixed range, (e.g. K12:X12, which is then copied down), you need to determine which row in the Caseload sheet applies to the row in the monthly sheets. You can do that using this expression:

    ... INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0) ...

    and you will see this expression in many of the formulae that follow (I'll colour it red, for clarity). Also, you can use COUNTA to count the number of non-blank cells, so the formula in H18 of the monthly sheets will become:

    =IF(B18="","",IF(COUNTA(INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0))=0,"",COUNTA(INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0))))

    No need to enter this as an array formula with CSE.

    The formula in I18 will be:

    =IF(C18="","",COUNTIF(INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0),C18&""))

    and if understand you correctly, the formula in J18 is just the difference between these two, so it can be simplified as:

    =IF(C18="","",H18-I18)

    Copy these down as required.

    Hope this helps.

    Pete

  13. #13
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    Further to this, you can simplify the formula in H18 of the monthly sheets to this:

    =IF(B18="","",IFERROR(1/(1/COUNTA(INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0))),""))

    then copy it down.

    Hope this helps.

    Pete

  14. #14
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Thank you, I'll have a play with this now and see what happens!
    Last edited by andynw1; 07-29-2020 at 02:54 AM.

  15. #15
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Quote Originally Posted by Pete_UK View Post
    Further to this, you can simplify the formula in H18 of the monthly sheets to this:

    =IF(B18="","",IFERROR(1/(1/COUNTA(INDEX(Caseload!$K$12:$X$186,MATCH(LEFT($E$12,3)&RIGHT($E$12,2)&"_"&ROWS($1:1),Caseload!$A$12:$A$186,0),0))),""))

    then copy it down.

    Hope this helps.

    Pete
    Hi Pete,

    Thank you for all your help and the time spent looking at this and replying! The formulas work exactly as expected and I have learnt such a great deal from your input.

    Andrew

  16. #16
    Registered User
    Join Date
    07-05-2020
    Location
    Manchester
    MS-Off Ver
    Mac 2019
    Posts
    14

    Re: Copy data from one worksheet to another based on month/year

    Quote Originally Posted by alansidman View Post
    My code works, but you have values in cells through row 192. Delete the formulas in Column B of the target sheets. Your formulas in that column is causing the code to look at the last row with data which is 192. Scroll down to see the results.
    Ah yes I see it now. I have learnt a lot looking at, and trying to understand your code. Thank you for your help and input, much appreciated!

    Andrew

  17. #17
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,463

    Re: Copy data from one worksheet to another based on month/year

    Quote Originally Posted by andynw1 View Post
    Hi Pete,

    Thank you for all your help and the time spent looking at this and replying! The formulas work exactly as expected and I have learnt such a great deal from your input.

    Andrew
    Glad to help, Andrew. Thanks for marking the thread as Solved.

    Pete

+ 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] Sum Data Based on a GL Code and Month in Year
    By tommpalmer123 in forum Excel Formulas & Functions
    Replies: 31
    Last Post: 04-28-2020, 04:33 AM
  2. Macro to copy rows and populate month/year based on start date?
    By ekwacillin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-26-2016, 03:35 PM
  3. Month and Year in footer based on cell data
    By ERI GURU in forum Excel General
    Replies: 1
    Last Post: 03-06-2015, 02:10 PM
  4. Extract data based on month/year to a simple table.
    By maax555 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-15-2014, 09:55 AM
  5. [SOLVED] Macro to copy data from source workbook to target, based on current month of the year
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 04-27-2014, 05:41 PM
  6. [SOLVED] Copying data based on current month of the year
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2014, 06:33 PM
  7. Get Data based on Month and Year
    By karstens in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-01-2006, 07:50 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