[英]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当前数据示例
Desired Ouput期望输出
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.