Excel VBA-将日期字符串转换为日期 [英] Excel VBA - Convert Date String to Date
问题描述
我在单元格中有许多字符串,它们是日期,但需要将它们转换为日期格式.
I have a number of strings in cells which are dates but they need to be converted to date format.
它们的格式如下:
mmm dd, yyyy
例如:
Feb 10, 2016
因此它们的长度可以是11或12:
So they can be 11 or 12 in length:
Feb 1, 2016
我已经开始编写一个函数来分别解析字符串的每个部分(以天为整数,以月为整数,以年为整数),然后将其转换为日期格式.
I have started writing a function to parse each part of the string individually (day as integer, month as integer and year as integer) to then convert into date format.
首先,有没有比上述方法更容易/更轻松的方法?
Firstly, is there an easier/slicker way to do this than the above?
如果没有更简单的方法,最好的方法是将3个字母的月份(例如2月,3月或4月)转换为月份数字(例如2或3或4)?因为那是我真正坚持的唯一一点.
If there isn't an easier way, what's the best was to convert the 3 letter month (e.g. Feb or Mar or Apr) into a month number (e.g. 2 or 3 or 4)? As that is the only bit I'm really stuck with.
推荐答案
在VBA中,您可以使用:cdate("Feb 10, 2016")
.
作为函数,它将是:
In VBA you could use: cdate("Feb 10, 2016")
.
As a function this would be:
Public Function ConvertToDate(rng As Range) As Date
ConvertToDate = CDate(rng)
End Function
这篇关于Excel VBA-将日期字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!