Excel VBA Month Function
The Mont function in VBA returns the month number for the specified date.
Syntax
Month (Date)
Parameter
Time (required) – This parameter represents the date.
Return
This function returns the month number for the specified date.
Example 1
Sub MonthFunction_Example1() ' Extracting the month number from the date "01/19/2020" Const Date_Val = "01/19/2020" Dim month_val As Integer month_val = Month(Date_Val) ' The variable month_val will return 01. Cells(1, 1).Value = month_val End Sub
Output
1
Example 2
Sub MonthFunction_Example2() ' Extracting the month number from the current date Dim Date_Val As Date Date_Val = Date Dim month_val As Integer month_val = Month(Date_Val) ' The variable month_val will return 02. Cells(1, 1).Value = month_val End Sub
Output
02
Example 3
Sub MonthFunction_Example3() ' Extracting the month number from the current date Dim Date_Val Date_Val = "Hello VBA" Dim month_val As Integer month_val = Month(Date_Val) ' The variable month_val will return type mis-match run time error. Cells(1, 1).Value = month_val End Sub
Output