3

I have a below string and was wondering how can I extract only date values from it and store them in separate cells.

11AUG2016 Changed gggqqq2i8yj 29SEP2016 Removed tyijdg298 30SEP2016 Added ,mkdjenb200 03OCT2016 zzxxddd4423 04OCT2016 jioi==++-234jju 24OCT2016 Updated tuiomahdkj 10JAN2017 Updated zzzz T4123III 13JAN2017 Updated jukalzzz123 20JAN2017 iiiwwwaazz678uuh

Shai Rado
  • 33,032
  • 6
  • 29
  • 51

4 Answers4

2

With data in A1 try:

Sub marine()
    Dim s As String, r As Range
    s = Range("A1").Value
    ary = Split(s, " ")
    i = 2
    For Each a In ary
            Cells(i, 1).Value = a
            If IsDate(Cells(i, 1).Value) Then
                i = i + 1
            End If
    Next a

    Set r = Cells(Rows.Count, 1).End(xlUp)
    If IsDate(r.Value) Then Exit Sub
    r.Clear
End Sub

enter image description here

The technique places a candidate in a cell and then tests if it a date. If it is a date, it is retained, otherwise it is overwritten.

Gary's Student
  • 95,722
  • 10
  • 59
  • 99
2

if dates are the only "numbers" then you can use SpecialCells()

Sub main()
    Dim arr As Variant

    arr = Split("11AUG2016 Changed gggqqq2i8yj 29SEP2016 Removed tyijdg298 30SEP2016 Added ,mkdjenb200 03OCT2016 zzxxddd4423 04OCT2016 jioi==++-234jju 24OCT2016 Updated tuiomahdkj 10JAN2017 Updated zzzz T4123III 13JAN2017 Updated jukalzzz123 20JAN2017 iiiwwwaazz678uuh", " ")
    With Range("A1").Resize(UBound(arr) + 1)
        .Value = Application.Transpose(arr)
        .SpecialCells(xlCellTypeConstants, xlTextValues).Delete xlUp
    End With
End Sub

if the string is in cell "A1" the code becomes:

Sub main()
    Dim arr As Variant

    With Range("A1")
        arr = Split(.Value, " ")
        With .Resize(UBound(arr) + 1)
            .Value = Application.Transpose(arr)
            .SpecialCells(xlCellTypeConstants, xlTextValues).Delete xlUp
        End With
    End With
End Sub
user3598756
  • 28,893
  • 4
  • 18
  • 28
2

Following approach retains the string format - i.e. date is written as string (it uses a simple Regular Expression). Assumption: your string is written in cell A1.

Sub ExtractDateFromString()
    Dim s As String: s = Range("A1")
    Dim re As Object: Set re = CreateObject("VBScript.RegExp")
    re.Global = True
    re.Pattern = "(\d{2}[A-Z]{3}20\d{2}\s)"
    Set d = re.Execute(s)
    r = 2
    For Each x In d
        Range("A" & r) = x
        r = r + 1
    Next
End Sub
Eddie Kumar
  • 1,216
  • 18
  • 20
0

Try the code below.

Added some error handling in case the RegEx will pass, but the value inside is not a valid date.

Option Explicit

Sub ExtractDates()

Dim Reg1 As Object
Dim RegMatches As Variant
Dim Match As Variant
Dim i As Long

Dim dDay As Long
Dim dYear As Long
Dim dMon As String

Set Reg1 = CreateObject("VBScript.RegExp")
With Reg1
    .Global = True
    .IgnoreCase = True
    .Pattern = "(\d{2}[a-zA-Z]{3}\d{4})" ' Match any set of 2 digits 3 alpha and 4 digits
End With

Set RegMatches = Reg1.Execute(Range("A1").Value)

i = 1
If RegMatches.Count >= 1 Then
    For Each Match In RegMatches
        dDay = Left(Match, 2)
        dYear = Mid(Match, 6, 4)
        dMon = Mid(Match, 3, 3)

        On Error Resume Next
        If Not IsError(DateValue(dDay & "-" & dMon & "-" & dYear)) Then
            If Err.Number <> 0 Then
            Else
                Range("B" & i).Value = (Match)
                Range("C" & i).Value = DateValue(dDay & "-" & dMon & "-" & dYear) ' <-- have the date (as date format) in column C
                i = i + 1
            End If
        End If
        On Error GoTo 0
    Next Match
End If

End Sub
Shai Rado
  • 33,032
  • 6
  • 29
  • 51