VBA MONTH Function in Excel is a built-in function in MS Excel. It has one input argument or parameter. It returns the two-digit Month from the specified date. It returns a number from 1 to 12. It is a ‘Data and Month’ type function. Default format of the Month function is ‘MM’.
The VBA Month Function can be used in either procedure or function in a VBA editor window in Excel. We can use the VBA Month Function any number of Months in any number of procedures or functions. In the following section we learn what is the syntax and parameters of the date function, where we can use this VBA Month Function and real-Month examples.
- Syntax of VBA Month Function
- Parameters or Arguments
- Where we can apply or use the VBA Month Function?
- Example 1: Display current month on the screen
- Example 2: Display current month on the Worksheet
- Example 3: Display complete current Month name using Format
- Download File
- Instructions to Run VBA Macro Code
- Other Useful Resources
The syntax of the VBA Month Function is
There is one parameter or argument for the Month function in VBA. That is nothing but ‘date_value’.
Where date_value is a date for which the Month is to be returned.
We can use this VBA month function in MS Office 365, MS Excel 2016, MS Excel 2013, 2011, Excel 2010, Excel 2007, Excel 2003, Excel 2016 for Mac, Excel 2011 for Mac, Excel Online, Excel for iPhone, Excel for iPad, Excel for Android tablets and Excel for Android Mobiles.
Here is a simple example of the VBA Month Function. This below example macro uses the Month function and displays the two-digit Month from the specified date. In this case specified date is current date.
'Display Current Month on the screen Sub VBA_Month_Function_Ex1() 'Variable declaration Dim sCurrentMonth As Integer 'Assign current Month to variable sCurrentMonth = Month(Now) MsgBox sCurrentMonth, vbInformation, "Current Month" End Sub
In the above example ‘sCurrentDate’ declared as an Integer data type. And Month is a vba function, it returns two digit number (number from a 1 to 12) as Month. This variable ‘sCurrentDate’ now contains the two-digit Month.
Output: It displays two-digit Month on the screen. Here is the screen shot of first example output.
Here is another example of the VBA Month Function. This below example macro uses the Month function and displays the Month from the specified date on the Worksheet named ‘VBAF1.Com’ in Range ‘B18’.
'Display Current Month on the Worksheet Sub VBA_Month_Function_Ex2() 'Variable declaration Dim dCurrentMonth As Date 'Assign current Month to variable sCurrentMonth = Now Sheets("VBAF1.com").Range("B18") = Month(sCurrentMonth) 'or 'Sheets("VBAF1.com").Cells(18, 2) = Month(sCurrentMonth) End Sub
Output: It displays two-digit Month on the Worksheet in Rage B18. Here is the screen shot of second example output.
Here is one more example with VBA Month Function. This below example macro uses the Date function and changes the format of the date. Finally, it displays the complete current month name from the specified date on the screen.
Sub VBA_Month_Function_Ex3() 'Format and Display complete Current Month on the screen 'Variable declaration Dim sCurrentMonth As Date 'Assign current Month to variable sCurrentMonth = Date MsgBox Format(sCurrentMonth, "MMMM"), vbInformation, "Current Month" End Sub
Note: Difference between first output and second output is format of the Month.
Output: It displays complete current month name form the specified date on the screen. Here is the screen shot of third example output.
Click on following link to download free example excel workbook to learn more about the VBA Month function.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.