I can't get the current month.
It seems very simple to get the current year and day, as tested with the following:
MsgBox Year(Date)
MsgBox Day(Date)
MsgBox Year(Now)
MsgBox Day(Now)
How is it possible to show the current month as either a number (1, 2 etc.) or a full name?
I could use TODAY()
in a cell and convert that in VBA with something like CurrentMonth = MonthName(Month(Sheet1.Range("A1")))
but I would like to do this directly in VBA for Excel.
Try,
Returns the index number associated with the current month.
Jeeped's code below is the most compact, but to give you an idea of how indexes work, the following code will return the month name based on the index returned:
Below is how I found the previous month based on the current month name, the assignment to monthNum is the piece needed to solve your question.
Found an easier solution to get the current Month Name
Just use
MonthName(Month(Now))
and assign it to a string.Month(Now)
gives you the month number andMonthName()
uses that number to display the current monthHere is the best way I have found to do it:
A really helpful and simple way is to combine the format function together with date.
Examples (assuming today is Oct 23, 2019):
To get current month as a number as in original question:
^ Will return: 10
To get current month as short text:
^ Will return: Oct
To get current month with full text:
^ Will return: October
You can combine these with days and years as well.
Additional examples:
^ Will return 23-Oct-2019
^ Will return: Wednesday-October-23-2019
This is creating a custom format, so you can rearrange the dd, mm, yyyy areas as you see fit, such as:
^ Will return: 2019/23/10