VBA MONTHNAME Function

VBA MonthName Function

Overview of VBA MonthName Function:


VBA MonthName function is categorized 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, where we can use this MonthName Function and real-time examples in Excel VBA.

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 ignored or omitted, it takes default value FALSE. If it is TRUE, the name of the month is abbreviated. If it is FALSE, the name of the month is not abbreviated.

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.
VBA MonthName Function

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.
VBA MonthName Function

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.
VBA MonthName Function

Example 4: Display an Abbreviate Month Name of Specified 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 Specified 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.
VBA MonthName Function

Example 5: Display Non Abbreviate Month Name of Specified 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 Specified 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.
VBA MonthName Function

Instructions to use Macro Codes

Here are the step by step instructions to use the VBA MonthName function in Excel VBA.

  • Open an Excel workbook.
  • Press Alt+F11 to open VBA Editor window.
  • Go to Insert menu and click on module from the available options.
  • Copy above specified procedures or macros to the VBA editor.
  • Select any procedure from above codes and click on ‘Run’ command or use Keyboard shortcut ‘F5’.

List of VBA Functions:

Please click on below link to go back to see all List of VBA Functions.

VBA Date & Time Functions List of VBA Functions

Leave a Comment

Your email address will not be published. Required fields are marked *