Putting it all together:
Public Function DaysInMonth(Optional ByVal vDate As Variant, _
Optional ByVal iMonth As Integer, Optional ByVal iYear As Integer) As Integer
If IsDate(vDate) Then
DaysInMonth= Day(DateSerial(Year(vDate), Month(vDate) + 1, 0))
Else
If iYear = 0 Then iYear = Year(Date)
If iMonth = 0 Then iMonth = Month(Date)
DaysInMonth= Day(DateSerial(iYear, iMonth + 1, 0))
End If
End Function
Everything is optional so if nothing is passed the current date will be used.
A date as a string or date variable can be passed, or just the month or just the year can be passed.
You could add error handling to determine if the if anything was passed in the vDate variable, and if so, but it's not a Date, then inform the user. The same with the iMonth if > than 12 or less than -12.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.