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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:36:29  来源:igfitidea点击:

How can I format a month in mmm format?

excelexcel-vbavba

提问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:

让我解释:

  • Dateis worth 21/02/2012
  • Month(Date)is worth 1
  • Formatwill consider this as a date so Month(Date) will be 1/1/1900
  • So Format(month(Date), "mmm")will return Janas the month of 1/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)returns 21/02/2012so this part is ok
  • Debug.Print (Format(Month(Date), "mmm"))returns janso this is not a displaying cell issue
  • Debug.Print (Date)返回21/02/2012所以这部分没问题
  • Debug.Print (Format(Month(Date), "mmm"))返回jan所以这不是显示单元格问题