+ Reply to Thread
Results 1 to 16 of 16

Extract Date from String

  1. #1
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Unhappy Extract Date from String

    Hello Again, hopefully this will be the last time I annoy anyone for a Solution.

    The provided Data (attached) is where I am try to extract the Date and such is Driving me to the Wall.
    Each Description is different and that's why formula or VBA has been working on some rows and not others.
    Some Strings have full dates in them, others have just month and year and the rest have none or even week numbers.

    Dates and Excel don't get along well it seems

    Thank you for the attempt to resolve this matter.

    Cheers
    OP
    Attached Files Attached Files

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,675

    Re: Extract Date from String

    Try with UDF
    In E5:
    Please Login or Register  to view this content.
    drag down
    code:
    PHP Code: 
    Option Explicit
    Function GetDate(ByVal cell As Range)
    Dim m&, mon As Strings
    mon 
    " JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC "
    Split(cell)
    For 
    1 To UBound(s)
        If 
    InStr(1mons(m)) Then
            
    If IsNumeric(Left(s(1), 2)) Then
                
    If Right(s(1), 2) < 32 Then
                    GetDate 
    DateValue(Right(s(1), 2) & " " s(m) & " 20" Left(s(1), 2))
                Else
                    
    GetDate = (InStr(1mons(m)) + 2) / "/20" Left(s(1), 2)
                
    End If
            Else
                
    GetDate ""
            
    End If
            Exit Function
        
    End If
    Next
    GetDate 
    ""
    End Function 
    Attached Files Attached Files
    Quang PT

  3. #3
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Extract Date from String

    In E5 and drag down;
    =IF(ExtractDate(D5)=0,"",ExtractDate(D5))

    The this UDF in a module;

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Bebo, Your code.....what can I say.....Just Brilliant, I just tested it on over 5000 rows. It did not miss a date. WoW its really Super) Cant thank you enough
    Thank you for being Super wish I had asked earlier in time (So much frustration)

    All the best to you
    Cheers Bebo
    OP

  5. #5
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Hi Croweater, Thank you for your help in providing a solution.
    I tested it side by Side with Bebo's code they both work super fast and accurate. However your code misses on a few Rows. Not sure why.

    I am reattaching the sample with your code and Bebo Side by side so you can see where it misses.

    Regardless. Both of you are brilliant.

    Thank you

    Cheers
    OP
    Attached Files Attached Files

  6. #6
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,675

    Re: Extract Date from String

    Nice to hear it works.
    Cheer!

  7. #7
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Yes Bebo I have reattached the file with both of your codes side by side.
    Thank you once more for your great help
    Cheers
    OP

  8. #8
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Hi Bebo,
    Do you think you could modify your code in the to retrieve just the year from the string rather than full date and/or month and date as your code is currently?
    it seems that your code results recognizes the two year digit number as a proper year format rather than Excel formulas recognizing #23 as "1905" or "1900".
    Having a real hard time with excel and dates

    Thank you
    OP

  9. #9
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Extract Date from String

    Mine only picks out full dates. Didn't realise you wanted partial dates (just year when it's a partial date?) as well.

    Easily fixed/changed as to how you want it, but I'll let Bebo change his code...no point in having us both work on it.
    Last edited by AliGW; 04-30-2023 at 12:43 AM. Reason: Please do NOT quote unnecessarily!

  10. #10
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Hi Croweater,
    Thank you for the reply.
    I as well didn't realize that Month and Year would make it harder on me when "day" is not available in the string.
    I discovered it after running the codes. Yours works real well too I use both codes on different columns
    Cheers
    OP

  11. #11
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Extract Date from String

    @AliGW Please do NOT edit my posts unnecessarily!

    The post I was replying to and quoting was AT LEAST 4 posts previous!

  12. #12
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,675

    Re: Extract Date from String

    Quote Originally Posted by onp View Post
    Hi Bebo,
    Do you think you could modify your code in the to retrieve just the year from the string rather than full date and/or month and date as your code is currently?
    OP
    How many digits (2 or 4) or years do you expect?
    In below UDF, 1 is for 2 digits, 2 for 4 digits
    =getdate(D5,1)=23
    =getdate(D5,2)=2023
    If it not your expectation, try to manual input the outcome. (a few is enough)
    PHP Code: 
    Option Explicit
    Function GetDate(ByVal cell As RangeAs Integer)
    Dim m&, mon As Strings
    mon 
    " JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC "
    Split(cell)
    For 
    1 To UBound(s)
        If 
    InStr(1mons(m)) Then
            
    If IsNumeric(Left(s(1), 2)) Then
                
    If Right(s(1), 2) < 32 Then
                    GetDate 
    DateValue(Right(s(1), 2) & "/" s(m) & "/20" Left(s(1), 2))
                Else
                    
    GetDate = (InStr(1mons(m)) + 2) / "/20" Left(s(1), 2)
                
    End If
                
    GetDate Format(GetDateIIf(1"yy"IIf(2"yyyy""")))
            Else
                
    GetDate ""
            
    End If
            Exit Function
        
    End If
    Next
    GetDate 
    ""
    End Function 
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    01-25-2014
    Location
    Italy - Currently In USA
    MS-Off Ver
    Excel 2019
    Posts
    213

    Re: Extract Date from String

    Thank you very much Bebo
    Much appreciated
    Cheers
    OP

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,786

    Re: Extract Date from String

    Quote Originally Posted by Croweater View Post
    The post I was replying to and quoting was AT LEAST 4 posts previous!
    You don't need to quote at all unless you need to draw attention to something specific in the post you are quoting or your post would not make sense without it. In this case, there was no ambiguity - your post makes complete and perfect sense without the quote, so no need for the clutter.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  15. #15
    Spammer
    Join Date
    10-23-2012
    Location
    Adelaide, Australia
    MS-Off Ver
    Excel 2003, Office 365
    Posts
    1,237

    Re: Extract Date from String

    Yet you quote my post AliGW, when your post too, makes perfect sense without it.

  16. #16
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,786

    Re: Extract Date from String

    Only because the thread is solved - otherwise I'd have PMd you. Try not to be so grumpy about the place.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Can't extract date from a string
    By mz.mat in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-18-2022, 06:12 AM
  2. [SOLVED] How to extract date from middle of text string and add one month to the date.
    By JulianS96 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-02-2021, 07:36 AM
  3. Extract date from string
    By acandan21 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-29-2021, 04:46 PM
  4. [SOLVED] How can I extract a date from a string?
    By Tooley in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-06-2017, 11:21 AM
  5. VBA to extract date from string
    By saravanan1981 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-09-2016, 11:33 AM
  6. Extract date from string
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-01-2011, 08:39 AM
  7. extract date from string
    By kdp145 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-03-2006, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1