【问题标题】:How to extract date time from an excel cell?如何从excel单元格中提取日期时间?
【发布时间】:2019-10-29 02:58:11
【问题描述】:

我有一个 excel 文件,其中的单元格包含一些 cmets,例如:

txxxxx:2019 年 10 月 15 日上午 11:38:48 - 客户 ID:xxxxx ) 第一个联系人 - 文本仅发送到 Mob TN xxxxxxw/Ref &TN


Txxxxxx:10/18/2019 下午 1:34:12 - 打电话给BEST CBR xxxxxx,与先生交谈,他说他们一直很忙,还无法查看/讨论。主动向他发送我们的直接信息,他们将在网上查询和/或很快给我们打电话。

一条短信已成功发送到 xxxxxx 的 Gull Family

OK WITH FINAL CB 下周。

文本可以是任何可能包含多个日期时间字段的内容。 我正在尝试从每个单元格中提取所有此类日期出现并将它们放在不同的列中

我尝试使用=regExFind=regExExtract。例如:

=RegExFind($Cell,"\d{2}/\d{2}/\d{4}")

我也试过=Text($cell, dd/mm/yyyy)

但是,这两种方法都行不通。 excel中有没有办法进行正则表达式提取,如果有,如何实现? 如果没有,提取所有日期时间字段的最佳方法是什么?

更新: 这是我使用的代码:

Function RegexExtract(ByVal text As String, _
                      ByVal extract_what As String, _
                      Optional separator As String = ", ") As String

Dim allMatches As Object
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
Dim i As Long, j As Long
Dim result As String

RE.pattern = extract_what
RE.Global = True
Set allMatches = RE.Execute(text)

For i = 0 To allMatches.count - 1
    For j = 0 To allMatches.Item(i).submatches.count - 1
        result = result & (separator & allMatches.Item(i).submatches.Item(j))
    Next
Next

If Len(result) <> 0 Then
    result = Right$(result, Len(result) - Len(separator))
End If

RegexExtract = result

End Function

但没有输出。

【问题讨论】:

  • regExFind()regExExtract() 不是 EXCEL 内置函数。你能出示他们的代码吗?
  • 您使用的是什么版本的 Excel?您能否提供更多包含多个日期时间信息和预期结果的示例?如果可以的话,可以使用 excel 公式或幂查询来完成吗?
  • 我添加了代码

标签: excel vba


【解决方案1】:

由于你的帖子不是很明确,我的回答不能再多了。 试试这个:

Dim regex As Object, str As String
Set regex = CreateObject("VBScript.RegExp")

With regex
    .Pattern = "([0-9]+)/([0-9]+)/([0-9]+)"
    .Global = True
End With

str = "Whatever string you have"

Set matches = regex.Execute(str)

For Each match In matches
    Debug.Print match.Value
Next match

所以你需要在你的范围内循环它。 str 应该是您在循环中的单元格,而不是 Debug.Print 您应该将此值带到像 Worksheet("?").Cells(i,j).Value = match.Value 这样的任何单元格。

希望对你有帮助

【讨论】:

    【解决方案2】:

    一个解决方法...而不是使用 RegEx,想法是在单元格中找到“AM”和“PM”,然后将 19/20 字符的字符串复制粘贴到“日期”列中萃取”。此方法的一个限制显然是只有当 AM 和 PM 始终以您的消息的日期格式出现时才有效。

    Sub ExtractDates()
    Dim myRange, cell As Range
    Dim StringInCell, MyDate As String
    Dim DateExtrColNum, i, j As Integer
    
    
     Set myRange = Worksheets("YourSheetName").UsedRange
     DateExtrColNum = myRange.Columns(myRange.Columns.Count).Column
     Cells(1, DateExtrColNum + 1).Value = "Date Extraction"
     j = 2
    
     For Each cell In myRange
    
       If Not cell.Find("AM") Is Nothing Or Not cell.Find("PM") Is Nothing Then
       StringInCell = cell.Value
       i = 1
       Do While InStr(i, StringInCell, "AM") <> 0 Or InStr(i, StringInCell, "PM") <> 0
       If InStr(i, StringInCell, "AM") <> 0 Then
       MyDate = Mid(StringInCell, InStr(i, StringInCell, "AM") - 20, 20)
       If InStr(1, MyDate, ":") = 1 Then
       MyDate = Right(MyDate, 19)
       End If
       i = InStr(i, StringInCell, "AM") + 1
       Else: MyDate = Mid(StringInCell, InStr(i, StringInCell, "PM") - 20, 20)
       If InStr(1, MyDate, ":") = 1 Then
       MyDate = Right(MyDate, 19)
       End If
       i = InStr(i, StringInCell, "PM") + 1
       End If
       Cells(j, DateExtrColNum + 1).Value = MyDate
       j = j + 1
       Loop
       End If
    Next
    
    End Sub
    
    

    【讨论】:

    • AM/PM 测试之前将 cell 保存到 StringInCell 将为您节省一个 COM打电话。
    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多