What is VBA MONTHNAME Function
The VBA MONTHNAME function is listed under the date and time category of VBA functions. When you use it in a VBA code, it returns the month name as a string from a valid date as per VBA. In simple words, you can extract month value a name from a date supplied.
how to use it
To use VBA's MONTHNAME function you need to understand its syntax and arguments:
- Month: an integer (ranging 1 to 12) representing month.
- [Abbreviate]: A boolean value to specify if you need to a full name of the month or abbreviated name [This is an optional argument and if omitted VBA takes FALSE by default].
- Use TRUE for abbreviated name (i.e. "Jan", "Feb", "Mar", etc.) or FALSE for full name (i.e. "January", "February", "March", etc.)
Example to use MONTHNAME Function in VBA
To practically understand how to use VBA MONTHNAME function, you need to go through the below example where we have written a vba code by using it:
Sub example_MONTHNAME() Range("B1").Value = MonthName(Range("A1"), False) End Sub
In the above code, we have used the MONTHNAME to get the name of the month as per the value in cell A1 and we have a 1 in the cell A1 so it has returned the “January”.
Below are some important points which you need to take care while using MONTHNAME function in VBA.
- If the value specified as month is other than an integer or a string that can't be recognized as a number, VBA will return the run-time 13 error.
About the Author
Puneet is using Excel since his college days. He helped thousands of people to understand the power of the spreadsheets and learn Microsoft Excel. You can ﬁnd him online, tweeting about Excel, on a running track, or sometimes hiking up a mountain.