在VBA中将日期转换为文本 [英] Converting a date to text in VBA

查看:1774
本文介绍了在VBA中将日期转换为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含诸如"2016年3月"之类的文本值.

I have a data set that includes values as text such as "March 2016".

现在,我编写了以下代码以将其复制到我的报告表中:

Now I have written the following code to replicate this in my report sheet:

DataFin.Range("E9:E9") = DateSerial(Year(Date), Month(Date), 0)
DataFin.Range("E9:E9").Text = Format(DateSerial(Year(Date), Month(Date), 0), "mmmm yyyy")

它具有正确的格式,但是当我单击该单元格时,该值显示为"3/1/2016",而不是"2016年3月".

It has the correct format, but when I click on the cell the value states "3/1/2016" instead of "March 2016".

任何想法如何将基础值更改为"2016年3月"

Any ideas how I can change the underlying value to "March 2016"

谢谢

Pete

推荐答案

您必须将单元格设置为文本格式,然后将该值设置为所需日期的文本值.

You must format the cell as text, and then set the value to the text value of the date you want.

例如:

Sub demo()
    Dim DT As Date
DT = Date

With Range("a1")
    .NumberFormat = "@"
    .Value = Format(DT, "mmmm yyyy")
End With
End Sub

这篇关于在VBA中将日期转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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