Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
datediff("M", DateSerial(2011,9,15),DateSerial(2012,3,4))
' Using dateserial makes the date format the same regardless of the users regional settings.
'The above result is 6.
'To get the average hours per month, hours/months.
200/6 = 33.33 (rounded to 2 decimal places)
[blue]Option Explicit
Private Const myMonth As Long = 0
Private Const myWorkingDays As Long = 1
Private Const myCalculatedHours As Long = 2
Private Sub Command1_Click()
Dim myResult As Variant
Dim lp As Long
myResult = HourSpread(200, "15 sep 2011", "4 March 2012")
For lp = LBound(myResult, 2) To UBound(myResult, 2)
Debug.Print myResult(myMonth, lp) & " Working Days: " & myResult(myWorkingDays, lp) & " Hours: " & Round(myResult(myCalculatedHours, lp), 2)
Next
End Sub
Public Function HourSpread(Hours As Single, Startdate As Date, EndDate As Date) As Variant
Dim lpMonth As Long
Dim CurrentMonth As Date
Dim NextMonth As Date
Dim Total As Long
Dim EachMonth() As Variant 'Long
EndDate = DateAdd("d", 1, EndDate) ' depending on our definition of 'between'
CurrentMonth = Startdate
ReDim EachMonth(2, DateDiff("m", Startdate, EndDate)) As Variant 'Long
For lpMonth = LBound(EachMonth, 2) To UBound(EachMonth, 2)
NextMonth = DateSerial(Year(CurrentMonth), Month(CurrentMonth) + 1, 1)
'DateAdd("m", 1, CurrentMonth)
If NextMonth > EndDate Then NextMonth = EndDate
EachMonth(myWorkingDays, lpMonth) = CountWeekdaysBetweenDates(CurrentMonth, NextMonth)
Total = Total + EachMonth(myWorkingDays, lpMonth)
CurrentMonth = NextMonth '+ 1
Next
For lpMonth = LBound(EachMonth, 2) To UBound(EachMonth, 2)
EachMonth(myMonth, lpMonth) = Format((DateAdd("m", lpMonth, Startdate)), "mmm yyyy")
EachMonth(myCalculatedHours, lpMonth) = 200 * EachMonth(myWorkingDays, lpMonth) / Total
Next
HourSpread = EachMonth
End Function
' Frtom my tek-tips example in thread222-361893
' Assumes EndDate>= StartDate
Private Function CountWeekdaysBetweenDates(ByVal Startdate As Date, ByVal EndDate As Date) As Long
If Weekday(Startdate, vbMonday) > 5 Then Startdate = DateAdd("d", 3 - Weekday(Startdate, vbSaturday), Startdate)
If Weekday(EndDate, vbMonday) > 5 Then EndDate = DateAdd("d", -Weekday(EndDate, vbSaturday), EndDate)
CountWeekdaysBetweenDates = DateDiff("d", Startdate, EndDate) - 2 * (DateDiff("ww", Startdate, EndDate))
End Function[/blue][