Extracting year from text string

Ducklings23

New Member
Joined
Mar 11, 2020
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
Hi all. I'm trying to extract the year (so four digit number) from various strings of text. The text may or may not contain other numbers. For context - worksheet is a list of incident reports and in some instances the date is contained within the description of the incident. I need to break the year out. Most of the time it just say things like "it was in November or late 2015" etc. I just want to extract any 4 digit number from the text, then I can check if it is a year or not afterwards. The year can appear in any part of the text string. Hope that makes sense! Thanks in advance.
 
Perhaps a user-defined function like this might help? To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below (you can use the icon at the top right of the code pane below) into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

VBA Code:
Function FourDigits(s As String) As String
  Dim M As Variant
 
  With CreateObject("VBSCript.RegExp")
    .Global = True
    .Pattern = "(^|\D)(\d{4})(?=\D|$)"
    For Each M In .Execute(s)
      FourDigits = FourDigits & ", " & M.Submatches(1)
    Next M
  End With
  FourDigits = Mid(FourDigits, 3)
End Function

Ducklings23.xlsm
AB
1it was in November or late 20152015
2It was 2015 or 2014 in November2015, 2014
3 
4cbcb 
523659 abc 23452345
620122012
Sheet1
Cell Formulas
RangeFormula
B1:B6B1=FourDigits(A1)
This worked perfectly - thank you!
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Alternative formula
Assum year in text string always after, i.e, year 2000
There is no other strange value exceed 2000
Book1
ABC
1blah blah 2000 blah2000
2blah blah 2021 blah2021
3blah blah15/11/2021blah2021
4blah 1500 blah2021blah2021
5blah blahblahNo date
6blah blah 200 blahNo date
Sheet2
Cell Formulas
RangeFormula
C1:C6C1=IFERROR(AGGREGATE(14,6,(MID(SUBSTITUTE(A1,"/","|"),ROW($1:$100),4)+0)/(MID(A1,ROW($1:$100),4)+0>1999),1),"No date")
 
Upvote 0
would you mind talking me through what the code you provided is doing in each step?
Regular Expressions are not easy to give simple explanations for. You would need to do some research on them.
The best I think that I can say is that the 'Pattern' "(^|\D)(\d{4})(?=\D|$)" looks for
(^|\D) The beginning of the string OR a non-digit
followed by
(\d{4}) exactly 4 digits
then looks ahead to check that the next character is
(?=\D|$) either a non-digit OR the end of the string

Then if that pattern is found, return the blue part (the 4 digits)



Alternative formula
Try your formula with these strings

Ducklings23.xlsm
A
1My code in 2015 was 23E4
2It was 1DEC2015
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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