【发布时间】:2017-02-15 01:21:38
【问题描述】:
我正在从许多工作簿中收集数据。问题是,在每个文档中,日期格式并不一致: 16/01/2015 2015-01-06 24.03.2014
我想要实现的是“YYYY-MM-DD”格式。在我的代码中,我有一个案例负责清除日期列。现在我越来越绝望,我在代码中添加了愚蠢的日期格式,但在点格式(上面加粗的格式)方面没有任何变化。即使我手动选择列并更改格式类型,或者将值复制到 .txt 文件然后复制回原始工作表,也尝试使用新的 WorkBook,但没有任何反应。为什么在这几个例子中无法更改日期值?任何帮助,将不胜感激。代码如下:
Case 6:
sourceWorkbook.Activate
sourceWS.Activate
sourceWS.Columns(i).Select
Selection.NumberFormat = "YYYY-MM-DD;@"
sourceWS.Range(Cells(2, i), Cells(lastrw, i)).NumberFormat = "YYYY-MM-DD;@"
For j = startRow To lastrw Step 1
'Assign the header to the first row
NewWorksheet.Cells(1, i) = sourceWS.Cells(startRow, i).Value
On Error Resume Next
textToFormat = CStr(sourceWS.Cells(j, i).Value)
d = CDate(textToFormat)
finalDate = Format(textToFormat, "YYYY-MM-DD")
NewWorksheet.Cells(j - adjustRows, i) = finalDate
'This error handler purpose to handle the header name!
If Err Then
NewWorksheet.Cells(j - adjustRows, i) = textToFormat
End If
On Error GoTo 0
Next j
Set fckFormat = NewWorksheet.Columns(i)
fckFormat.NumberFormat = "YYYY-MM-DD;@"
NewBook.Activate
NewWorksheet.Activate
NewWorksheet.Columns(i).Select
Selection.NumberFormat = "YYYY-MM-DD;@"
【问题讨论】:
-
查看开头的答案:很多人似乎遇到了这个问题。
-
@R3uK ,您的意思是在源代码表中还是在代码中?我测试了它适用于除上述格式之外的所有其他格式的代码。
-
以
24.03.2014格式保存日期值的单元格,它们的格式是日期还是字符串? -
+1 @Jordan 问题!手动更改格式会影响日期单元格中显示的内容吗?我建议您可以在此处找到该实用程序:onedrive.live.com/…