vba 将月份名称转换为数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11895228/
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
Convert month name into number
提问by HL8
I have dropdown box with months populated. When a month is selected I would like to then convert it to the month number is there a function that can do this?
我有几个月填充的下拉框。选择月份后,我想将其转换为月份数是否有可以执行此操作的功能?
Eg. September = 9
例如。九月 = 9
回答by Siddharth Rout
Another way
其它的办法
Excel Formula
Excel公式
=MONTH(1&A1)
VBA
VBA
Sub Sample()
Dim MonthNm As String
MonthNm = "September"
Debug.Print Month(DateValue("01 " & MonthNm & " 2012"))
End Sub
or
或者
Sub Sample()
Dim MonthNm As String
MonthNm = "September"
Debug.Print Application.Evaluate("=MONTH(1&" & Chr(34) & MonthNm & Chr(34) & ")")
End Sub
Replace
代替
回答by Bharat Sinha
Try this...
尝试这个...
=MONTH(DATEVALUE(A1&"1"))
Where A1
cell contains month name.
其中A1
单元格包含月份名称。
回答by Inaki Melchor
Sub month()
Dim monthh As Integer
monthh = month(Date)
MsgBox monthh
End Sub
try this.
尝试这个。
回答by ThiamTeck
another excel formula where A1
is the cell id with month name:
另一个 excel 公式,其中A1
带有月份名称的单元格 id 是:
=TEXT(DATEVALUE(A1&" 1"), "m")
回答by Alexus
This solution didn't work for me (Excel 2010), I had to shorten the month name to 3 characters and add the day number in front of the shortened string.
此解决方案对我不起作用(Excel 2010),我必须将月份名称缩短为 3 个字符,并在缩短的字符串前添加日期。
=MONTH(1&LEFT(A1;3))
回答by T.M.
Another VBA solution
另一个 VBA 解决方案
For the sake of the art in addition to Siddharth's valid answer :-)
除了 Siddharth 的有效答案之外,为了艺术:-)
Sub SampleTM()
Dim MonthNm$: MonthNm = "September"
Debug.Print MonthNm2Num(MonthNm)
End Sub
Function MonthNm2Num(ByVal MonthNm) As Long
MonthNm2Num = Format(CDate(MonthNm & "/1 0"), "m") * 1&
End Function