+ Reply to Thread
Results 1 to 97 of 97

VBA/Function for complex date difference

  1. #1
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    VBA/Function for complex date difference

    Hi,

    Can anyone help me with a VBA or function to calculate the difference between 2 or many more dates all grouped in a cell?

    Based on the attached workbook and how the data is structured(all the time) it would be useful for me to get the following data:

    a) How many days(or hours) have passed since creation of the Case and the date of the first comment of the Handler (date of creation of the Case = all the times the first date from the bottom, the Creator's first comment date).
    b) How many days(or hours) have passed between each subsequent comment of the Handler.
    c) How many comments have been totally placed by the Handler for that case.

    Finally, it would be excellent to not count weekend days and if time passed is less than 1 day - show the number of hours.

    Appreciate your help
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,176

    Re: VBA/Function for complex date difference

    the data cannot be used with this format. Every text should have its own record. Not shared in 1 cell.
    youd have to run a process to strip the existing layout into correct data records.

  3. #3
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Can we have a VBA to do exactly what you suggested?:

    Extract/split/strip any number of characters between "2022....." until the next sequence of "2022....." but also considering that some might start with "2020" or "2021".

    But then again, this should be a horizontal split because I've already tested something vertically and it pumps up the nr of lines from 30K to 600K.
    Last edited by AliGW; 03-18-2022 at 03:19 AM. Reason: PLEASE don't quote unnecessarily!

  4. #4
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Any help on this please? I guess it can be achieved with some sort of combination of SPLIT, for, do, MID, etc. but I have no clue

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Can you give us the details of your desired results and how you calculate?

  6. #6
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Basically what I want to calculate is mentioned here in D2, E2, F2. Can you help me with a VBA or function to achieve this?

    Or in a simpler way: I need to extract each sequence of the text between "2022-...Comments)" so I can do a count/difference afterwards. Horizontally

    Attachment 772941
    Last edited by AliGW; 03-18-2022 at 03:20 AM. Reason: PLEASE don't quote unnecessarily!

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Your attachment is not valid.

    Since you didn't show us your desired result and how it should be calculated, this is only my guess.

    1) Select D2:F2
    2) enter formula
    =GetAll(C2,"Creator","Handler")
    Confirm with Ctrl + Shift + Enter (array formula entry)
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    I have re-attached the sample. Let me know if this is fine now and if your code fits the sample?
    Attached Files Attached Files
    Last edited by AliGW; 03-18-2022 at 03:20 AM. Reason: PLEASE don't quote unnecessarily!

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    That's that workbook exactly the same as the one in your post#1.

    Have you tested the UDF I posted?

  10. #10
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon, the first two results are not correct:

    - In D2 I get "2" days whereas if you look at the workbook, the actual difference is around 1.2 days.
    - In E2 I get "2,3,3" days whereas if you look, the Handler only commented twice after his first comment with a difference of 1.6 days, respectively 2 days.
    - In F2 I get "3 days". Correct.

    Can you adjust the code?

    Or as an alternative , can you think of a code which could extract horizontally all the occurrences of "2022....Comments)"? This way I might be able to easily do some counts and calculations afterwards.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    That's why I asked you to show me the result that you want.
    I don't want to do a guess work, so if you upload a workbook with your Exact result, I will think about it.

  12. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,169

    Re: VBA/Function for complex date difference

    I took the liberty of updating Jindon's code:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  13. #13
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon, I have attached the sample workbook with my desired results in yellow.

    Option B would be the most suitable for me. Looking forward to see your solution.

    Thank you!
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,169

    Re: VBA/Function for complex date difference

    A significant change in your requirements.
    Last edited by JohnTopley; 03-18-2022 at 03:30 PM.

  15. #15
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,169

    Re: VBA/Function for complex date difference

    Based on Jindon's function approach

    Please Login or Register  to view this content.
    but if you have a large number of Cases a standard subroutine will be better as the number of entries per case will (surely) vary.
    Attached Files Attached Files
    Last edited by JohnTopley; 03-18-2022 at 03:37 PM.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Option B
    No array formula
    D2
    =IFERROR(GetAll($C2,COLUMN(A1)),"")
    Copy to the right as you want.
    Please Login or Register  to view this content.
    Last edited by jindon; 03-18-2022 at 05:21 PM.

  17. #17
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon, this is exactly what I need, but it works only if I apply it in Sample2.xlsx

    It does not work and it returns blank if I try to apply it in any other workbook (I need it to work in a workbook where "Comments" column is AU)

    Can you please help with adjusting this?
    Last edited by tyxanu; 03-19-2022 at 11:08 AM.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    It should look like
    =IFERROR(GetAll($AU2,Column(A1)),"")

  19. #19
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    I've used it like that and it returns a blank cell. Any clue?
    Last edited by tyxanu; 03-19-2022 at 11:29 AM.

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Did you also copy the code to the said workbook then I need to see your wokbook.

  21. #21
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Quote Originally Posted by jindon View Post
    Did you also copy the code to the said workbook then I need to see your wokbook.
    I think I know what is causing this error: each cell starts with 2 blank lines first. Forgot to add them in C2.

    Can you now adjust the code to take in consideration those 2 line breaks? They are located all the time at the very bottom of C2, just below the initial comment.
    Last edited by tyxanu; 03-19-2022 at 11:45 AM.

  22. #22
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Quote Originally Posted by jindon View Post
    Did you also copy the code to the said workbook then I need to see your wokbook.
    Reattached the Sample including the 2 line breaks in C2. Looking forward to see your code adapted to this
    Attached Files Attached Files

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Then
    1) Now the formula doesn't need IFERROR function, so just
    =GetAll($C2,COLUMN(A1))
    2) change the code to
    Please Login or Register  to view this content.
    Last edited by jindon; 03-19-2022 at 11:58 AM.

  24. #24
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon, this is working splendid now. Can I have one final wish so I can have more flexibility when importing this in PBi?

    Based on your same logic can you adjust the code to only show:

    -in D2: the date of the first comment
    -in E2: the name of the person who placed the first comment
    -in F2: the date of the last comment(most recent one)
    -in G2: the name of the person who placed the last comment(most recent one)

    (Regardless of the number of comments in C2)

  25. #25
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    D2:
    =OptionX($C2,COLUMN(A1))
    Please Login or Register  to view this content.

  26. #26
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,169

    Re: VBA/Function for complex date difference

    Can you please update your profile as I am sure you are not on excel 2007.

  27. #27
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Yes, forgot

  28. #28
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    MS-Off Ver 365
    Just enter
    D2:
    =GetAll(C2)
    or
    =OptionX(C2)
    Please Login or Register  to view this content.

  29. #29
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon,

    Is it possible to update your code one more time? Change in requirement relates to:

    - Column G: date of the most recent comment of the Handler (even tho the latest comment generally is not the Handler's)
    - Column H: name of the Handler who gave this comment

    I have also added Column C which might help evaluate the condition.

    Looking forward to your knowledge
    Attached Files Attached Files
    Last edited by tyxanu; 03-25-2022 at 12:20 PM.

  30. #30
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    E2:
    =OptionZ(D2)
    Please Login or Register  to view this content.

  31. #31
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Here are my results in this new Sample. I have 2 scenarios in which the code does not properly evaluate:

    In case AB125 - it does not evaluate that the Handler is the only who commented and returns "0", "0" in G and H. I would need to return HandlerZ's most recent comment from 2022-03-16 22:03:16
    In case AB126 - it returns Michael's comment as the most recent comment, which I don't need. I would only need HandlerZ's most recent comment from 2022-03-16 16:45
    Attached Files Attached Files

  32. #32
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    If col.C comes in play then change to
    Please Login or Register  to view this content.
    P.S
    D2;
    =OptionZ(C2,D2)
    Last edited by jindon; 03-26-2022 at 05:06 AM.

  33. #33
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Results are still not showing right.

    I have attached a new Sample to make this more easy:

    -repositioned the data exactly as in my official file
    -highlighted the actual results vs desired results so you can easily analyze

    Is it possible to update this so it matches the Desired Results?

    Many thanks for your support!

    PS: don't forget about the 2 breaks at the very bottom of each cell in AU. They will always be there
    Attached Files Attached Files
    Last edited by tyxanu; 03-26-2022 at 07:31 AM.

  34. #34
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Few time in your result are strange...
    change to
    Please Login or Register  to view this content.

  35. #35
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon,

    I have changed my laptop and MS got updated. Now the function does not seem to work properly and extracts only the Date of the First Comment. I have attached this new Sample with Actual Results shown vs my Desired Results.

    Can you look into this issue? Does it have something to do with Regional Settings in Windows?

    No change in requirements. Everything should be as previously discussed in the latest reply above (link should be done with column J2).
    Attached Files Attached Files
    Last edited by tyxanu; 04-14-2022 at 03:48 AM.

  36. #36
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Your formula is now

    =@optionz(J2,AU2)

    Remove @ mark.

  37. #37
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    But if you look at my Sample7, I did use the =optionz(J2,AU2) and it's not populating like in Desired Results. It's only retrieving the date.
    Last edited by tyxanu; 04-14-2022 at 04:32 AM.

  38. #38
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    See sheet2
    Attached Files Attached Files

  39. #39
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Here is the way I apply it. Am I doing something wrong?
    Attached Files Attached Files

  40. #40
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    No use of uploading the workbook without code.

  41. #41
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Sorry, here it is again.

    Steps which I apply:

    1. Apply VBA code from your reply on 03-26-2022, 11:47 AM. This is the final one and the most suitable.

    2. I type =OptionZ(J2,AU2) and hit CTRL+SHIFT+ENTER to make it an Array Formula like you also did above (however, previously I was not doing the Array part and everything was working fine).

    3. Drag the formula down.
    Attached Files Attached Files

  42. #42
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    This UDF is made for Office365 or the version with spill functionality.

    So, if you enter formula =optionz(J2,AU2) in AW2, it automatically expand the needed columns to AX2, AY2 or AZ2.

    If you using earlier version you need to
    1) Select AW2:AZ2
    2) enter formula
    =optionz(J2,AU2)
    and confirm with Ctrl + Shift + Enter (array formula entry)

  43. #43
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Hello Jindon!

    I am back after quite some time with a small extra requirement to the above code

    Is it possible to adjust the code to also extract the actual comment that was placed?

    Attached my sample showing the desired extra result in yellow.
    Attached Files Attached Files

  44. #44
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    try change to
    Please Login or Register  to view this content.

  45. #45
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Forgot to mention that comments can also include line breaks or can also include multiple lines of text.

    I've reattached the new scenario. Result is only partial. Can the rest of the comment be captured too?
    Attached Files Attached Files

  46. #46
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Please Login or Register  to view this content.

  47. #47
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Seems that it's working only for text on multiple lines, but not for text with empty line breaks (see Handler's comment)

    Attached partial results.
    Attached Files Attached Files

  48. #48
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Upload a workbook with all the possible combinations of data and result that you want.

  49. #49
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Attached.
    I tried to show all the possible desired combinations, but there are too many. However, in the end, they all = same logic -> Columns AW and AZ should contain the comment placed by the targeted persons, regardless of how they write their comments(with line breaks, spaces, more than 2 lines, paragraphs split by spaces or line breaks, etc).

    Also, don't forget that at the end of each cell in AU there will always be by default: 2 empty line breaks(spaces).
    Attached Files Attached Files
    Last edited by tyxanu; 07-12-2022 at 11:32 AM.

  50. #50
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Change to
    Please Login or Register  to view this content.

  51. #51
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    You are a God!

    Thank you!

  52. #52
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Hello Jindon,

    Can you help me one more time with another requirement for the attached workbook? Desired result is shown on the right and preferably the second one.

    a) purpose is to extract/count all the dates in the chronological order only for 2022 (in which they are listed anyway in each cell)
    b) There is no clear standard form. Sometimes "_" is the link and pattern, but sometimes there is no "_" between text and the next date
    c) date is always in the dd/mm/yyyy format
    Attached Files Attached Files

  53. #53
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    I don't recall original function, so this is only for the data in last attached workbook,

    B3
    =GetDateAndCount(A3,2022)
    then copy down

    I3
    =GetDateAndCount(A3,2022,TRUE)
    then copy down
    TRUE returns counts, FALES or omitted, all dates.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by jindon; 01-24-2023 at 11:07 AM.

  54. #54
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Yes, this is a complete new file.

    However, the function does not seem to work properly. Uploading a copy of the file with data very close to the actual file and the result.
    In my official file I get a lot of "0" and "#SPILL!".

    Is the function looking for any string of dd/mm/yyyy and not considering any other delimiters? Because it seems to not work properly.
    Also, I now see some variance of also mm/dd/yyyy. Can you adapt the code to also look for this variance?
    Attached Files Attached Files

  55. #55
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  56. #56
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    I am trying =GetDateAndCount(S2,2022) in my official file, but it populates the cells wrongly, as follows:

    Attachment 815096

  57. #57
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    No one can even guess how your "official file"looks like and your attachment in your last post is invalid, so can not open.

    Attach your workbook with ALL THE POSSIBLE COMBINATION of the data and the result that you want.

    No GUESS game anymore.

  58. #58
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Reattaching this example with code applied.

    Looking forward.
    Attached Files Attached Files

  59. #59
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Now your data is in table...

    Need to see your EXACT EXPECTED result.

  60. #60
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Reattached. Column B contains the result after applying the code.
    Starting Column C are the two desired results separated by border.

    (Please note that dates in red are auto-converted by Excel to dd/mm, but they should actually be mm/dd).
    Attached Files Attached Files

  61. #61
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Please Login or Register  to view this content.
    Attached Files Attached Files

  62. #62
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon,

    I have changed to: .Pattern = "(\d{1,2})/(\d{1,2})/(" & myYear & ")" and it's perfectly fine. I will further analyze all the 20K lines from official file.

    But which line should I change to get the same result only for 2023?

  63. #63
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    1) Add one line
    Please Login or Register  to view this content.
    2) change 2 of 2022 to myYear
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    3)
    change -2 to -1
    Please Login or Register  to view this content.
    Change myYear to suite.
    Last edited by jindon; 01-27-2023 at 07:48 AM.

  64. #64
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    You are a King!

    Seems to work properly. I'll get back in case any other discrepancies.

    Thank you!

  65. #65
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon,

    Can this code be adjusted to count & extract only unique values (dates) whenever there are duplicates/triplicates/quadruplicates/etc?

    Example and desired result(in yellow) in this attached file.
    Attached Files Attached Files

  66. #66
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Replace the function with the below
    Please Login or Register  to view this content.

  67. #67
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    This new code does not seem to give the desired result.

    Attaching the result.
    Attached Files Attached Files

  68. #68
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    You are missing main sub procedure, i,e, "test".

  69. #69
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    My mistake

    Thank you

  70. #70
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Hi Jindon,

    I have attached 2 samples with 2 Run-time error '9'.

    sample2a: the issue seems to rely on this text "Michael392/13/2023" which I should not take it in consideration anyway, but my official data is 200k+ lines and there many similar texts.
    sample2b: the issue seems to rely on this date "19/01/2023" which is in the wrong format (should have been inputted by the user as "01/19/2023" and it is impossible for me to manually test and CTRL+H all the problems)

    Question is:

    To cover both issues, can the code be adjusted so it can work as usual but:

    1. read and count dates only when:

    a) if a date is the starting text in the cell -> if it's followed by "_" (example "xx/xx/xxxx_...etc") and
    b) if a date is anywhere in the middle or the in the end -> if it's placed between "_" (example "etc..._xx/xx/xxxx_...etc")
    Anything outside this standard -> ignored

    2. read and count "dd/mm/yyyy" dates as "mm/dd/yyyy" ? (or code to perform the format change first, then read/count/work as usual)
    Of course, here I am not sure how the code can differentiate (for example) between 01/03 March vs 01/03 January ?

    Many thanks!
    Attached Files Attached Files
    Last edited by tyxanu; 03-03-2023 at 01:05 PM.

  71. #71
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Change the pattern to
    Please Login or Register  to view this content.

  72. #72
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    The conversion seems to work almost fine, except for a few issues which I've highlighted in the attached sample + expected result.

    a) "19/01" transforms into "01.09" which is wrong
    b) "_19/01/2023text" is taken in consideration but should not be, giving the conditions in above post (there is no "_" in the end after "2023")
    Attached Files Attached Files

  73. #73
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Replace the Function with
    Please Login or Register  to view this content.

  74. #74
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Jindon, I have tried your function but does not seem to change anything.

    See attached:
    Attached Files Attached Files

  75. #75
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Oops, I posted wrong one.

    Please Login or Register  to view this content.

  76. #76
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Still looking strange:
    Attached Files Attached Files

  77. #77
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Why do you expect text_19/01/2023_ to exracted as 01.19.2023 ?

    Obviously, it is not mm/dd/yyyy format, so the code is not extracting.

    If you expect like that, you should explain why.

  78. #78
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    For example in the below sample:

    a) cell A2: "_19/01/2023_" -> I would like the code to count this entry, but it should put it under January, not September. This is why I was curious if the code can somehow first switch the format to mm/dd/yyyy, then count and associate with the right month?

    - otherwise, can the code just differentiate and count all different format entries (mm/dd/yyyy and dd/mm/yyyy) and flag under the right month?

    b) cell A3: "_19/01/2023text " -> I need the code to ignore any entry like this one, because this one is missing the final "_"(correct format from the user should be "_19/01/2023_text").

    The most preferable result is highlighted under yellow. Is it possible?
    Attached Files Attached Files
    Last edited by tyxanu; 03-07-2023 at 06:05 AM.

  79. #79
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Then huge problem.

    if you want 19/01/2023 to be treated as 01/19/2023(mm/dd/yyyy)
    What about

    02/03/2023, 12/11/2023, 10/11/2023 etc.

    Need clear logic to read 19/01/2023 as 01/19/2023, and why not others?

    P.S And your latest attachment is not using the code posted in #75.
    Last edited by jindon; 03-07-2023 at 06:09 AM.

  80. #80
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    You are right.

    Since there is no technical way in which the code can distinguish between 01.03.2023 as 1st of March or 01.03.2023 as 3rd of January and any variation like this,

    Then can the code just capture all dates (that respect the standard format between "_"..."_") and flag under the right Month?

    At the moment with:

    a) post 71 - code is the closest code to expected result, but it is flagging 19/01/2023 under September, which is wrong and capturing "_19/01/2023text" which is against the standard.
    b) post 75 - code is not working at all right
    Attached Files Attached Files

  81. #81
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    The last line of your expected results is wrong, no Jan.
    Please Login or Register  to view this content.

  82. #82
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Seems fine now! Let me explore

  83. #83
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Hi again Jindon,

    The code has been working very fine so far, but I was wondering if you could give a new approach.

    Is it possible to adjust the code to perform 99% of what it has been doing so far, but:

    - to extract & count only the Dates where the standard text format is "_date_ALIAS_" and where ALIAS inside column B = ALIAS inside column A.
    - if ALIAS in column A and ALIAS in column B don't match -> skip count/skip extract.

    You can see the current vs desired result in the attached.

    Many thanks!
    Attached Files Attached Files
    Last edited by tyxanu; 08-02-2023 at 08:15 AM.

  84. #84
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Include Col.A in the table.
    Select B1 - [Table Design] - [Resize Table] change $B$1:$T$3 to $A$1:$T$3
    Please Login or Register  to view this content.
    Attached Files Attached Files

  85. #85
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Quote Originally Posted by jindon View Post
    The last line of your expected results is wrong, no Jan.
    Please Login or Register  to view this content.
    Hello again Jindon,

    Hope you are doing well!

    I would like to ask whether you could do a small adjustment to the version of the code to which I give the post reply now? (attached also)

    It would be awesome if you could make the code to do the same thing as of now, but:

    a) to count the dates only for the Month I select in "settings" sheet (desired result shown in "result" sheet).
    b) to count also the number of occurrences for the respective Month of the free texts that I add in "settings" sheet (desired result shown in "result" sheet).
    -example, if I select "January", for the line "..._02/10/2024_FREE TEXT FREE TEXT1_01/28/2024_FREE TEXT FREE TEXT1_...", the count of the text "FREE TEXT FREE TEXT1" must be 1, because in January it appears only once.

    All other rules from the code remain applicable.

    Many thanks!
    Attached Files Attached Files
    Last edited by tyxanu; 01-17-2024 at 09:13 AM.

  86. #86
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    change to
    Please Login or Register  to view this content.

  87. #87
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Quote Originally Posted by jindon View Post
    change to
    Please Login or Register  to view this content.
    Hi Jindon,

    Can you check the outcome? I see the code generated the result in a new sheet ("Your Result"), but it is not counting the text after I change it in "Settings".

    I need to be able to write whatever text in "Settings" and the code needs to count it.

    I have attached a sample as close as possible to my actual data and changed the free text in "Settings". I've also created a sheet with "My Desired Result".

    Thank you!
    Attached Files Attached Files

  88. #88
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    1)
    Please don't quote whole posts -- it's just clutter. If you are responding to a post out of sequence, limit quoted content to a few relevant lines that makes clear to whom and what you are responding

    2) Before we go with the workbook you last attached, you need to tell me if the code I posted in #86 is working or not with the workbook you attached in #85.
    If not, you need to tell me how it was not working. This is the first step to continue.

    3) What is your intention to change data/ data layouts time after time?
    If you think you can change/adjust the given codes please do so.
    If not, you should upload a workbook which is closesst to your actual data set contains all the possible combinstions of the data at first to save time for both ends.

    Otherwise mark this thread as "Solved" and open a new thread for the last one.

  89. #89
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    1) Ok, noted

    2) and 3)

    a) The text count function of code #86 is not working ok:

    The only issue: When I freely change the texts in G10-G29 from "Settings" sheet, it is not able to count the texts anymore.
    I am attaching a sample with code #86. It contains:

    - sheet "Settings" - here I need to be able to input free texts in G10 to G29. Based on what text I write here (any text I write), the code needs to identify the text in the data from "MyData" and count it.
    - sheet MyData - here I have put my data to be analyzed.
    - sheet "Your Result" - here is the result with code #86 after I change the texts in G10 to G29 in "Settings".
    - sheet "Desired Result" - here is how code #86 should reach the result.

    The texts from G10-G29 "Settings" will always be free texts, not case sensitive, not color sensitive. I might even type "Apples & Pears".

    The texts from G10-G29 "Settings" will always be located in "MyData" anywhere between two sequences of dates. Example:

    "02/13/2024_testTEXTdummy_DUMMY_ABCGHJ_texxt_01/29/2024_dummy text_tExt_text_ABCGHJ_01/15/2024_DUMMY TEXT_test text_ABCGHJ_text_text_01/03/2024_test_ABCGHJ_12/31/2023_testTEXT_ABCGHJ_" -> here the count of "ABCGHJ" should be 3, since the code will anyway count only 2024 January dates(based on my input in G8 in "Settings".

    b) The dates count function of code #86 is working ok.
    Attached Files Attached Files
    Last edited by tyxanu; 01-22-2024 at 07:24 AM.

  90. #90
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    So, the code worked for that workbook.
    You need to mention this first, otherwise your Excel is doing something different from mine.

    Change to
    Please Login or Register  to view this content.
    I can not even guess what the actual strings, but you need to order the list of "Free text to look for:" very carefully.
    Since your data have no clear logic to separate the dates and text to look for, the code needs to use Like operator.
    When the list contains very similar ones like "MyText", "MyText1", "MyText12", etc., the longer string should appear above the shorter one. Otherwise, the code will not work as you want.
    Last edited by jindon; 01-22-2024 at 11:10 PM.

  91. #91
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Ok, so this was making the difference: "the longer string should appear above the shorter one"

    You're a star!

    Seems to work. Let me further test.

  92. #92
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    I've found one last thing which is not working as expected (see attached).

    The code should apply the same rules as the former code when it comes to duplicates (post #81):

    - where there are duplicate dates, to count only once please. Also for text, where there are duplicate texts between the sequence of dates, to count only once.

    Example:

    02/03/2024_Bla bla text_Code123_
    01/19/2024_Bla bla text_Code123_
    01/19/2024_text TExT_TEXT_blaBLA_Code123_
    01/10/2024_Dummy text_dsgjdka_Code123_
    01/10/2024_Texting is nice_Text__Code12_


    Here the count and extraction should be:

    -January dates: 01/19/2024, 01/10/2024
    -Occurrences of "Code123" text: 2

    (Also, if it helps: dates are always going to be located between "_", except for the first date from the beginning of the cell which begins without "_" and it's ok)

    Thanks!
    Attached Files Attached Files
    Last edited by tyxanu; 01-23-2024 at 03:24 PM.

  93. #93
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    Let me clear;
    Dates are OK.

    Problem is finding "Free text to look for:" within a string.

    Look at the data you posted in #85.
    in A4

    01/28/2024_ALIAS2_FREE TEXT FREE TEXT1_TEXT_19/01/2024FREE TEXT2 23/914_
    12/23/2022_ALIAS1_text text text text text text_text text text text_


    No underscore after the date (19/01/2024FREE TEXT2).
    The code is written to match this case too.

    That's why I said the logic to find those string is NONE.

    So, you now must give me CLEAR logic to find those word from the string.
    Attached Files Attached Files

  94. #94
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    But in my latest post (#92) the problem is with dates and text (in case of duplicate dates and duplicate texts)

    Can you just adjust the code from #92 to count & extract duplicates only once? (you managed to apply this rule before in older posts).

    I am attaching this picture for better clarification. The result in the picture is with code from post #90(same #92).

    Red = not ok
    Green arrows = what should be the change = duplicates to be considered only once
    Green checkmarks = what is already ok and no need for change
    Attached Images Attached Images
    Attached Files Attached Files

  95. #95
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    You are not answering to my question.

    I said
    So, you now must give me CLEAR logic to find those word from the string.
    And I've never heard of DULPICATES.

    This is the last.

    Upload a workbook that shows all the possible variation of the data and the result that you want.
    Tell clear logic to match the "Free text to look for:".

    Otherwise, just wasting time...

  96. #96
    Registered User
    Join Date
    01-07-2020
    Location
    england
    MS-Off Ver
    365
    Posts
    79

    Re: VBA/Function for complex date difference

    Ok, attaching workbook with all the rules and instructions.

    I hope this helps you better, but let me know in case there are still doubts.

    Many thanks again
    Attached Files Attached Files

  97. #97
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,518

    Re: VBA/Function for complex date difference

    For the data provided.
    Please Login or Register  to view this content.

+ 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. complex date function
    By Bob Phillips in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 09:05 PM
  2. [SOLVED] complex date function
    By tjb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 PM
  3. complex date function
    By Bob Phillips in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 09:05 AM
  4. complex date function
    By tjb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2005, 08:05 AM
  5. [SOLVED] complex date function
    By tjb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 07:05 AM
  6. [SOLVED] complex date function
    By Bob Phillips in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2005, 06:05 AM
  7. complex date function
    By tjb in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 11:05 PM
  8. [SOLVED] complex date function
    By tjb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-30-2005, 12:05 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