Contractual Weeks Calculation

August

Active Member
Joined
Jun 18, 2004
Messages
284
Office Version
  1. 365
Platform
  1. Windows
In the attached a contractual week starts at the first work day marked x and ends 7days later. The next contractual week starts on the next work day marked x and end 7 days later. The final contractual week can have less than 7 days. The No of contractual weeks for Harry would therefore be 3.
I have tried many variants of sumproducts/countifs/mod but cannot get a formula that works.
Does anyone have an idea how I could achieve the corrrect result in a formula

Many thanks
 

Attachments

  • ContWeeks.png
    ContWeeks.png
    24.8 KB · Views: 24
The issue is theres no real pattern to automate it with.
He was basing his formula off of the contractual weeks 44-47 on row 2, and as you can see Martin has an x under each week # 44, 45, 46, and 47. But now we learn that isn't what its based on.
You could try just always counting 4 x's in a row no matter if it has a space or 2 in-between (weekends etc) but that also isn't always constant because sometimes there is only 1 to 3 x's in a contractual week, like with Arthur or Martin.
So without some sort of constant or pattern this will never be fully automated.

What you could do is use =COUNTIF(C4:Z4,"x") (in col C) and also (in col B) =IF(COUNTIFS(C4:Z4,"x")=0,0,INT((MAX(IF($C4:$Z4="x",COLUMN($C4:$Z4)))-MIN(IF($C4:$Z4="x",COLUMN($C4:$Z4))))/7)+1)
That way you get actual # of days they worked and approx. # of weeks they worked based on the week # and get at least a good feel for it then, but you would always need to confirm since there is no perfect rule set for it.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Thank you all for taking the time to reply
You are correct Martin should be 3 and contractual weeks start on the first day of work (x) not Monday.
I'll try the solution from myall_blues over the weekend and see how I get on

Again many many thanks for your time
Which version of Excel are you using?
 
Upvote 0
Here is a user-defined function (UDF) for you to try.
Usage example: =CWeeks(B5:Y5)
VBA Code:
Function CWeeks(rng As Range) As Long
    Application.Volatile
    Dim s As String, cll As Range
    s = "": CWeeks = 0
    For Each cll In rng.Resize(1)
        s = s & IIf(cll = "x", "x", " ")
    Next cll
    s = LTrim(s)
    Do While Len(s) > 0
        CWeeks = CWeeks + 1
        s = LTrim(Mid(s, 8))
    Loop
End Function
 
Upvote 0
Here is a user-defined function (UDF) for you to try.
Usage example: =CWeeks(B5:Y5)
VBA Code:
Function CWeeks(rng As Range) As Long
    Application.Volatile
    Dim s As String, cll As Range
    s = "": CWeeks = 0
    For Each cll In rng.Resize(1)
        s = s & IIf(cll = "x", "x", " ")
    Next cll
    s = LTrim(s)
    Do While Len(s) > 0
        CWeeks = CWeeks + 1
        s = LTrim(Mid(s, 8))
    Loop
End Function
does that work in 2016? Wasn't working for me when i tested it.
 
Upvote 0
true, forgot he just wanted week counts and not just a count of days worked per person.
I've updated my profile and attached is the sheet with desired results under Weeks Worked
 

Attachments

  • ContWeeks.png
    ContWeeks.png
    29.8 KB · Views: 5
Upvote 0
I've updated my profile and attached is the sheet with desired results under Weeks Worked
Have you tried the code provided by @Tetra201? It works well and I'm not sure a formula solution is possible for your problem.

Book1
ABC
128/10/2024
2Contractual44
3weeksMon
4Harry3
5John0
6Mary0
7Arthur2
8Wilson0
9Martin3
10George0
11Ivy0
12Helen1
13Edward2x
14Mabel1x
15Monty3
16August3
Sheet1
Cell Formulas
RangeFormula
C2C2=ISOWEEKNUM(C1)
C3C3=TEXT(C1,"ddd")
B4:B16B4=cweeks(C4:Z4)
 
Upvote 0
The code works perfectly thank you Bond00. !!!
 
Upvote 0
One question.
On the actual document the "x" will be either "SW", "W" or "SWF" (representing Start Work, Work or Start Work Finish)
Can the x be substituted in the code with the above.
I've tried myself without any success

Thanks
 
Upvote 0
Correction could also be "WF (Work Finish)
Entered the below from Copilot but this gives all zeros.

Function CWeeks(rng As Range) As Long
Application.Volatile
Dim s As String, cll As Range
s = "": CWeeks = 0
For Each cll In rng
If cll.Value = "SW" Or cll.Value = "W" Or cll.Value = "SWF" Or cll.Value = "WF" Then
s = s & "x"
Else
s = s & " "
End If
Next cll
s = Trim(s)
If Len(Replace(s, " ", "")) = 0 Then
CWeeks = 0
Else
Do While Len(s) > 0
If Left(s, 1) = "x" Then
CWeeks = CWeeks + 1
s = Mid(s, 8)
Else
s = Mid(s, 2)
End If
Loop
End If
End Function
The code works perfectly thank you Bond00. !!!
Apologies Tetra201 it was your code that did the trick
 
Upvote 0
Here is a user-defined function (UDF) for you to try.
Usage example: =CWeeks(B5:Y5)
VBA Code:
Function CWeeks(rng As Range) As Long
    Application.Volatile
    Dim s As String, cll As Range
    s = "": CWeeks = 0
    For Each cll In rng.Resize(1)
        s = s & IIf(cll = "x", "x", " ")
    Next cll
    s = LTrim(s)
    Do While Len(s) > 0
        CWeeks = CWeeks + 1
        s = LTrim(Mid(s, 8))
    Loop
End Function
Thank you works perfectly with "x"
Is there a way to use "SW", "W", "WF" and "SWF" in the code as these are the markers to denote days worked.

Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top