你不能用那种mmdd
数字格式真正格式化它。至少它不适合我。 (Excel 2010/Win 7 欧洲区域设置)
我建议它增加一个额外的列并复制数据以保留原始格式,最后隐藏该列。同时,您将使用Month()
和Day()
函数来创建所需的格式。
Sub formatColumns()
Dim lastRow As Long
' Set the LastRow variable to hold the last row number
lastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
Dim cell As Range
For Each cell In Range("B3:B" & lastRow)
'cell.NumberFormat = "mmdd;@"
cell.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
cell.Offset(0, -1).NumberFormat = "@"
cell.Offset(0, -1) = _
IIf(Len(Month(cell)) = 1, "0" & Month(cell), Month(cell)) & _
IIf(Len(Day(cell)) = 1, "0" & Day(cell), Day(cell))
Next
Columns("C:C").EntireColumn.Hidden = True
End Sub
,如果你不希望添加一个额外的列,但你不介意失去原始格式那么你可以使用此代码
Sub formatColumns()
Dim lastRow As Long
' Set the LastRow variable to hold the last row number
lastRow = Cells.Find("*", [A1], , , xlByRows, xlPrevious).Row
Dim cell As Range
For Each cell In Range("B3:B" & lastRow)
'cell.NumberFormat = "mmdd;@"
cell = IIf(Len(Month(cell)) = 1, "0" & Month(cell), Month(cell)) & _
IIf(Len(Day(cell)) = 1, "0" & Day(cell), Day(cell))
Next
End Sub
只需更换单元格的内容那么,你的满意是什么? – pnuts
如果可以,请在内存中进行转换,而不是循环超过10 000个单元。如果格式是固定的,您应该能够从输入中提取所需的字符串。 – Trace
问题是什么?您是否尝试应用格式化或者您是否将日期时间值转换为格式化的字符串? –