Results 1 to 10 of 29

Thread: Code Required to calculate number of days worked, normal overtime and holiday overtime

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10

    Second Code for Nelson using Admin Formula Idea

    Hi Nelson,
    This is another solution separate to mine form posts 5 6 and 7

    Here is a new Full code: Sub IJAdjustKAddTotalAllWorksheet()
    Post #20
    http://www.excelfox.com/forum/showth...0072#post10072


    The check criteria is now the background color, ( as alternative to Friday as in the first code ) '3d) We need to check Interior color . I got it from this quick code
    Code:
    ' Rem Ref '_-   http://www.excelfox.com/forum/showthread.php/2145-Excel-VBA-Interception-and-Implicit-Intersection-and-VLookUp?p=10062#post10062
    '_-             http://www.excelfox.com/forum/showthread.php/2138-Understanding-VBA-Range-Object-Properties-and-referring-to-ranges-and-spreadsheet-cells?p=10012#post10012
    Sub GetInteriorColor() '   https://msdn.microsoft.com/en-us/library/office/ff840499.aspx             http://www.excelfox.com/forum/showthread.php/2144-Code-Required-to-calculate-number-of-days-worked-normal-overtime-and-holiday-overtime?p=10070#post10070
    Dim ObjColor As Object
     Set ObjColor = Range("E3").Interior: Debug.Print ObjColor.Color ' Looks Yellow is Double 65535 in Immedite window ( Ctrl+g )
     Set ObjColor = Range("E4").Interior: Debug.Print ObjColor.Color ' Looks like no color is Double 16777215
    Dim RngDatesColors As Range ' Select RngDatesColors and hit Shift+F9 to get Range object in watch window. navigate by scrolling and opening with + signs the .Interor Object
     Set RngDatesColors = ThisWorkbook.Worksheets("121").Range("A1").Offset(0, 4).Resize(31, 1) ' .Interior shows only one value for the entire range which seems to be that for a cell with no background color as seen in the screenshot above  all the cells have a colour
    End Sub
    which, for the BEFORE worksheet “121” tells me that the yellow is an interior taken as a Double of 65535
    InteriorColor.JPG http://imgur.com/MavnwLi
    InteriorColor.JPG

    I make an extra array, arrK() to take the string values for the helper column K, to hold “H” for Holiday hrs or “N” for normal hrs


    I no longer take in a dates array to check for Friday. Instead, I take in an Array, arrDteClr(), of the dates interior color. This is a bit inefficient as there is no Interior colors array in the Excel Range object. ( .Interior property for a Range object shows only one value for the entire range which seems to be that as for no color as seen in the screenshot above unless all the cells have a colour, in which case it gives the color for that )
    So I have to take in each value in a new loop, 'InnerLoop for dates background colors ( so I could just as well have checked the worksheet, but I made an array for consistency as I am predominantly using arrays in my code).


    Code section '3e) from the first code is not now used to calculate totals. ( and neither are the variables NOHrsV2 and HOHrsV2 which were used for those totals

    Code section ‘3f) is modified to paste out the formulas from Admin instead of the totals values


    In section ‘3g) there is a line which pastes out that help column




    So in next post is just the BEFORE ( “File1 Before.xlsx” ) data range for the first worksheet “121”.
    (as in Post #6
    http://www.excelfox.com/forum/showth...0068#post10068
    )


    Then in the final 2 couple of post is the Output ( AFTER ) for the new code for one of the Worksheets ( “121” ). The code works on the same Input ( BEFORE ) as in Post #6
    http://www.excelfox.com/forum/showth...0068#post10068
    Last edited by DocAElstein; 02-05-2017 at 11:40 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  2. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10
    BEFORE ( “File1 Before.xlsx” ) data range for the first worksheet “121”.
    (as in Post #6
    http://www.excelfox.com/forum/showth...0068#post10068
    )


    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    1
    SOVEL JOY
    121
    TEAM LEADER
    21.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    2
    SOVEL JOY
    121
    TEAM LEADER
    22.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    3
    SOVEL JOY
    121
    TEAM LEADER
    23.Dec.16
    7:00
    15:00
    8:00
    9:00
    0:00
    4
    SOVEL JOY
    121
    TEAM LEADER
    24.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    5
    SOVEL JOY
    121
    TEAM LEADER
    25.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    6
    SOVEL JOY
    121
    TEAM LEADER
    26.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    7
    SOVEL JOY
    121
    TEAM LEADER
    27.Dec.16
    7:00
    17:00
    10:00
    9:00
    1:00
    8
    SOVEL JOY
    121
    TEAM LEADER
    28.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    9
    SOVEL JOY
    121
    TEAM LEADER
    29.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    10
    SOVEL JOY
    121
    TEAM LEADER
    30.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    11
    SOVEL JOY
    121
    TEAM LEADER
    31.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    12
    SOVEL JOY
    121
    TEAM LEADER
    1.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    13
    SOVEL JOY
    121
    TEAM LEADER
    2.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    14
    SOVEL JOY
    121
    TEAM LEADER
    3.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    15
    SOVEL JOY
    121
    TEAM LEADER
    4.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    16
    SOVEL JOY
    121
    TEAM LEADER
    5.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    17
    SOVEL JOY
    121
    TEAM LEADER
    6.Jan.17
    18
    SOVEL JOY
    121
    TEAM LEADER
    7.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    19
    SOVEL JOY
    121
    TEAM LEADER
    8.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    20
    SOVEL JOY
    121
    TEAM LEADER
    9.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    21
    SOVEL JOY
    121
    TEAM LEADER
    10.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    22
    SOVEL JOY
    121
    TEAM LEADER
    11.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    23
    SOVEL JOY
    121
    TEAM LEADER
    12.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    24
    SOVEL JOY
    121
    TEAM LEADER
    13.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    25
    SOVEL JOY
    121
    TEAM LEADER
    14.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    26
    SOVEL JOY
    121
    TEAM LEADER
    15.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    27
    SOVEL JOY
    121
    TEAM LEADER
    16.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    28
    SOVEL JOY
    121
    TEAM LEADER
    17.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    29
    SOVEL JOY
    121
    TEAM LEADER
    18.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    30
    SOVEL JOY
    121
    TEAM LEADER
    19.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    31
    SOVEL JOY
    121
    TEAM LEADER
    20.Jan.17
    Worksheet: 121
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  3. #3
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10
    Output data range ( AFTER ) for worksheet “121” after running either of the codes
    Sub IJAdjustTotalAllWorksheet()
    Or
    Sub IJAdjustKAddTotalAllWorksheet()




    Using Excel 2007 32 bit
    Row\Col
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    SOVEL JOY
    121
    TEAM LEADER
    21.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    2
    SOVEL JOY
    121
    TEAM LEADER
    22.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    3
    SOVEL JOY
    121
    TEAM LEADER
    23.Dec.16
    7:00
    15:00
    8:00
    8:00
    H
    4
    SOVEL JOY
    121
    TEAM LEADER
    24.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    5
    SOVEL JOY
    121
    TEAM LEADER
    25.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    6
    SOVEL JOY
    121
    TEAM LEADER
    26.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    7
    SOVEL JOY
    121
    TEAM LEADER
    27.Dec.16
    7:00
    17:00
    10:00
    9:00
    1:00
    N
    8
    SOVEL JOY
    121
    TEAM LEADER
    28.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    9
    SOVEL JOY
    121
    TEAM LEADER
    29.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    10
    SOVEL JOY
    121
    TEAM LEADER
    30.Dec.16
    7:00
    18:00
    11:00
    10:00
    H
    11
    SOVEL JOY
    121
    TEAM LEADER
    31.Dec.16
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    12
    SOVEL JOY
    121
    TEAM LEADER
    1.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    13
    SOVEL JOY
    121
    TEAM LEADER
    2.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    14
    SOVEL JOY
    121
    TEAM LEADER
    3.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    15
    SOVEL JOY
    121
    TEAM LEADER
    4.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    16
    SOVEL JOY
    121
    TEAM LEADER
    5.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    17
    SOVEL JOY
    121
    TEAM LEADER
    6.Jan.17
    H
    18
    SOVEL JOY
    121
    TEAM LEADER
    7.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    19
    SOVEL JOY
    121
    TEAM LEADER
    8.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    20
    SOVEL JOY
    121
    TEAM LEADER
    9.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    21
    SOVEL JOY
    121
    TEAM LEADER
    10.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    22
    SOVEL JOY
    121
    TEAM LEADER
    11.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    23
    SOVEL JOY
    121
    TEAM LEADER
    12.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    24
    SOVEL JOY
    121
    TEAM LEADER
    13.Jan.17
    7:00
    18:00
    11:00
    10:00
    H
    25
    SOVEL JOY
    121
    TEAM LEADER
    14.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    N
    26
    SOVEL JOY
    121
    TEAM LEADER
    15.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    N
    27
    SOVEL JOY
    121
    TEAM LEADER
    16.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    N
    28
    SOVEL JOY
    121
    TEAM LEADER
    17.Jan.17
    7:30
    17:30
    10:00
    9:00
    1:00
    N
    29
    SOVEL JOY
    121
    TEAM LEADER
    18.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    30
    SOVEL JOY
    121
    TEAM LEADER
    19.Jan.17
    7:00
    18:00
    11:00
    9:00
    2:00
    N
    31
    SOVEL JOY
    121
    TEAM LEADER
    20.Jan.17
    H
    Worksheet: 121
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  4. #4
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10
    Output values and formulas for output range for Worksheet "121" after running second code
    Sub IJAdjustKAddTotalAllWorksheet()
    ( Second code is here: http://www.excelfox.com/forum/showth...0072#post10072




    Output values as seen in the spreadsheet:Using Excel 2007 32 bit
    Row\Col
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    33
    34
    TOTAL NO. OF DAYS -----> 30
    Normal Overtime ----->
    Holiday Overtime ----->
    35
    47
    28
    36
    37
    For all Month no. of days we take as 30 only
    Worksheet: 121





    Formula string values ( as seen in the Formula Bar ) :Using Excel 2007 32 bit
    Row\Col
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    33
    34
    TOTAL NO. OF DAYS -----> 30
    Normal Overtime ----->
    Holiday Overtime ----->
    35
    =SUMIF(K1:K31,"N",J1:J31)*24
    =SUMIF(K1:K31,"H",J1:J31)*24
    36
    37
    For all Month no. of days we take as 30 only
    Worksheet: 121




    There you go . lots for you to try

    before you tell us you wanted something different !

    वहाँ आप का प्रयास करने के लिए बहुत सारे हैं।

    संभवत: आप कुछ अलग चाहते हो सकता है



    Alan
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  5. #5
    Junior Member
    Join Date
    Jan 2017
    Posts
    9
    Rep Power
    0
    Dear Alan,

    The code seems to be working fine..i need normal OT and Holiday OT to appear exactly in cell G34 and J34
    Can i use the same code for every month and if yes what changes i need to do from my end
    I have around 800 sheets ...will the code work on all sheets in one go

    Thanks,

    Nelson

  6. #6
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10

    Further to the TWO codes you now have from US .......... :)

    Hi Nelson,
    Quote Originally Posted by nelsondavid View Post
    ..i need normal OT and Holiday OT to appear exactly in cell G34 and J34 ...
    I think you can see what part of the code determines where the output goes
    Code:
    '3f) Paste out final Totals and days to current Worksheet
         Let wsStear.Range("G34").Value = NOHrsV2 * 24 'Normal Overtime is held in Array as fraction of a day
         Let wsStear.Range("J34").Value = HOHrsV2 * 24 'Holiday Overtime is held in Array as fraction of a day
         Let wsStear.Range("C34").Value = TDays ' The constant value of Total days is simply added to cell C34
    '3g) Normal   Hrs  ( Column I ) and Overtime Hrs ( Column J ) are  changed
    Code:
    '3f) Paste out final Totals formulas and days to current Worksheet
         Let wsStear.Range("G35").Value = "=SUMIF(K1:K" & lr & ",""N"",J1:J" & lr & ")*24"
         Let wsStear.Range("J35").Value = "=SUMIF(K1:K" & lr & ",""H"",J1:J" & lr & ")*24"
         Let wsStear.Range("C34").Value = TDays ' The constant value of Total days is simply added to cell C34
    '3g) Normal   Hrs  ( Column I ) and Overtime Hrs ( Column J ) are  changed ' And can paste out help column if you like
    I am sure you can understand how to modify to change which cells are used for where the total appear
    _............................................


    Quote Originally Posted by nelsondavid View Post
    .... Can i use the same code for every month ...
    Yes..
    Quote Originally Posted by nelsondavid View Post
    . if yes what changes i need to do from my end..
    ..The code is not in any way dependant on any month information......There is no reason that I can see to need to change anything..
    Quote Originally Posted by nelsondavid View Post
    ....what changes i need to do from my end..
    You should not need to change your end, leave it alone, at least as far as any effect on the Months are concerned
    _................................................. .......


    Quote Originally Posted by nelsondavid View Post
    ...I have around 800 sheets ...will the code work on all sheets in one go...
    The code is designed to work on multiple worksheets.
    Obviously I only tested it with two.
    If I were you, I would test it first with a larger amount, 10, 30, 60, .100 .......etc.. then finally 800 etc.


    _................................................. ...

    In my opinion, it is very important that you try to understand the code...

    Alan.


    P.s.
    Quote Originally Posted by nelsondavid View Post
    ..The code seems to be working fine..
    We have given you 2 codes
    From Alan
    http://www.excelfox.com/forum/showth...age2#post10066

    From Alan and Admin
    http://www.excelfox.com/forum/showth...age2#post10072
    Last edited by DocAElstein; 02-06-2017 at 03:43 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

  7. #7
    Junior Member
    Join Date
    Jan 2017
    Posts
    9
    Rep Power
    0
    Dear Nelson,

    The code working fine , but have two problems 1)It calculates the number of days worked as 30 even if the person absent for one or two days

    2)I use column K for remarks which you are currently using to identify the days

    Sample attached

    Thanks,

    Nelson

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    https://eileenslounge.com/viewtopic.php?p=244184#p244184
    https://eileenslounge.com/viewtopic.php?p=246586#p246586
    https://eileenslounge.com/viewtopic.php?p=246112#p246112
    https://eileenslounge.com/viewtopic.php?p=246112#p246112
    https://eileenslounge.com/viewtopic.php?p=245761#p245761
    https://eileenslounge.com/viewtopic.php?p=245722#p245722
    https://eileenslounge.com/viewtopic.php?p=245616#p245616
    https://eileenslounge.com/viewtopic.php?p=247043#p247043
    https://www.excelfox.com/forum/showthread.php/2307-VBA-Range-Sort-with-arrays-Alternative-for-simple-use
    https://eileenslounge.com/viewtopic.php?p=245238#p245238
    https://eileenslounge.com/viewtopic.php?p=245131#p245131
    https://eileenslounge.com/viewtopic.php?f=18&t=31638
    https://eileenslounge.com/viewtopic.php?p=244579#p244579
    https://eileenslounge.com/viewtopic.php?p=244648#p244648
    https://eileenslounge.com/viewtopic.php?p=244647#p244647
    https://eileenslounge.com/viewtopic.php?p=244577#p244577
    https://eileenslounge.com/viewtopic.php?p=245201#p245201
    https://eileenslounge.com/viewtopic.php?p=243975#p243975
    https://eileenslounge.com/viewtopic.php?p=243884#p243884
    https://eileenslounge.com/viewtopic.php?p=242439#p242439
    https://eileenslounge.com/viewtopic.php?p=243595#p243595
    https://eileenslounge.com/viewtopic.php?p=243589#p243589
    https://eileenslounge.com/viewtopic.php?p=243589#p243589
    https://eileenslounge.com/viewtopic.php?p=243002#p243002
    https://www.eileenslounge.com/viewtopic.php?p=242761#p242761
    https://eileenslounge.com/viewtopic.php?p=242459#p242459
    https://eileenslounge.com/viewtopic.php?p=242054#p242054
    https://eileenslounge.com/viewtopic.php?p=241404#p241404
    https://eileenslounge.com/viewtopic.php?p=229145#p229145
    https://eileenslounge.com/viewtopic.php?p=228710#p228710
    https://eileenslounge.com/viewtopic.php?p=226938#p226938
    https://eileenslounge.com/viewtopic.php?f=18&t=28885
    https://eileenslounge.com/viewtopic.php?p=222689#p222689
    https://eileenslounge.com/viewtopic.php?p=221622#p221622
    https://eileenslounge.com/viewtopic.php?f=27&t=22512
    https://eileenslounge.com/viewtopic.php?f=26&t=26183
    https://eileenslounge.com/viewtopic.php?f=26&t=26030
    https://eileenslounge.com/viewtopic.php?p=202322#p202322
    https://www.excelforum.com/word-formatting-and-general/1174522-finding-a-particular-word-phrase-in-word.html#post4604396
    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Attached Files Attached Files
    Last edited by DocAElstein; 04-16-2024 at 06:43 PM.

Similar Threads

  1. Replies: 33
    Last Post: 01-12-2015, 10:08 AM
  2. Replies: 1
    Last Post: 07-23-2013, 10:43 PM
  3. Replies: 0
    Last Post: 09-17-2012, 02:05 PM
  4. The Number of Years, Months and Days Between Two Dates
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 7
    Last Post: 06-08-2012, 10:35 PM
  5. Number of Days In A Month
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 05-14-2011, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •