vba 如何以 mmm 格式格式化月份?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9375354/
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 can I format a month in mmm format?
提问by user1199080
I am trying to format a date in Excel with VBA, the current month to be in mmmformat. Somehow I am getting the previous month, instead of the current month. I have checked and my computer month is Feb, but I am getting Jan, instead.
我正在尝试使用 VBA 在 Excel 中格式化日期,当前月份为mmm格式。不知何故,我得到了上个月,而不是当月。我已经检查过,我的电脑月份是二月,但我收到的是一月。
This is my code:
这是我的代码:
Cells(1, 2) = Format(month(Date), "mmm")
回答by brettdj
Just format the existing date directly, ie
直接格式化现有日期即可,即
Cells(1, 2) = Format(Date, "mmm")
回答by JMax
You are formating the date 1/1/1900
您正在格式化日期1/1/1900
Let me explain:
让我解释:
Date
is worth21/02/2012
Month(Date)
is worth1
Format
will consider this as a date so Month(Date) will be1/1/1900
- So
Format(month(Date), "mmm")
will returnJan
as the month of1/1/1900
Date
值得21/02/2012
Month(Date)
值得1
Format
会将其视为日期,因此 Month(Date) 将是1/1/1900
- 所以
Format(month(Date), "mmm")
将Jan
作为月份返回1/1/1900
To teach you fishing, when you face this kind of issue, you can try to find the value of each statement :
要教你钓鱼,当你面对这样的问题,你可以尝试找到每个语句的值:
Debug.Print (Date)
returns21/02/2012
so this part is okDebug.Print (Format(Month(Date), "mmm"))
returnsjan
so this is not a displaying cell issue
Debug.Print (Date)
返回21/02/2012
所以这部分没问题Debug.Print (Format(Month(Date), "mmm"))
返回jan
所以这不是显示单元格问题