Hi -
This is just a challenge -- I enjoy them and suspect other folks do too. I've gone brain-dead trying to resolve it and hope that someone can see through the problem and provide an accurate answer.
Problem: Want to determine the number of business days (Mon - Fri) in an input month (mm/yyyy) without resorting to a loop to count the odd days (left after full weeks have been determined). In other words, I'm trying to define a one-liner that will correctly return the number of non-weekend days left over after the full weeks have been determined.
Here's the code up till now. It works in the majority of cases, but bombs about 2 times out of 12. The fatal line is:
If you enjoy playing around with a puzzle, I'd sure appreciate your input.
Best wishes,
Bob
This is just a challenge -- I enjoy them and suspect other folks do too. I've gone brain-dead trying to resolve it and hope that someone can see through the problem and provide an accurate answer.
Problem: Want to determine the number of business days (Mon - Fri) in an input month (mm/yyyy) without resorting to a loop to count the odd days (left after full weeks have been determined). In other words, I'm trying to define a one-liner that will correctly return the number of non-weekend days left over after the full weeks have been determined.
Here's the code up till now. It works in the majority of cases, but bombs about 2 times out of 12. The fatal line is:
Code:
OddDays = OddDays - Choose(WeekDay(StartDate), 1, 0, 0, 0, 0, 0, 2)
Code:
Function fMonthBusDay(pmmyyyy As String) As Integer
'Input: ? fMonthBusDay("10/2006")
'Output: 22
Dim StartDate As Date
Dim EndDate As Date
Dim FullWeek As Integer
Dim OddDays As Integer
EndDate = DateAdd("m", 1, DateValue(pmmyyyy)) - 1
StartDate = DateValue(pmmyyyy)
FullWeek = Int((EndDate - StartDate + 1) / 7) * 5
OddDays = (EndDate - StartDate + 1) Mod 7
'the following line works in most -- but not all -- cases
OddDays = OddDays - Choose(WeekDay(StartDate), 1, 0, 0, 0, 0, 0, 2)
fMonthBusDay = FullWeek + OddDays
End Function
If you enjoy playing around with a puzzle, I'd sure appreciate your input.
Best wishes,
Bob