Be the first user to complete this post

  • 0
Add to List

VBA-Excel: Date-Time Functions – Month(), Year() and MonthName()

Month()

Description:

The Month function takes Date as a parameter and returns a number between 1 and 12, that is the month of the date provided.

Format:

Month(strDate)

Arguments:
  • strDate
    • Manda­tory
    • Type: Date
    • Date, whose Month need to be calculated.

Example:

Function FnMonth()

  Dim strDate

  strDate = "15-July-2013"    

  MsgBox "Month of the " & strDate & " is -> " & Month(strDate)    

End Function
Month()
Month()

_____________________________________________________________________________________

MonthName()

Description:

The MonthName function takes numeric value as a parameter and returns a Month Name.

Format:

MonthName(intMonth[,blnAbbreviate])

Arguments:
  • intMonth
    • Manda­tory
    • Type: Numeric
    • Value from 1 to 12 , whose Month Name need to be calculated. Like MonthName(3) will return “March”
  • blnAbbreviate
    • Optional
    • Type: Boolean
    • True value will provide the month name abbreviated,  for example “July” will be abbreviated to “Jul”. default value is False

Example:

Function FnMonthName()

    Dim strDate

   Dim strResult

    strDate = "15-07-2013"    

   strResult = "Full Month Name of the " & strDate & " is -> " & MonthName(Month(strDate)) & vbCrLf

   strResult = strResult & "Abbriviated Month Name of the " & strDate & " is -> " & MonthName(Month(strDate), True) 

   MsgBox strResult

End Function

 
The MonthName function takes numeric value as a parameter and returns a Month Name.
MonthName()

_____________________________________________________________________________________________

Year()

Description:

The Year function takes Date as a parameter and returns a number represents the year.

Format:

Year(strDate)

Arguments:
  • strDate
    • Manda­tory
    • Type: Date
    • Date, whose Year need to be calculated.

Example:

Function FnYear()

  Dim strDate

  strDate = "15-July-2013"    

  MsgBox "Year of the " & strDate & " is -> " & Year(strDate)    

End Function
The Year function takes Date as a parameter and returns a number represents the year.
Year()



Also Read:

  1. VBA-Excel: String Functions – LTrim()
  2. VBA-Excel: Date-Time Functions – WeekDay() and WeekDayName()
  3. VBA-Excel: Date-Time Functions – FormatDateTime()
  4. VBA-Excel: Date-Time Functions – Day()
  5. VBA-Excel: CurrentRegion