VBAdate:无法获取格式正确

我正在尝试将date从“月/日/年”等date转换为完全拼写,如2014年5月31日。

我遇到了障碍。 目前,我正在使用这个,当popup消息框时,如果有正确的date(2014年5月31日),但是一旦我将它写入单元格,它将转换为一个数字(从5/31/14到41790)。 我迷路了,会喜欢一些帮助。 谢谢!

Dim whatever as String whatever = Format("5/31/14","mmmm dd, yyyy") MsgBox whatever ActiveWorkbook.Activesheet.Cells(1,1) = whatever 

我有一个程序,使用工作表中的数据运行一个邮件合并的单词,所以我想把整个date写出来,而不是简单地格式化单元格,因为Word将原始数据(从我所知道的。)

当试图在单元格中显示格式时,正确的方法是更改​​单元格的数字格式,然后设置单元格的值

例如

 Cells(1,1).NumberFormat="MMM DD, YYYY" Cells(1,1).Value2= Date 

要获得序号格式的date,请考虑:

 Public Function OrdinalDate(d As Date) As String Dim dy As Long, mnth As String, yr As Long Dim s As String ar = Array("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th") dy = Day(d) mnth = MonthName(Month(d)) yr = Year(d) If dy > 10 And dy < 20 Then s = "th" Else s = ar(dy Mod 10) End If OrdinalDate = dy & s & " " & mnth & " " & yr End Function 

在这里输入图像说明

当然删除序号不需要VBA 。 使用B1中的序号,使用:

 =DATEVALUE(SUBSTITUTE(B1,MID(B1,FIND(" ",B1)-2,2),"")) 

在这里输入图像说明

我们又回到了我们开始的地方。