数字到date
我有一个数字forms的date,如下所示: 20150529
有没有办法将其转换为29/05/2015
?
我需要为此写一个macros。
我有这个代码,但它不起作用:
Dim Current_Date As Date Dim Date_String As String Range("K2").Select Date_String = Range("K2").Value Current_Date = CDate(Date_String) Range("Q2").Select Range("Q2").Value = Current_Date
Range.TextToColumns方法可以快速处理选区中的一个或多个单元格或整个20150629列(如date)。
sub YMD_2_Date with selection .TextToColumns Destination:=.cells(1), DataType:=xlFixedWidth, _ FieldInfo:=Array(0, 5) end with end sub
xlYMDFormat (例如5 )的xlColumnDataType属性强制Excel将该数字视为“ 年 – 月 – 日” 。
这可以从一个select(单个列中的一个或多个单元格)通过循环遍历到多个列来扩展。
您必须先以美式格式重新排列您的string(mm / dd / yyyy)。
然后你可以使用CDate()。
r = Mid(Date_String , 5, 2) & "/" & Right(Date_String , 2) & "/" & Left(Date_String , 4) current_date = CDate(r)
这现在也适用
Range("K2", Range("K2").End(xlDown)).Select For Each c In Selection.Cells c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2)) 'Following line added only to enforce the format. c.NumberFormat = "mm/dd/yyyy" Next