简体   繁体   English

Excel宏以格式化日期

[英]Excel Macro to format date

I have a column with some string value in excel file. 我在Excel文件中有一列带有一些字符串值的列。

   Col A
20131022082702
20131022082702
20131022082739
20131022082739

I want to convert each cell value in to date format eg YYYY/MM/DD HH:MM 我想将每个单元格值转换为日期格式,例如YYYY/MM/DD HH:MM

20131022082702   // should be formatted as 2013/10/22 08:27 

I was just wondering if is there any built in function we can use to format it using some kind of macro? 我只是想知道是否有任何内置函数可用于通过某种宏对其进行格式化?

One approach I can think of is taking sub string and writing custom logic in macro. 我可以想到的一种方法是采用子字符串并在宏中编写自定义逻辑。 Any other work around for it will be much appreciated ! 任何其他解决方法将不胜感激!

Thank you for your time. 感谢您的时间。

If you use the TEXT worksheet function, you can format it to look like a date/time stamp, and Excel will convert it to a "real" value. 如果使用TEXT工作表函数,则可以将其格式化为类似于日期/时间戳的格式,Excel会将其转换为“真实”值。 You can then format it as you wish: 然后,您可以根据需要设置其格式:

=--TEXT(A1,"0000\/00\/00 00\:00\:00")

EDIT : In VBA, one can use this line to do the conversion. 编辑 :在VBA中,可以使用此行进行转换。 Similar algorithm, but using native VBA functions: 相似的算法,但使用本机VBA功能:

cdate(format(N,"0000 00 00 00:00:00"))

where N is your 14 digit number 其中N是您的14位数字

比@Ron的答案更冗长,但是另一种方法是将字符串分离出来:

=TEXT(DATE(MID(A1,1,4),MID(A1,5,2),MID(A1,7,2))+TIME(MID(A1,9,2),MID(A1,11,2),MID(A1,13,2)),"dd/mm/yyyy hh:mm")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM