根据excel中的数字显示月份名称 [英] Show name of month according to a number in excel

查看:103
本文介绍了根据excel中的数字显示月份名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以将单元格容器的内容显示为月份(1月至12月)的整数(1-12)。如果我这样操作:

  With Cells( A1)
.NumberFormatLocal = MMM
.Value = 5

结束

Excel读取值5作为日期5 1900年1月返回了1月,而我想要的是5月。



问题是我不想在单元格A1中写一个日期,而只想写一个整数。当单元格A1按值复制到另一个单元格或读取时,我希望复制/读取的值是5,而不是2015年5月5日的42129。所以转换不是我想要的。



是否有任何自定义格式的代码和其他技术可以帮助将值1-12显示为Jan-Dec?谢谢您的帮助! ;每个数字/月份名称(从1到12)一个,并为每个数字名称提供一个文字自定义格式掩码,以拼写出缩写月份。

  Sub cf_months()
Dim m as long,cnf As String
With Worksheets( Sheet1)
With .Range( A1)
.FormatConditions.Delete
For m = 12 To 1 Step -1
cnf = StrConv(Format(DateSerial(2016,m,1), mmm),vbUnicode)
cnf = Join(Split(cnf ,vbNullChar),Chr(92))
cnf = Chr(91)& Chr(61)和并购Chr(93)和Chr(92)和左(cnf,Len(cnf)-1)& Chr(59)& Chr(59)& Chr(59)
.FormatConditions.Add Type:= xlExpression,Formula1:= Chr(61)&地址(0,0)& Chr(61)和m
.FormatConditions(.FormatConditions.Count).NumberFormat = cnf
Debug.Print cnf
下一个m

结尾的结束以
结尾Sub


I wonder if there is a way to show the content of a cell container an integer (1-12) as the name of the month (Jan-Dec). If I do it this way:

With Cells("A1")
    .NumberFormatLocal = "MMM"
    .Value = 5
End With

Excel reads the value 5 as the date 5 Jan 1900 as thus returns Jan, while what I want is May.

The thing is I do not want to write a date in cell A1 but only a single integer. When cell A1 is copied by value to another cell or when read, I hope that the value copied/read is 5, not 42129 as if it is 5 May 2015. So conversion is not what I want.

Is there any custom code of formatting and other technique that can help with displaying the values 1-12 as Jan-Dec? Any help is appreciated!

解决方案

If it is mission critical that you bypass the rules of dates and numbers then write 12 conditional formatting rules; one for each number/month name from 1 to 12 and supply a 'literal' custom format mask for each that spells out the abbreviated month.

Sub cf_months()
    Dim m As Long, cnf As String
    With Worksheets("Sheet1")
        With .Range("A1")
            .FormatConditions.Delete
            For m = 12 To 1 Step -1
                cnf = StrConv(Format(DateSerial(2016, m, 1), "mmm"), vbUnicode)
                cnf = Join(Split(cnf, vbNullChar), Chr(92))
                cnf = Chr(91) & Chr(61) & m & Chr(93) & Chr(92) & Left(cnf, Len(cnf) - 1) & Chr(59) & Chr(59) & Chr(59)
                .FormatConditions.Add Type:=xlExpression, Formula1:=Chr(61) & .Address(0, 0) & Chr(61) & m
                .FormatConditions(.FormatConditions.Count).NumberFormat = cnf
                Debug.Print cnf
            Next m
        End With
    End With
End Sub

            

这篇关于根据excel中的数字显示月份名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆