VBA for Extracting a Date from a Cell with Other Text

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
210
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm coding a macro that formats a table of data copied and pasted from an email, and part of what I need is to extract a date from a cell with other text, which I don't need, so all I have left is the date itself. The cell involved will always be in A1. Here is an example:

NHO - Friday 3/4/22 -7am-4pm

I simply want to be left with 3/4/2022.

Does anyone have any suggestions?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try:
VBA Code:
Sub ExtractDate()
    Range("A1") = Trim(Split(Range("A1"), "-")(1))
End Sub
 
Upvote 0
I end up with this afterward:

Friday 3/4/22

That's a good start. Can you eliminate the day of the week as well? It won't always be Friday, so it shouldn't be that specific.
 
Upvote 0
Hey FrenchCelt
This should do it no matter what data is either side of the date with the exceptions of "/"
VBA Code:
Function Get_Date() As Date
    Dim SpaceBefore As Integer
    Dim SpaceAfter As Integer
    With ThisWorkbook.Sheets("Data").Range("A1")
        SpaceBefore = InStr(1, .Value, "/", vbTextCompare) - 2
        SpaceAfter = InStr(SpaceBefore + 2, .Value, " ", vbTextCompare)
        Get_Date = DateValue(Mid(.Value, SpaceBefore, SpaceAfter - SpaceBefore))
    End With
End Function

Sub Processing()
ThisWorkbook.Sheets("Data").Range("A1") = Get_Date

End Sub
 
Upvote 0
Try:
VBA Code:
Sub ExtractDate()
    Range("A1") = Split(Trim(Split(Range("A1"), "-")(1)), " ")(1)
End Sub
 
Upvote 0
Solution
Thanks mumps, that did the trick.

I appreciate your suggestion, EFANYoutube. I wasn't able to get it to work by the time mumps got back to me with the reply that did work.
 
Upvote 0
You are very welcome. :)
 
Upvote 0
No issues, Just make sure that the format is always exactly the same as it is in your example or Mumps will not work. If you want further help with mine let me know, it did work in my mock workbook
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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