Excelmacros将date值转换为纯文本,无格式

类似的问题(但不一样), 这一个 。

我的问题稍有不同。 我有一列date从mm/dd/yyyy转换为yyyy-mm-dd 。 例如:

2013年11月4日
2013年11月5日
二〇一三年十二月十日

这些单元格的格式如下: yyyy-mm-dd

我遇到了符合XML Schema的问题,然后导出到XML。 我认为这个问题与单元格公式有关(似乎在没有公式的情况下工作),因此我只想保留文本并删除格式。

有一个Clear > Clear Formats选项,但将date转换回原来的状态mm/dd/yyyy

我也需要在macros中这样做。

在这里我的伪代码:

 For Each cell In myRange Dim s As String s = c.Text c.ClearFormats c.Text = s Next 

我认为这将工作,但它不…我得到一个object required错误消息。

这至less是正确的语法:

 Sub demo() Dim Myrange As Range, Cell As Range Dim s As String Set Myrange = Range("A1:A10") For Each Cell In Myrange s = Cell.Text Cell.ClearFormats Cell.Value = s Next Cell End Sub 

但你可能实际上需要

 Sub demo() Dim Myrange As Range, Cell As Range Dim s As String Set Myrange = Range("A1:A10") For Each Cell In Myrange s = Cell.Text Cell.NumberFormat = "@" Cell.Value = s Next Cell End Sub 

这样做有点棘手..如下所示

 For Each Cell In myrange Dim s As String s = "'" & Cell.Text Cell.ClearFormats Cell.Value = s Next