Wednesday, January 22, 2014

VBA - Find the number of days in a Month and Return the number of a Month

Two simple functions for finding the number of days in a given month name and the numerical order that month comes in:


Function ReturnMonthNumber(ourMonth) As Integer
    ReturnMonthNumber = Month(DateValue("01 " & ourMonth & " 2000"))
End Function

Function NB_DAYS(ourMonth)
    NB_DAYS = Day(DateSerial(Year(DateValue("01 " & ourMonth & " 2000")), Month(DateValue("01 " & ourMonth & " 2000")) + 1, 1) - 1)
End Function

Sub TestFunctions()
    MsgBox ("The number of days in May is: " & NB_DAYS("May"))
    MsgBox ("April is the " & ReturnMonthNumber("April") & " month")
End Sub

No comments:

Post a Comment