vba 如何获取当前月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35921124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get current month?
提问by Tim Wilkinson
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?
如何将当前月份显示为数字(1、2 等)或全名?
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.
我可以TODAY()在一个单元格中使用并在 VBA 中使用类似的东西进行转换,CurrentMonth = MonthName(Month(Sheet1.Range("A1")))但我想直接在 VBA for Excel 中执行此操作。
回答by
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
回答by Scott Marcus
Month(Now)
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:
下面 Jeeped 的代码是最紧凑的,但为了让您了解索引的工作原理,以下代码将根据返回的索引返回月份名称:
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
回答by Justin
Found an easier solution to get the current Month Name
找到了一个更简单的解决方案来获取当前月份名称
Just use MonthName(Month(Now))and assign it to a string.
只需使用MonthName(Month(Now))并将其分配给一个字符串。
Month(Now)gives you the month number and MonthName()uses that number to display the current month
Month(Now)为您提供月份编号并MonthName()使用该编号显示当前月份
回答by David Mays
A really helpful and simple way is to combine the format function together with date.
一个真正有用且简单的方法是将格式函数与日期结合起来。
Examples (assuming today is Oct 23, 2019):
示例(假设今天是 2019 年 10 月 23 日):
To get current month as a number as in original question:
要将当前月份作为原始问题中的数字获取:
MsgBox Format(Date, "mm")
^ Will return: 10
^ 将返回:10
To get current month as short text:
以短文本形式获取当前月份:
MsgBox Format(Date, "mmm")
^ Will return: Oct
^ 将返回:10 月
To get current month with full text:
要获取当前月份的全文:
MsgBox Format(Date, "mmmm")
^ Will return: October
^ 返回时间:10 月
You can combine these with days and years as well.
您也可以将这些与天和年结合起来。
Additional examples:
附加示例:
MsgBox Format(Date, "dd-mmm-yyyy")
^ Will return 23-Oct-2019
^ 将于 2019 年 10 月 23 日返回
MsgBox Format(Date, "dddd-mmmm-dd-yyyy")
^ Will return: Wednesday-October-23-2019
^ 将返回:2019 年 10 月 23 日星期三
This is creating a custom format, so you can rearrange the dd, mm, yyyy areas as you see fit, such as:
这是创建自定义格式,因此您可以根据需要重新排列 dd、mm、yyyy 区域,例如:
MsgBox Format(Date, "yyyy/mm/dd")
^ Will return: 2019/23/10
^ 回归时间:2019/23/10
回答by William Humphries
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.
下面是我如何根据当前月份名称找到上个月,对 monthNum 的分配是解决您的问题所需的部分。
month = "February"
'****'
monthNum = Application.Evaluate("=MONTH(1&" & Chr(34) & month & Chr(34) & ")") 'Returns month #
'****'
If monthNum = 1 Then
monthNum = 12
Else
monthNum = monthNum - 1
End If
month = MonthName(monthNum) 'Returns January
回答by Anonymous X
Here is the best way I have found to do it:
这是我发现的最好的方法:
Sub getMonth()
'MsgBox DatePart("m", Date)
'MsgBox Evaluate("MONTH(""" & Date & """)")
'MsgBox VBA.DateTime.Month(Date)
MsgBox Format(Date, "mmmm")
End Sub

