+ Reply to Thread
Results 1 to 26 of 26

Database report returns different date formats sometimes,I need to cover both formats(MAC)

  1. #1
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Database report returns different date formats sometimes,I need to cover both formats(MAC)

    THIS IS EXPLICIT FOR MAC, I did not clarify this initially.

    I get a CSV file from a reporting system and I take some parts of that data and paste it to another Excel file where a macro uses a formula to get the difference from one date provided from the CSV compared to the date today.

    Cell L19 is where the data from the CSV file is going

    Some days the data looks like this:

    04/03/2016 00:00 , for this, the following formula works > =IF(OR(ISERROR(I19),ISBLANK(I19)),"", DATEDIF(L19,TODAY(),"Y"))

    At other days the data looks like > 04/03/2016 10:34 AM and the following formula works:

    =IF(OR(ISERROR(I3),ISBLANK(I3)),"", DATEDIF(DATE(RIGHT(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),4),LEFT(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-1),MID(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))+1,FIND("/",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-1)),TODAY(),"Y"))

    Is there a way to cover both cases in the same Cell formula?
    Attached Files Attached Files
    Last edited by ExcellentVBA; 05-02-2022 at 10:03 AM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Database report returns different date formats sometimes, I need to cover both formats


    If you are just comparing the date without the time why just not just use LEFT or MID worksheet function ?

    As here it's the VBA section so not the Excel formulas one how do you import the source file ?
    If under VBA so why not doing the necessary within the import procedure ?

  3. #3
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes, I need to cover both formats

    I import it from a CSV file and if I try that via Left fn, I cannot get it to work, I am not sure why.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes, I need to cover both formats

    Do you want a formula or VBA? If the former, I will move the thread for you.

    There are instructions at the top of the page explaining how to attach your sample workbook.

    A good sample workbook has just 10-20 rows of representative data that has been desensitised. It also has expected results mocked up, relevant cells highlighted and a few explanatory notes.
    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.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes, I need to cover both formats

    This should work in both cases:

    =IF(OR(ISERROR(I19),ISBLANK(I19)),"", DATEDIF(ABS(L19),TODAY(),"Y"))

    It will pull only the date part where a time is also present.
    Attached Files Attached Files
    Last edited by AliGW; 05-02-2022 at 04:39 AM. Reason: Workbook attached.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Database report returns different date formats sometimes, I need to cover both formats


    In addition to Ali' suggestion if you are looking for a VBA import procedure
    I will need a source csv file and accordingly the exact expected result workbook …

  7. #7
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes, I need to cover both formats

    Quote Originally Posted by AliGW View Post
    Do you want a formula or VBA? ...
    Either will do, VBA or Formula, the whole procedure is based on VBA, this is just one component.

    I have uploaded a CSV file to the first post named SourceData.CSV , the Columns A and C are exactly how I get the data, unfortunately, your formula > =IF(OR(ISERROR(I19),ISBLANK(I19)),"", DATEDIF(ABS(L19),TODAY(),"Y"))
    did not work with it.

    Maybe I should say that I work on a MAC, it might be important.
    Last edited by AliGW; 05-02-2022 at 05:44 AM. Reason: PLEASE don't quote unnecessarily!

  8. #8
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes, I need to cover both formats

    Quote Originally Posted by Marc L View Post

    In addition to Ali' suggestion if you are looking for a VBA import procedure
    I will need a source csv file and accordingly the exact expected result workbook …
    Thanks for that, I have uploaded a CSV file to the first post named SourceData.CSV

    All I need is to get a return value in years comparing todays date with the report data.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes, I need to cover both formats

    I have uploaded a CSV file to the first post named SourceData.CSV
    Nope - nothing there.

    Have you tried my suggestion in post #5?

    Please don't quote WHOLE posts - it's just clutter.

    Maybe I should say that I work on a MAC, it might be important.
    Crucial! Please update your forum profile to add "for Mac" after your Office version information.
    Last edited by AliGW; 05-02-2022 at 05:56 AM.

  10. #10
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes, I need to cover both formats

    @AliGW and Marc L, sorry, the uploaded CSV source file is up now, the submission hadn't worked. I edited the main post to say "for MAC"

  11. #11
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Try this:

    =DATEDIF(DATE(--MID(A2,FIND(" ",A2)-4,4),--LEFT(A2,FIND("/",A2)-1),--SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),C2,"Y")

    What do you want to happen if the date in column C is BEFORE the one in A?

    Maybe this:

    =IFERROR(DATEDIF(DATE(--MID(A2,FIND(" ",A2)-4,4),--LEFT(A2,FIND("/",A2)-1),--SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),C2,"Y"),-DATEDIF(C2,DATE(--MID(A2,FIND(" ",A2)-4,4),--LEFT(A2,FIND("/",A2)-1),--SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),"Y"))

    I edited the main post to say "for MAC"
    But I asked you to update your forum profile: Change it from Office 2019 to Office 2019 for Mac, please. Thanks.
    Attached Files Attached Files
    Last edited by AliGW; 05-02-2022 at 06:29 AM. Reason: Workbook attached.

  12. #12
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,791

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    @Ali,

    the DATE function can work with string values, so there is no need to coerce the year, month and day parameters to numbers using --, i.e.:

    =DATEDIF(DATE(MID(A2,FIND(" ",A2)-4,4),LEFT(A2,FIND("/",A2)-1),SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),C2,"Y")

    Hope this helps.

    Pete

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    @Pete

    That's what I thought, but it wasn't working wihtout the --, which is why I added it in.

  14. #14
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,791

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    It worked for me - maybe it is a quirk of XL365 ?

    Pete

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Dunno - I'm not going to worry, as it wpn't do any harm if it's there, anyway.

    We need to hear back from the OP before going any further, anyway.

    EDIT: Weird! It IS working now!
    Attached Files Attached Files
    Last edited by AliGW; 05-02-2022 at 06:42 AM.

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

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    For the profile update:
    Attached Images Attached Images

  17. #17
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Thanks Ali and Pete, I have now finally updated the profile as requested.

    Now I need to clarify the procedure a bit.

    The uploaded CSV file is not how I get the source report, I get the source report on some days with the format in column A and on other days with the format of column C.

    I need a formula that I can insert on my own report generating template file into a call which covers both formats, returning the difference between today and the registration date, format agnostic, so to say. I do not have a reference to both columns, the csv file always comes in the format as in column A or C.

    So, on the days where the source csv file send this value > 04/03/2016 00:00

    The following formula works > =IF(OR(ISERROR(I19),ISBLANK(I19)),"", DATEDIF(L19,TODAY(),"Y"))

    On the days where the source csv file send this value > 04/03/2016 10:34 AM and the following formula works:

    =IF(OR(ISERROR(I3),ISBLANK(I3)),"", DATEDIF(DATE(RIGHT(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),4),LEFT(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-1),MID(SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1),FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))+1,FIND("/",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-FIND("$",SUBSTITUTE(LEFT(L3,FIND(" ",L3)-1),"/","$",1))-1)),TODAY(),"Y"))

    I have simply included both examples of source formats into the same file here, sorry if that caused any confusion.

    The inconsistency of the reports is random, but it is always wither one of these formats.

  18. #18
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    The CSV file I looked at had only one format.

    We seem to be back at square one: you don't appear to have tried anything I've suggested, which is a bit disappointing.

    However, if you provide a workbook with BOTH date formats included, I'll tell you how to adapt my suggestion. I am NOT working with your unnecessarily long-winded version - sorry.

  19. #19
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Hi Ali,

    Apologies, I should have said the values provided by the csv reports rather than the format.

    I had tried these:

    =IF(OR(ISERROR(I19),ISBLANK(I19)),"", DATEDIF(ABS(L19),TODAY(),"Y")) << return Value error

    =DATEDIF(DATE(MID(A2,FIND(" ",A2)-4,4),LEFT(A2,FIND("/",A2)-1),SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),C2,"Y") returns value error

    same here with this

    =IFERROR(DATEDIF(DATE(--MID(A2,FIND(" ",A2)-4,4),--LEFT(A2,FIND("/",A2)-1),--SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),C2,"Y"),-DATEDIF(C2,DATE(--MID(A2,FIND(" ",A2)-4,4),--LEFT(A2,FIND("/",A2)-1),--SUBSTITUTE(MID(A2,FIND("/",A2)+1,2),"/","")),"Y"))

  20. #20
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    I asked for an updated workbook showing this.

    Please provide the workbook YOU are working with (desensitised) with your attempts in place.

    Without this, I am sorry, but I won't be offering any further assistance.

  21. #21
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Thanks for that, I have uploaded a sanitized template XLSM file "shareable" now.

    Columns L3-L6 show the data value that I import from the CSV reports , while columns L7-L9 show the values that I sometimes get from the same reports for the reg Date.

    The logic runs in O3-O9, you can see the formulas and which works with which date value/format.

    My goal is to have a logic which covers both values > 4/28/2013 12:00:00 AM and 05/01/2020 00:00 and returns the difference in years.

    Thanks a lot for the help.

  22. #22
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    In O3 copied down:

    =IFERROR(DATEDIF(DATE(MID(L3,FIND(" ",L3)-4,4),LEFT(L3,FIND("/",L3)-1),SUBSTITUTE(MID(L3,FIND("/",L3)+1,2),"/","")),TODAY(),"Y"),DATEDIF(L3,TODAY(),"Y"))
    Attached Files Attached Files
    Last edited by AliGW; 05-02-2022 at 10:16 AM.

  23. #23
    Registered User
    Join Date
    04-16-2022
    Location
    uk
    MS-Off Ver
    Office 2019 for Mac
    Posts
    22

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    @AliGW , here is your well deserved rep points, you deserve even more for the patience, this is truly a magical solution, a whole BI dept could not come up with.

    this helps a great deal. If I may ask why this part > DATEDIF(DATE(MID(L3,FIND(" ",L3)-4,4),LEFT(L3,FIND("/",L3)-1),SUBSTITUTE(MID(L3,FIND("/",L3)+1,2),"/","")) makes it work and what exacly it does.

    Thanks a million.

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

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Thanks for the rep. No magic involved!

    a whole BI dept could not come up with.
    LOL!

  25. #25
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    I'll explain:

    DATE(

    MID(L3,FIND(" ",L3)-4,4), - finds the space in the text string and returns the 4 characters before it (year)

    LEFT(L3,FIND("/",L3)-1), - finds the first / in the text string and returns the character(s) that preceed it (month)

    SUBSTITUTE(MID(L3,FIND("/",L3)+1,2),"/","") - finds the first / in the text string and returns the 2 characters after it - the substitute gets rid of the trailing / if it's a one-digit number (day)

    )

    The DATE function requires 3 arguments: year, month and day.

  26. #26
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,247

    Re: Database report returns different date formats sometimes,I need to cover both formats(

    Any further questions, just shout.

+ 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. Replies: 1
    Last Post: 10-21-2020, 07:49 AM
  2. [SOLVED] VBA debug - Creating conditional formats that cover a range
    By The_Snook in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2016, 09:26 AM
  3. Replies: 4
    Last Post: 10-01-2014, 03:18 PM
  4. [SOLVED] PasteSpecial with number formats but without conditional formats or borders
    By bodhi808 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-18-2013, 09:58 AM
  5. Problem copying Page formats and print formats to another workbook
    By Chrisgeni in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-27-2013, 08:05 AM
  6. [SOLVED] Copying formats - column widths, formats, outlining to worksheets
    By DavidBr318 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-17-2005, 09:05 AM
  7. Replies: 1
    Last Post: 01-15-2005, 03:06 PM

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