简体   繁体   English

从带有文本 SQL/Excel/VBA 的单个字段中提取多个日期

[英]Extracting Multiple Dates from a single field with text SQL/Excel/VBA

I currently have a spreadsheet with a free text column, within each entry there are multiple dates that sit in the format 13.1.11 or 1.11.12 (dd.mm.yy).我目前有一个带有自由文本列的电子表格,在每个条目中都有多个日期,格式为 13.1.11 或 1.11.12 (dd.mm.yy)。 What i would like to do is extract all the dates from each entry, below I have listed a sample of the data and also the desired output.我想要做的是从每个条目中提取所有日期,下面我列出了数据样本以及所需的输出。 Are there any solutions to get the desired output in Excel,VBA or SQL?是否有任何解决方案可以在 Excel、VBA 或 SQL 中获得所需的输出?

Example of current Data当前数据示例

  • 6.5.18 - no int in zone 1 but int in zone 2. 18.7.19 - summer update. 6.5.18 - 区域 1 中没有 int 而区域 2 中的 int。 18.7.19 - 夏季更新。
  • 12.7.19 - may have French investor. 12.7.19 - 可能有法国投资者。 11.9.19 - invests in series c firms 11.9.19 - 投资 C 系列公司
  • 4.5.17 - James interested in purchase. 4.5.17 - 詹姆斯有兴趣购买。 14.3.18 - only invest in passed ventures. 14.3.18 - 只投资于通过的企业。 20.11.18 - sent material. 20.11.18 - 发送材料。 24.7.19 - summer update. 24.7.19 - 夏季更新。
  • Zone IV 5.9.19 - not available区域 IV 5.9.19 - 不可用

Desired Ouput期望输出

  • 6.5.18; 6.5.18; 18.7.19 18.7.19
  • 12.7.19; 12.7.19; 11.9.19 11.9.19
  • 4.5.17; 4.5.17; 14.3.18; 14.3.18; 20.11.18; 20.11.18; 24.7.19 24.7.19
  • 5.9.19 5.9.19

Many Thanks非常感谢

If it's a one time thing, then I would suggest doing this in excel.如果这是一次性的,那么我建议在 excel 中执行此操作。 You could use "Text to Column" under the "Data" tab, with "-" as the delimiter to extract all of these individual cases.您可以使用“数据”选项卡下的“文本到列”,以“-”作为分隔符来提取所有这些个别情况。 Then you can use the left/right/mid functions, to extract just the date values.然后您可以使用左/右/中函数来仅提取日期值。 If it's going to be a regular thing, then you can look into more automated processes using VBA or SQL.如果这将是一件常规的事情,那么您可以使用 VBA 或 SQL 研究更多自动化流程。 Hope this helps.希望这可以帮助。

If the text is in column A this will put the dates in column B.如果文本在 A 列中,这会将日期放在 B 列中。

Option Explicit
Sub ExtractDates()

    Dim ws As Worksheet, sPattern As String
    Set ws = ThisWorkbook.Sheets(1)

    Dim Regex As Object
    Set Regex = CreateObject("vbscript.regexp")

    sPattern = "(\d{1,2}\.\d{1,2}\.\d\d)"
    With Regex
      .Global = True
      .MultiLine = False
      .IgnoreCase = True
      .Pattern = sPattern
    End With

    Dim cell, sOut As String, iLastRow As Long
    Dim match, dt

    iLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    For Each cell In ws.Range("A1:A" & iLastRow)
        sOut = ""
        If Regex.test(cell) Then
            Set match = Regex.Execute(cell)
            For Each dt In match
                If Len(sOut) > 0 Then sOut = sOut & "; "
                sOut = sOut & dt
            Next
            cell.Offset(0, 1) = sOut
        End If
    Next

End Sub

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

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