+ Reply to Thread
Results 1 to 10 of 10

If Date-7 or Date+7 includes dates in another month, then...

  1. #1
    Registered User
    Join Date
    01-13-2011
    Location
    Boston, Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    87

    If Date-7 or Date+7 includes dates in another month, then...

    Sorry, I couldn't think of a better title. I have a spreadsheet with payroll costs by department by week. I've developed a monthly report which requires manual manipulation of some data - specifically, if the payroll week goes between months, only a portion should be allocated to a specific month (e.g. if there's a weekly payroll ending 2/3/2011, you take the payroll cost for the week, divide it by 7 and multiply that number by 4 or 3 for the allocations to January and February, respectively) - but I'd like to know if it's possible to have Excel do all of this work automatically. I'd like to both reduce the potential for error and increase the speed in which these reports can be generated each month.

    I've attached a spreadsheet showing how the data is organized and a demonstration of the problem.

    Thank you.

    Adam
    Attached Files Attached Files
    Last edited by hektisk; 03-15-2011 at 09:53 PM.

  2. #2
    Forum Contributor Charlie_Howell's Avatar
    Join Date
    09-27-2010
    Location
    Memphis, TN
    MS-Off Ver
    Excel 2007
    Posts
    218

    Re: If Date-7 or Date+7 includes dates in another month, then...

    try this method and let me know if it works for you..
    Attached Files Attached Files
    Last edited by Charlie_Howell; 03-13-2011 at 02:12 AM.

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: If Date-7 or Date+7 includes dates in another month, then...

    Here is something for you to test.

    You have to input a date in cell F35.

    Row 27 handles odd numbered months while row 28 handles the even numbered months.
    Attached Files Attached Files

  4. #4
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: If Date-7 or Date+7 includes dates in another month, then...

    If you make F33 into a true date, 1st of the relevant month, as per Cutter's example then you can use this formula for the total

    =SUMPRODUCT(LOOKUP(ROW(INDIRECT(F33&":"&EOMONTH(F33,0)))+6,$4:$4,8:8))/7
    Audere est facere

  5. #5
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: If Date-7 or Date+7 includes dates in another month, then...

    Well that's certainly a much more streamlined option!

    I wish I could grasp how it works but that's just not happening.

  6. #6
    Registered User
    Join Date
    01-13-2011
    Location
    Boston, Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: If Date-7 or Date+7 includes dates in another month, then...

    Charlie: Thank you for your hard work. I apologize for any confusion, but I was looking for something a little different.

    Cutter and daddylonglegs: Thank you! If it's not too much to ask, daddylonglegs, could you share the logic? I'm staring at the formula, and can't figure it out. I know it works, but why?

  7. #7
    Registered User
    Join Date
    01-13-2011
    Location
    Boston, Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: If Date-7 or Date+7 includes dates in another month, then...

    Forget this message...my mistake
    Last edited by hektisk; 03-15-2011 at 08:45 PM.

  8. #8
    Forum Contributor Charlie_Howell's Avatar
    Join Date
    09-27-2010
    Location
    Memphis, TN
    MS-Off Ver
    Excel 2007
    Posts
    218

    Re: If Date-7 or Date+7 includes dates in another month, then...

    i noticed the same

  9. #9
    Registered User
    Join Date
    01-13-2011
    Location
    Boston, Massachusetts
    MS-Off Ver
    Excel 2010
    Posts
    87

    Re: If Date-7 or Date+7 includes dates in another month, then...

    I'd love to know the logic, but even if I can't grasp that, I do have one more question:

    The monthly report template is located in a different worksheet from the data. How do I adjust the formula accordingly? I've tried a couple of things, but haven't been able to figure it out. Thanks.

  10. #10
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: If Date-7 or Date+7 includes dates in another month, then...

    OK, you normally have to start from the inside out, i.e. in this formula

    =SUMPRODUCT(LOOKUP(ROW(INDIRECT(F33&":"&EOMONTH(F33,0)))+6,$4:$4,8:8))/7

    F33 is the start date of the month and EOMONTH(F3,0) generates the end date of the month so this part

    ROW(INDIRECT(F33&":"&EOMONTH(F33,0)))

    is a way to generate an array of dates including every day in the month.

    Now with LOOKUP you can lookup a value in one row and return the corresponding value in another row, e.g.

    =LOOKUP(A1,2:2,3:3)

    In that version the values in row 1 need to be sorted ascending....and then A1 will match with the next lowest (or equal) value in row 2 and return the corresponding value from row 3...so if A1 is 5 and A2:C2 is 1, 4 and 7 then A1 matches with the next lowest value, 4 in B2 and returns the value from B3.

    In your version the dates are in ascending order...but they are week end dates so you need to add 6 to the lookup dates, so now the LOOKUP is like this

    =LOOKUP({array of dates}+6,$4:$4,8:8)

    So, fortunately for me LOOKUP allows you to have an array or range as the LOOKUP value (unlike VLOOKUP, for instance) and the above therefore looks up all the dates (+6) in the date row and returns the values from the value row.

    So when you lookup the first date it's the 7th of Jan (1st +6) so that matches with D4 (next lowest date) and the formula returns D8 value....but the next date (8th Jan) matches with E4 and E8 is returned....in fact E8 is returned 7 times, F8 7 times etc.....so for full weeks you get 7 values and a reduced number for weeks at the start of the month.

    SUMPRODUCT then sums all the value (31 for January) and then we divide by 7, effectively giving you E8 only once but D8/7 etc.....

    If you have the data in data worksheet then you can just change the formula to include the sheet name, i.e.

    =SUMPRODUCT(LOOKUP(ROW(INDIRECT(F33&":"&EOMONTH(F33,0)))+6,Data!$4:$4,Data!8:8))/7

    I'm assuming that F33 will be in the same sheet as the formula......

+ 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