Can you extract each instance of *##-##-##* in a string?

HockeyDiablo

Board Regular
Joined
Apr 1, 2016
Messages
182
Say you had the following data in "A1";

12-24-16 was Christmas, 03-15-16 was another day 07-13-16 people didn't always use commas; 09-27-16 or whatever type well.

Could you get the data extracted like this?:

[TABLE="class: grid, width: 600, align: center"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[/TR]
[TR]
[TD="align: center"]data thats above[/TD]
[TD="align: center"]12-24-16[/TD]
[TD="align: center"]03-15-16[/TD]
[TD="align: center"]07-13-16[/TD]
[TD="align: center"]09-27-16[/TD]
[TD="align: center"]open for more occurrences?[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
t5MSAzsRZwQ



Code:
Sub ExtractDates()  Dim R As Long, X As Long, Txt As String, Nums As Variant
  For R = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    Txt = Cells(R, "A").Value
    For X = 1 To Len(Txt) - 8
      If Mid(Txt, X, 1) Like "[!0-9-]" Then Mid(Txt, X) = " "
    Next
    Nums = Split(Application.Trim(Txt))
    For X = 0 To UBound(Nums)
      If Not (Nums(X) Like "##-##-##" Or Nums(X) Like "##-##-####" Or _
              Nums(X) Like "#-##-##" Or Nums(X) Like "#-##-####" Or _
              Nums(X) Like "##-#-##" Or Nums(X) Like "##-#-####" Or _
              Nums(X) Like "#-#-##" Or Nums(X) Like "#-#-####") _
              Then Nums(X) = ""
    Next
    Cells(R, "B").Value = Application.Trim(Join(Nums))
  Next
  Columns("B").TextToColumns , xlDelimited, , , 0, 0, 0, 1
End Sub

It picked up all 8 instances of date.

06.30.2016-14.16.55 - JamesStruss7324's library
Oh, you extended my code to include the four 4-digit years... yes, that would work... I misunderstood what you meant in your previous message. I still don't see how this code would be so dramatically faster than the original code which you said took 50 minutes to run. The underlying algorithm in both codes is the same and, as such, should run in about the same time frame (with the new code being slightly slower because of the seven additional Like test). If you are getting a consistent fast time with this latest code then there was something wrong with you timing test originally.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Oh, you extended my code to include the four 4-digit years... yes, that would work... I misunderstood what you meant in your previous message. I still don't see how this code would be so dramatically faster than the original code which you said took 50 minutes to run. The underlying algorithm in both codes is the same and, as such, should run in about the same time frame (with the new code being slightly slower because of the seven additional Like test). If you are getting a consistent fast time with this latest code then there was something wrong with you timing test originally.


I am uncertain why it did take so long on the initial run. My 6 gen core i7 6700k processor just rips through them now. The only thing that matters is it works now and I can move on to the next daunting task of this project.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top