VBA MonthName function in Excel is categorizes as a Date & Time function. This is a built-in Excel VBA Function. This function returns a string representing the month given a number from 1 to 12.
We can use this function in VBA and can’t use in Excel. This function we use in either procedure or function in a VBA editor window in Excel. We can use this VBA MonthName Function in any number of times in any number of procedures or functions. Let us learn what is the syntax and parameters of the MonthName function, And also learn we can use this MonthName Function and real-time examples in Excel VBA.
- Objective
- Syntax of VBA MonthName Function
- Parameters or Arguments
- Where we can apply or use VBA MonthName Function?
- Example 1: Display the Month Name of Month Number(1)
- Example 2: Display an Abbreviate Month Name of Month Number(3)
- Example 3: Display Non Abbreviate Month Name of Month Number(3)
- Example 4: Display an Abbreviate Month Name of Specific Date
- Example 5: Display Non Abbreviate Month Name of Specific Date
- Instructions to Run VBA Macro Code
- Other Useful Resources
Syntax of VBA MonthName Function
The syntax of the MonthName Function in VBA is
MonthName(Month,[Abbreviate])
The MonthName function returns a string value.
Parameters or Arguments:
The MonthName function has two arguments in Excel VBA.
where
Month:The Month is a required argument. It represents a month value 1 to 12.
[Abbreviate]:The [Abbreviate] is an optional argument. It accepts a Boolean value either TRUE or FALSE. If we ignore or omit, it takes default value FALSE. If it is TRUE, the name of the month is abbreviate. If it is FALSE, the name of the month is not abbreviate.
Where we can apply or use VBA MonthName Function?
We can use this MonthName Function in VBA 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.
Example 1: Display the Month Name of Month Number(1)
Here is a simple example of the VBA DateDiff function. This below example displays the month name of month number(1).
'Display the Month Name of Month Number(1) Sub VBA_MonthName_Function_Ex1() 'Variable declaration Dim sMonth As String Dim sMntName As String sMonth = 1 sMntName = MonthName(sMonth) MsgBox "The Month Name of number(1) is : " & sMntName, vbInformation, "VBA MonthName Function" End Sub
Output: Here is the screen shot of the first example output.
Example 2: Display an Abbreviate Month Name of Month Number(3)
Let us see one more example of the VBA DateDiff function. This below example displays an abbreviate month name of month number(3).
'Display an Abbreviate Month Name of Month Number(3) Sub VBA_MonthName_Function_Ex2() 'Variable declaration Dim sMonth As String Dim sMntName As String sMonth = 3 sMntName = MonthName(sMonth, True) MsgBox "An Abbreviate month name of number(3) is : " & sMntName, vbInformation, "VBA MonthName Function" End Sub
Output: Here is the screen shot of the second example output.
Example 3: Display Non Abbreviate Month Name of Month Number(3)
Let us see another example of the VBA DateDiff function. This below example displays non abbreviate month name of month number(3).
'Display Non Abbreviate Month Name of Month Number(3) Sub VBA_MonthName_Function_Ex3() 'Variable declaration Dim sMonth As String Dim sMntName As String sMonth = 3 sMntName = MonthName(sMonth, False) MsgBox "Non Abbreviate month name of number(3) is : " & sMntName, vbInformation, "VBA MonthName Function" End Sub
Output: Here is the screen shot of the third example output.
Example 4: Display an Abbreviate Month Name of Specific Date
One more example of the VBA DateDiff function. This below example displays an abbreviate month name of specified date.
'Display an Abbreviate Month Name of Specific Date Sub VBA_MonthName_Function_Ex4() 'Variable declaration Dim sMonth As String Dim sMntName As String sMonth = "15/11/2013" sMntName = MonthName(Month(sMonth), True) MsgBox "An Abbreviate month name of date " & sMonth & " is : " & sMntName, vbInformation, "VBA MonthName Function" End Sub
Output: Here is the screen shot of the fourth example output.
Example 5: Display Non Abbreviate Month Name of Specific Date
One more example of the VBA DateDiff function. This below example displays non abbreviate month name of specified date.
'Display Non Abbreviate Month Name of Specific Date Sub VBA_MonthName_Function_Ex5() 'Variable declaration Dim sMonth As String Dim sMntName As String sMonth = "15/11/2013" sMntName = MonthName(Month(sMonth), False) MsgBox "Non Abbreviate month name of date " & sMonth & " is : " & sMntName, vbInformation, "VBA MonthName Function" End Sub
Output: Here is the screen shot of the fifth example output.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays in Excel Blog
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers