Forum Discussion
How to count periods of absence in excel
- Dec 10, 2021
What do you see when you open the sample workbook attached below?
Here is a custom VBA function that you can use:
Function Periods(daterange As Range, coderange As Range, code As String)
Dim i As Long
Dim f As Boolean
For i = 1 To daterange.Count
If Weekday(daterange(i).Value, vbMonday) <= 5 Then
If coderange(i).Value = code Then
If Not f Then
Periods = Periods + 1
f = True
End If
Else
f = False
End If
End If
Next i
End Function
In CC3, enter the formula
=Periods($B$9:$B$42,CC9:CC42,"S")
Adjust the ranges as needed, then fill to the right.
Don't forget to save the workbook as a macro-enabled workbook and to allow macros when you open the workbook.
HansVogelaar Thank you, I'm one step closer now! That doesn't quite get what I'm after though, I need TEST 1 to show as one period and so on as over a weekend should still count as one period. It should only count as more if there is a working day between periods.
- HansVogelaarDec 10, 2021MVP
What do you see when you open the sample workbook attached below?
- Jack_RobertsDec 10, 2021Copper ContributorHey, that's what I am after thank you. I have copied that formula into my sheet and it works as desired now
- Olivier_LanguilleNov 09, 2023Copper Contributor
Hi Hans
Am picking up an old thread you have helped with.
I have used your period function for my calculation but as we are using several reasons for absenteism the outcome is current double counting some periods.
For example i could have an employee being sick for 5 days and this will be recorded as 3 days of sick holiday (SH) meaning that he will use his holiday allocation for 3 days and 2 days as sick (S) which would be unpaid.
So the formula below would need to be able to look at different reason code
=Periods(last_year_dates,OFFSET(last_year_dates,12,0),"S")
Do you have any suggestion on how i could achieve this?
Thankyou very much.
- HansVogelaarNov 09, 2023MVP
Do you mean that the function should count the 3 SH days and 2 S days as one period?