Excelsearchdate格式

我使用的是Excels 2013 mid和searchfunction来从评论栏中获取date,但是我不知道如何去覆盖8个date格式(dd / mm / yyyy,dd / mm / yy等等)。 我的三个公式如下,但我需要将它们连接在一起以捕获所有date格式。注释列可以是任何文本,其中包含任何date。

=MID(A6, SEARCH("??/??/????", A6, 1), 10) =MID(A7, SEARCH("?/??/????", A7, 1), 9) =MID(A8, SEARCH("??/?/????", A8, 1), 9) 

必须有更好的方法来做到这一点,因为我能想到的唯一方法是将每个公式放在一个单独的列上,然后使用if语句来显示date。 任何帮助将是非常赞赏。 谢谢

样本列数据

 Paid on 01/01/2017 Paid on 1/01/2017, Reference AAA123 Reference BBB456 Paid on 01/1/2017 

如果string中的date之前没有其他数字,则此数组公式中的date之前没有其他数字

 =INDEX(--TRIM(CLEAN(MID(SUBSTITUTE(SUBSTITUTE(A1," ",REPT(" ",999)),",",""),(ROW(INDIRECT("1:" & LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1))-1)*999+1,999))),MATCH(TRUE,ISNUMBER(--TRIM(CLEAN(MID(SUBSTITUTE(SUBSTITUTE(A1," ",REPT(" ",999)),",",""),(ROW(INDIRECT("1:" & LEN(A1)-LEN(SUBSTITUTE(A1," ",""))+1))-1)*999+1,999)))),0)) 

作为一个数组公式,在退出编辑模式时,需要使用Ctrl-Shift-Enter而不是Enter来确认。

它通过空格parsingstring并遍历每个“单词”,直到find可以转换为数字的第一个数字,这是一个date。

这将要求将输出格式化为所需的date格式。

在这里输入图像说明

这是正则expression式的用法。

这里有一个函数实现正则expression式,如下所示: 如何在Microsoft Excel中使用正则expression式(正则expression式)在单元格内和循环

(请注意,您必须添加“Microsoft VBScript正则expression式5.5”参考,如链接中所述)

 Function showDate(thisVal As String) Dim strPattern As String Dim regEx As New RegExp strPattern = "[0-9]?[0-9]/[0-9]?[0-9]/[0-9]?[0-9]?[0-9][0-9]" If strPattern <> "" Then With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern End With Set objMatches = regEx.Execute(thisVal) For Each Match In objMatches 'The variable match will contain the full match showDate = Match Next End If End Function 

像这样使用它: =showDate(A1)

代码位:

  strPattern = "[0-9]?[0-9]/[0-9]?[0-9]/[0-9]?[0-9]?[0-9][0-9]" 

控制将要查找的模式。 这个? 在每个[0-9]范围表示一个“可选”字符之后,所以如果存在的话可以接受月份中的前导零或一个,但是缺less它不会导致匹配/查找错过。

请注意,此解决scheme提供了来自string的最后一个date实例。 如果你只有一个date的实例,那么每个string的多个date是一个你必须考虑的范围。