First of all, you need to understand how does VBA "understands" a date.
Excel, Access, VBA and many MS products store dates as a double precision number; the integer part of the number is the date (number of days counted since January 1st, 1900), and the decimal portion of the number is the time (fraction of day).
This makes it easy to compare two dates: You can compare, add or substract dates just as if you were using numbers. So, if t0
and t1
are two dates (t1 >= t0
), then the expression t1 - t0
will give you the difference of days.
Now... how yo count the "business days" between two dates? The format()
function in VBA can help you. You can use this function to return the "day-of-week" number.
Check the online help of the function: http://msdn.microsoft.com/en-us/library/office/gg251755.aspx
So... how do you put this together? Here's an example:
public function bizDaysRemaining(t0 as date, t1 as date) as String
Dim ans As String, dayCount as Integer, n as Integer
If t1 < t0 Then
ans = "Warning"
Else
dayCount = 0
n = 0
While t0 + n <= t1
if format(t0 + n, "w", vbMonday) <= 5 Then dayCount = dayCount + 1
n = n + 1
Wend
If dayCount < 3 Then
ans = "Warning"
Else
ans = "There are " & dayCount & " business days remaining"
End If
End IF
bizDaysRemaining = ans
End Function
Hope this helps you