Exceldate问题

使用Excel 2003 VBA代码,我将表单中的date复制到单元格中,然后将其格式化为vbLongDate。 这就像“2014年8月13日星期三”这样的date。 尽pipe如此,我正在努力使date组件恢复正常。 我正在尝试这个

Dim myDate As Date myDate = Date(Range("A1").Value) 

从那里,我想从myDate中提取年,月,日值,但这是行不通的。 我尝试了一些变化,但他们都轰炸。

谢谢!

我假设你使用FormatDateTime()函数与vbLongDate。 这返回文本。 考虑使用date格式格式化单元格,并保留其date属性。

 Sub test() Dim myDate As Date ' this will return text Range("A1") = FormatDateTime(Now, vbLongDate) ' A2 will remain a date Range("A2") = Date Range("A2").NumberFormat = "[$-F800]dddd, mmmm dd, yyyy" myDate = Range("A2").Value Debug.Print Month(myDate) ' no error End Sub 

编辑:根据使用的date格式,date的外观将调整为区域设置。

 ' this date format will adjust to the regional settings ' my regional settings are DMY and I see "Thursday, 14 August 2014" ' even though the format shows the mmmm before the dd myRange1.NumberFormat = "[$-F800]dddd, mmmm dd, yyyy" ' this date format will always be like displayed below myRange2.NumberFormat = "dddd, d mmmm yyyy;@" 

您可以根据是否需要调整,将不同的格式设置为不同的单元格。