所以我在我的专栏中混淆了日期信息。例如Jan preplan-2017,Feb-afterplan-2017-low等
两件事总是一致的:月份总是以3个字母开头。年份有4位数字(可以在任何地方。)
我基本上需要标准日期格式(例如1/1 / 2017,1 / 2017/2017等)
答案 0 :(得分:1)
首先将字符串转换为日期值:
s = "Jan preplan-2017"
TrueDate = DateValue("1 " & Left(s, 3) & " " & Right(s, 4))
然后 - 根据需要显示格式:
ShowDate = Format(TrueDate, "d.m.yyyy")
或者一气呵成:
ShowDate = Format(DateValue("1 " & Left(s, 3) & " " & Right(s, 4)), "d.m.yyyy")
编辑:使用Split创建元素数组,循环这些并选择一个整数:
Year = Split("Feb-afterplan-2017-low", "-")(2)
答案 1 :(得分:0)
我对RegEx并不擅长,所以毫无疑问这可以改进。
作为工作表功能:
Public Function ConvertDate(sData As String) As Variant
Dim RE As Object, REMatches As Object
Dim Temp As String
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = False
.IgnoreCase = True
.Pattern = "\d{4}"
End With
Set REMatches = RE.Execute(sData)
If REMatches.Count > 0 Then
Temp = "1-" & Left(sData, 3) & "-" & REMatches(0)
ConvertDate = CDate(DateValue(Temp))
Else
'Returns #VALUE error on no match.
ConvertDate = CVErr(xlValue)
End If
End Function
按引用传递日期并返回TRUE / FALSE:
Public Sub Test()
Dim MyDate As Date
If ConvertDate1("Jan preplan-2017", MyDate) Then
MsgBox "Date converted to " & Format(MyDate, "dd-mmm-yy"), vbOKOnly
Else
MsgBox "Date not converted.", vbOKOnly
End If
End Sub
Public Function ConvertDate1(sData As String, ByRef ReturnValue As Date) As Boolean
Dim RE As Object, REMatches As Object
Dim Temp As String
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = False
.IgnoreCase = True
.Pattern = "\d{4}"
End With
Set REMatches = RE.Execute(sData)
If REMatches.Count > 0 Then
Temp = "1-" & Left(sData, 3) & "-" & REMatches(0)
ReturnValue = CDate(DateValue(Temp))
ConvertDate1 = True
Else
ConvertDate1 = False
End If
End Function
可以改进RegEx以检查前三个字母是否是一个月...可以进行加载以改善,但是你得到了我希望的图片。