简体   繁体   English

如何从现有数据中获取月份名称和年份

[英]How can get Month Name and year from the existing data

This the data I have.这是我拥有的数据。

I just want its month name and year.我只想要它的月份名称和年份。

For example 201804 : Apr 2018例如201804Apr 2018

在此处输入图像描述

Try this UDF试试这个 UDF

Sub Test()
    Debug.Print FormatDate("201804")
End Sub

Function FormatDate(sInput As String)
    sInput = "1/" & Right(sInput, 2) & "/" & Left(sInput, 4)
    FormatDate = Format(CDate(sInput), "mmm yyyy")
End Function

Try尝试

Sub test()
    Dim vDB
    Dim i As Long
    Dim s As String, y As String, m As String

    vDB = Range("a1", Range("a" & Rows.Count).End(xlUp))

    For i = 2 To UBound(vDB, 1)
        s = vDB(i, 1)
        y = Left(s, 4)
        m = Right(s, 2)
        vDB(i, 1) = Format(DateSerial(y, m, 1), "mmm yyyy")
    Next i
    With Range("b1").Resize(UBound(vDB, 1), 1)
        .NumberFormatLocal = "@"
        .Value = vDB
    End With
End Sub

Here is the worksheet formula, converting the date in A1.这是工作表公式,转换 A1 中的日期。 Copy the formula down as required.根据需要将公式复制下来。

=DATE(LEFT(A1,4),RIGHT(A1,2),1)

The result is a proper date set for the first day of the month, in this case April 1, 2018. The format in which this result is displayed depends entirely upon your system settings.结果是为该月的第一天设置了正确的日期,在本例中为 2018 年 4 月 1 日。显示此结果的格式完全取决于您的系统设置。 However, you can over-ride the system by setting a custom date format in Format > Cells > Numbers > Custom*.但是,您可以通过在“格式”>“单元格”>“数字”>“自定义*”中设置自定义日期格式来覆盖系统。 Set Type as mmm yyyy to display Apr 2018 in the cell.类型设置为mmm yyyy以在单元格中显示Apr 2018

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM