What is wrong my formula?

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Trying to extract a date in any part of a cell. However I can't get the following formula to work.

VBA Code:
=IFERROR(DATEVALUE(LEFT(RIGHT(A2,LEN(A2)-FIND("/",A2)-3))),10)),"")
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
try

varios 31ene2024.xlsm
AB
1
2Trying to extract a date 31/01/2024 in any part of a cell31/01/2024
3Trying to extract a date in any part of a cell 
Hoja4
Cell Formulas
RangeFormula
B2:B3B2=IFERROR(DATEVALUE(MID(A2,FIND("/",A2)-2,11)),"")
 
Upvote 0
Can you give some examples of what you can put in the cell and what do you expect as a result?

As you can see, my example works.

You must provide more information, it is not enough to put:
I just get a blank. No errors though.

:unsure:
 
Upvote 0
Actually it kinda worked. Here is what I get. I tried it on the second cell at random and I got a blank as you can see. If I use 10/05/2018, then it works.

Financial Statements dated 31/12/2018
31/12/2018​
Deposits on 10/5/2018 were 25 million
I was born on 2/2/1958
02/02/1958​
When were you born?
You can come and join us on 01/01/2021 for training
01/01/2021​
 
Upvote 0
Try this and format the formula column with your preferred date format.

24 02 01.xlsm
AB
1
2Financial Statements dated 31/12/201831/12/2018
3Deposits on 10/5/2018 were 25 million10/05/2018
4I was born on 2/2/195802/02/1958
5When were you born? 
6You can come and join us on 01/01/2021 for training01/01/2021
Extract Date
Cell Formulas
RangeFormula
B2:B6B2=LET(t,TEXTSPLIT(A2," "),IFERROR(--FILTER(t,ISNUMBER(FIND("/",t))),""))
 
Upvote 0
If you need it to work on all the versions of Excel you have listed, maybe
Excel Formula:
=IFERROR(--TRIM(MID(A2,FIND("/",A2)-2,10)),"")
 
Upvote 0
try with VBA

VBA Code:
Sub FindDate()

    Dim RegEx As RegExp
    Dim strPattern As String
    Dim strInput As String
    Dim strOutput As String
    Dim objMatches As Object
    Dim i As Long
    Dim rng As Range
    
    On Error Resume Next

    Set RegEx = New RegExp
    RegEx.Global = True
    RegEx.IgnoreCase = True

    'Set the RegEx pattern to find the date string within the input string
    RegEx.Pattern = "\b\d{1,2}[\/-](\d{1,2}|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))[\/-](\d{4}|\d{2})\b"
    
    
    'Set the range of input string cells
    Set rng = Range("A1:A5")

    For i = 0 To rng.Count - 1
        strInput = Range("A1").Offset(i, 0).Value
    
        ' Find all matches of pattern in input string
        Set objMatches = RegEx.Execute(strInput)

        ' Returning only the first value found in the input string
        strOutput = objMatches(0).Value
        Range("A1").Offset(i, 1).Value = strOutput
    Next i
    

End Sub

1706770243906.png
 
Upvote 0
If you need it to work on all the versions of Excel you have listed, maybe
Excel Formula:
=IFERROR(--TRIM(MID(A2,FIND("/",A2)-2,10)),"")
Don't know if it could be possible with the OP's data but that would fail if the date is the first thing in the cell & the date had a single-digit day value.
TEXTSPLIT method already handles that but if required, that circumstance could be overcome with your method as shown below in col D

@azizrasul
Again I don't know if it would ever be possible with your data but the TEXTSPLIT formula should also work if any of the dates in the text have 2-digit years instead of 4. eg row 8 below

24 02 01.xlsm
ABCD
1
2Financial Statements dated 31/12/201831/12/201831/12/201831/12/2018
3Deposits on 10/5/2018 were 25 million10/05/201810/05/201810/05/2018
4I was born on 2/2/1958 ss02/02/195802/02/195802/02/1958
5When were you born?   
6You can come and join us on 01/01/2021 for training01/01/202101/01/202101/01/2021
72/2/1999 is Tom's DoB02/02/1999 02/02/1999
8Deposits on 10/5/18 were 25 million10/05/2018  
Extract Date
Cell Formulas
RangeFormula
B2:B8B2=LET(t,TEXTSPLIT(A2," "),IFERROR(--FILTER(t,ISNUMBER(FIND("/",t))),""))
C2:C8C2=IFERROR(--TRIM(MID(A2,FIND("/",A2)-2,10)),"")
D2:D8D2=IFERROR(--TRIM(MID(" "&A2,FIND("/"," "&A2)-2,10)),"")
 
Upvote 0
When I use B2

=LET(t,TEXTSPLIT(A2," "),IFERROR(--FILTER(t,ISNUMBER(FIND("/",t))),""))

it comes up with an error "That function isn't valid". It highlights FILTER.

C2 and D2 works for 4 digit years but not 2 digit years as stated above.

SunnyAlv, thanks for that. Will try it in the future, ideally looking for a formula solution at the moment.
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,235
Members
449,092
Latest member
SCleaveland

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