patternMinor
Sum hours worked depending on day type
Viewed 0 times
workedhourstypesumdependingday
Problem
A while ago I posted this question about finding the Date in Range, where I explained I have a spreadsheet to control my shift hours and was interesting in improving one of my functions.
I got really nice feedbacks from the community and the improvements provided really helped me out, but now I'm interesting in another function of mine (the caller of that previous function), so here is how this function works:
The function is intended to Sum the amount of hours worked from my spreadsheet, depending on the
There are probably more ways to improve my function, so feel free to suggest any other improvements, but my main concern right now is about my
Here is my current code:
```
Public Function SumHoursByDayType(ByVal firstCell As Range, _
ByVal dayType As String, _
Optional ByVal monthInterval As Long = 3) As Double
With ThisWorkbook.Worksheets(1)
Dim lastRow As Long
lastRow = .Cells(.Rows.count, firstCell.Co
I got really nice feedbacks from the community and the improvements provided really helped me out, but now I'm interesting in another function of mine (the caller of that previous function), so here is how this function works:
The function is intended to Sum the amount of hours worked from my spreadsheet, depending on the
dayType, which can be either a weekend or a weekday. It also checks whether or not the day is a holiday (by calling the function from the aforementioned post) and finally it only considers days that are inside my monthInterval, which means I can determine if I want to get the total of hours worked from the past month, the past 2 month and so forth (defaulted to 3 months). There are probably more ways to improve my function, so feel free to suggest any other improvements, but my main concern right now is about my
If logic. I'm checking too many conditions in the same statement, but I couldn't think of a better way to do it. The logic is:- If the day is a Weekend and I'm looking for
weekenddays, I should add it to total; or
- If the day is a holiday and I'm looking for
weekenddays, I should also sum it (so I'm considering both holidays and weekends in the same result, because they don't enter in the Bank of hours); or
- If the day is a regular day and I'm looking for
weekdaydays, I should sum it; and
- I should only sum it if the day is inside the allowed range determined by
monthInterval.
Here is my current code:
```
Public Function SumHoursByDayType(ByVal firstCell As Range, _
ByVal dayType As String, _
Optional ByVal monthInterval As Long = 3) As Double
With ThisWorkbook.Worksheets(1)
Dim lastRow As Long
lastRow = .Cells(.Rows.count, firstCell.Co
Solution
Don't use VBA when an Excel formula can do the job. Look into the Workday function. It takes in a date and an optional array of holidays and returns a number indicating the day of the week. I have a feeling with just a bit of effort, it can completely replace your VBA code.
Context
StackExchange Code Review Q#153665, answer score: 3
Revisions (0)
No revisions yet.