我遇到一些日期以Excel无法识别的格式导入的问题,因此我正在寻找通过vba轻松解决的问题。 格式为2018年12月31日。这似乎不是一种异常的格式,所以我试图将文本转换为数字格式,但没有用。我发现可行的唯一方法是将值切换为类似2018/12/31的格式。
基本上我想重新组织单元格的内容,但是不确定如何执行(我认为Trim函数可能是要使用的函数):
Dim rngCell As Range
Dim Lastrow As Integer
With Workbooks("Untitled Analysis.xlsx").Sheets("Untitled Analysis")
Lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngTemp = .Range("J2:J" & Lastrow)
For Each rngCell In rngTemp
TRIM(MID(J176,SEARCH("/",J176)+1,250))&"/"&LEFT(J176,SEARCH("/",J176)-1)
但是,这将返回12/2018/31
答案 0 :(得分:0)
如果您的数据始终以“ /”分隔,那么实际使用split函数可能更方便。这将只替换单元格中的所有值。完全公开,我还没有测试过,这也会将值更改为字符串,而不是实际的日期格式
Dim rngCell As Range
Dim Lastrow As Integer
With Workbooks("Untitled Analysis.xlsx").Sheets("Untitled Analysis")
Lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngTemp = .Range("J2:J" & Lastrow)
For Each rngCell In rngTemp
rngCell.Value = Split(rngCell.Value,"/")(2) & "/" & Split(rngCell.Value,"/")(1) & "/" &Split(rngCell.Value,"/")(0)
next rngCell
end With
编辑这应该将其转换为格式正确的日期值
Dim rngCell As Range
Dim Lastrow As Integer
With Workbooks("Untitled Analysis.xlsx").Sheets("Untitled Analysis")
Lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
Set rngTemp = .Range("J2:J" & Lastrow)
For Each rngCell In rngTemp
rngCell.Value = DateSerial(Split(rngCell.Value,"/")(2),Split(rngCell.Value,"/")(1),Split(rngCell.Value,"/")(0))
rngCell.NumberFormat = "yyyy-mm-dd;@"
next rngCell
end With