+ Reply to Thread
Results 1 to 18 of 18

Sum time spent based on conditions

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Sum time spent based on conditions

    Hello everyone
    I have two sheets : Sheet1 contains data and in sheet2 I need to calculate the time spent in column H based on conditions

    Sheet2:
    The Code field in Column A is the first condition
    The second condition is the date to be between two dates in range("A1") and range("E1")
    In Columns B:G if the cell is not empty then the condition would be the string n range("B4:G4")
    ---------------------
    I will attach the expected results for the first example:
    In sheet1 I will put the word "OK" in column L to illustrate the rows that applied all the conditions ...
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Can it be done by formulas or I have to use vba?

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    Hi,

    Although this can be done using formula (SUMIFS() and COUNTIFS()), here is the vba way :

    Please Login or Register  to view this content.

    Regards

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Thank you very much for this code
    I will test it now and I'm sure it will be great of course ..
    But if it can be solved by formulas I'm eager to know how ..?
    Thanks for help me all the time

  5. #5
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Sum time spent based on conditions

    Hi YasserKhalil,

    formula may not be my forte but did an attempt. It's an array formula that and quite long (confirm by Ctrl+Shift+Enter). Each sum formula is for each sector separately - have tried to make it shorter but failed so far.

    In H5:
    =SUM(IF(B5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=B$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&B5))),0))+SUM(IF(C5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=C$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&C5))),0))+SUM(IF(D5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=D$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&D5))),0))+SUM(IF(E5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=E$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&E5))),0))+SUM(IF(F5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=F$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&F5))),0))+SUM(IF(G5>0,LARGE((Sheet1!$C$5:$C$12=$A5)*(Sheet1!$B$5:$B$12>=$A$2)*(Sheet1!$B$5:$B$12<=$E$2)*(Sheet1!$G$5:$G$12=G$4)*(Sheet1!$J$5:$J$12),ROW(INDIRECT("1:"&G5))),0))

    where, for example, the highlighted part sums for the sector AR seperately given all the criterias you mentioned.

    See attached.

    Best,
    berlan
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Mr. Karedog
    I tested the code .. after trying to run it again I have noticed that it clears the contents of the range B5:G10
    See the attachment
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Mr. Berlan
    Thank you very much for this formula
    But it is too long ..
    Is there a way to devise UDF for this mission
    I imagine : UDFName(startdate,enddate,code,sectorrange(B4:G4))
    Generally thanks a lot for the time spent in this successful formula

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    You are welcome, and thank you for the reps.

    I have amend the code, to make the cells with contents like numeric 1862 is the same as string 1862 :

    Please Login or Register  to view this content.

    This is my version using Excel formulas :
    Formula on B5 :
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and to be copied to B5:G10



    Formula on H5 :
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    and to be copied to H5:H10



    Quote Originally Posted by YasserKhalil View Post
    Mr. Karedog
    I tested the code .. after trying to run it again I have noticed that it clears the contents of the range B5:G10
    See the attachment
    Yes, the cells must be cleared first, or there will be non zero initial values for arrOut

    Quote Originally Posted by YasserKhalil View Post
    Is there a way to devise UDF for this mission
    Since the results is span across multicell, it means that the UDF is returning array, which is known to be slow.


    Regards

  9. #9
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Mr. Karedog
    I thought formulas will be difficult .. I tried both formulas and the code and the results were amazing
    Thank you very much for your fantastic solutions
    Thanks to Mr. Berlan who contributed a great and helpful formula too
    Now it is solved

  10. #10
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    You are welcome Yasser, thanks to you too.


    Regards

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Mr. Karedog
    I know that I marked the thread as solved ..
    But it seems that I will have to use the array formula of Mr. Berlan .. It is the right way to achieve the task ..
    The problem is that the required from me now is not to calculate the range("B5:G10") just the required range("H5:H10")

    And the range("B4:G4") is fixed in calculation method as Berlan's sloution .. and sometimes the data in that range may be empty
    So I was talking about the UDF function in post 7 ?
    Sorry for disturbing you again

  12. #12
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    This is the UDF code :

    Please Login or Register  to view this content.
    To use this UDF, highlight Sheet2 H5:H10, press F2 and put this formula (and ended with pressing Ctrl Shift Enter --> array UDF) :
    Formula: copy to clipboard
    Please Login or Register  to view this content.




    Regards

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Thank you very much mr. Karedog
    I tried clearing the contents of range("C4:G5") so there is only one sector and the value 1 related to it
    Pressed Ctrl + Alt + F9 .. the same result 9:15 .. the result should be related to the range ("B4:G4") and if any is empty it is ignored completely from calculations
    The result after clearing range("C4:G5") should be 3:00
    I hope it is clear now
    and sorry again for disturbing you

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    Yasser, please look at attached file.

    From the original data (not changed yet), the criteria is 25/06/2015 <= date <= 30/06/2015,
    for Code = 1862, the matched items from sheet1 are :

    PHP Code: 
    Row 5     30/06/2015     1862     3:15
    Row 7     30
    /06/2015     1862     3:00
    Row 8     30
    /06/2015     1862     2:00
    Row 9     25
    /06/2015     1862     2:00
    Row 12    28
    /06/2015     1862     1:00
                                
    ------------
                            
    Total    11:15 
    and the result from UDF in sheet2 cell H5 is also 11:15, so they are matched.


    Now you try to clear the contents of Sheet1 range("C4:G5"), so according to above list, only Row 5 (3:15) is removed, so the total should be 11:15 - 3:15 = 8:00
    This also reflected in sheet2 cell H5.

    I don't understand what you mean.
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    Mr. Karedog
    I'm sorry for not clarifying well
    In sheet2:
    ----------
    Range("B4:G5") which has the headers of sectors , this range may be cleared .. I need to refresh the results if cleared for example range("C4:G4") and calculate in this case B4 only
    So in this case the result should be 3:00
    Hope it is clear now

  16. #16
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    Ok, I get it now, so you want add a new criteria based on Sector field.

    This is the UDF :

    Please Login or Register  to view this content.
    Usage :
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  17. #17
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Sum time spent based on conditions

    I can't believe my eyes
    It worked like charm. Really brilliant and awesome
    Thank you very much for this great and continuous help

  18. #18
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Sum time spent based on conditions

    You are welcome, glad I can help.


    Regards

+ 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. Replies: 3
    Last Post: 12-19-2013, 06:49 AM
  2. Time Tracker - I need a macro that calculates the time spent on an activity
    By amark in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-01-2011, 09:55 AM
  3. % Of Time Spent Over A Week...
    By iAaron in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 10-21-2007, 02:45 PM
  4. How can I calculate amount of time left based on amount spent?
    By KLD in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-23-2006, 11:25 AM
  5. [SOLVED] How can I show time spent?
    By L in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-08-2006, 10:13 AM

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