How to get the current month?

I can not get the current month.

It seems that getting the current year and day is very simple, as shown below:

MsgBox Year(Date) MsgBox Day(Date) MsgBox Year(Now) MsgBox Day(Now) 

How can I show the current month as a number (1, 2, etc.) or a full name?

I could use TODAY() in the cell and convert it to VBA using something like CurrentMonth = MonthName(Month(Sheet1.Range("A1"))) , but I would like to do it directly in VBA for Excel.

+10
source share
5 answers

Try

 debug.print Format(Date, "mmm") 'Mar debug.print Format(Date, "mmmm") 'March debug.print Format(Date, "m") '3 debug.print Format(Date, "mm") '03 
+26
source
  Month(Now) 

Returns the index number associated with the current month.

The jeeped 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:

 Dim months(11) As String months(0) = "Jan" months(1) = "Feb" months(2) = "Mar" months(3) = "Apr" months(4) = "May" months(5) = "Jun" months(6) = "Jul" months(7) = "Aug" months(8) = "Sep" months(9) = "Oct" months(10) = "Nov" months(11) = "Dec" Dim nowMonth As Integer nowMonth = Month(Now) For i = 0 To 11 If nowMonth = (i + 1) Then MsgBox (months(i)) End If Next 
+2
source

Found a simpler solution to get the name of the current month

Just use MonthName(Month(Now)) and assign it to a string.

Month(Now) gives you the month number, and MonthName() uses this number to display the current month.

+2
source

Here is the best way I found for this:

 Sub getMonth() 'MsgBox DatePart("m", Date) 'MsgBox Evaluate("MONTH(""" & Date & """)") 'MsgBox VBA.DateTime.Month(Date) MsgBox Format(Date, "mmmm") End Sub 
0
source

How to get the name of the previous month with the same formula as

Sub getMonth ()

MsgBox format (Date, "mmmm")

End sub

0
source

Source: https://habr.com/ru/post/1244830/


All Articles