Possible to pull apart 1st section of string for date & time

Bond00

Board Regular
Joined
Oct 11, 2017
Messages
140
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
So people type notes in short hand, and I'd like to be able to have a button linked to vba to auto pull apart the first date and time part of the text and put them into the proper cells shown below.
So this list in Col D will go down to an unknown row # so maybe use LRow = ws2.Range("B138").End(xlUp).Row to get the bottom row to work with. (this is in worksheet2)
Example:
Book1
ABCDEFGHI
1IDCusTicketDescriptionDateStart TimeAM/PMEnd TimeAM/PM
210/8 2-530pm Work on system checks, fixed any errors found8-Oct2:00PM5:30PM
310/9 11am-230pm Work on system repairs and firmware upgrades9-Oct11:00AM2:30PM
410/9 230-5pm Work on reviews9-Oct2:30PM5:00PM
510/10 930-1130am Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to main server.10-Oct9:30AM11:30AM
610/10 1130-3pm Review all system projects and collect data10-Oct11:30AM3:00PM
Sheet1


So if they type 10/8 2-530pm put the date into col E and start time being 2pm into col F and PM into col G and 530 into col H and PM into col I
Now it can be assumed (almost always) that if the first # doesn't have a AM or PM with it that its the same as the 2nd # in this case above its 2-530pm so 2 is also PM by assumption.

There is occasional times when its like 10/10 1130-3pm and the 11:30 is assumed AM in this case since they didn't work from 1130pm yesterday until 3pm today. So maybe a rule that if the math is over 12 hrs then assume the correct time from that. in this case assume its AM since if it was 11:30PM-3PM it would be over 12 hrs span.
If this rule is ever broken for some reason then its fine we just correct it manually since it will almost never happen.

Also when the button is pressed and the date and time is put into the proper cells remove the first part and place the rest of the text back into col D (without the date and time in front)

thanks for taking a look at this!
 
Ah, I see. No, it didn't need an extra part as both times had an am/pm they were already collected and used (like in cell D3 of your original post)



I'm pretty sure the code will work unless a work period exceeds 12 hours, which I assumed unlikely.
Yeah its good how it is now, that one post i made above is unlikely to ever happen.

Also i get the decimal # of time from those rows, is this the best way/most efficient formula for that? this is in row16
Excel Formula:
=IF(F16<>"",SUM(MOD(TIMEVALUE(TEXT(H16, "h:mm")&" "&I16)-TIMEVALUE(TEXT(F16, "h:mm")&" "&G16),1)*1440)/60,"")
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
ah one other little issue i found, it doesn't handle 12pm noon correctly.

Book1
DEFGHI
3111/22 12-4pm fixed stuff22-Nov12:00AM4:00PM
3211/22 1215-4pm fixed stuff22-Nov12:15AM4:00PM
3311/22 1230-4pm fixed stuff22-Nov12:30AM4:00PM
Sheet1
 
Upvote 0
Also i get the decimal # of time from those rows, is this the best way/most efficient formula for that? this is in row16
Hmm, I wondered from early on about the wisdom of putting the AM/PM in a separate column, but it was what you had asked for. Do you really need that?
If the Start/End times could be displayed as below, many of the calculations, including the hours calculation would be easier. Also, why not have the macro calculate those hours. You did not specify where that formula was so below I have put those results in col H. If they need to be in a different column then that could be arranged.

To help check, I have included my sample data in column C. So before the code, that same data was also in column D.
Would this be any use?

VBA Code:
Sub Date_Time_v4()
  Dim a As Variant, b As Variant, Bits As Variant, t As Variant
  Dim i As Long
  Dim ampm1 As String, ampm2 As String
  
  With Range("D2", Range("D" & Rows.Count).End(xlUp))
    a = .Value
    If IsNumeric(Left(a(1, 1), 1)) Then
      ReDim b(1 To UBound(a), 1 To 5)
      For i = 1 To UBound(a)
        Bits = Split(Replace(Replace(a(i, 1), "-", " "), "/", " "), , 5)
        ampm1 = ""
        b(i, 1) = Bits(4)
        b(i, 2) = DateSerial(Year(Date), Bits(0), Bits(1))
        ampm2 = UCase(Right(Bits(3), 2))
        t = Replace(Bits(3), ampm2, "", , , 1)
        b(i, 4) = TimeValue(IIf(Len(t) < 3, t, Format(t, "00:00")) & ampm2)
        If UCase(Right(Bits(2), 1)) = "M" Then ampm1 = UCase(Right(Bits(2), 2))
        t = Replace(Bits(2), ampm1, "", , , 1)
        b(i, 3) = TimeValue(IIf(Len(t) < 3, t, Format(t, "00:00")) & IIf(ampm1 = "", ampm2, ampm1))
        If b(i, 3) > b(i, 4) And ampm1 = "" Then b(i, 3) = b(i, 3) + IIf(ampm2 = "AM", 0.5, -0.5)
        b(i, 5) = 24 * IIf(b(i, 3) > b(i, 4), b(i, 3) - b(i, 4), b(i, 4) - b(i, 3))
        If b(i, 5) > 12 Then b(i, 5) = 24 - b(i, 5)
      Next i
      With .Resize(, 5)
        .Value = b
        .Columns(2).NumberFormat = "d-mmm"
        .Columns(3).Resize(, 2).NumberFormat = "h:mm AM/PM"
        .Columns(5).NumberFormat = "0.00"
      End With
    End If
  End With
End Sub

Bond00.xlsm
CDEFGH
1OriginalDescriptionDescriptionDateStart TimeEnd TimeHrs
210/8 2-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct2:00 PM5:30 PM3.50
310/9 11am-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
410/9 230-5pm Work on reviewsWork on reviews9-Oct2:30 PM5:00 PM2.50
510/10 930-1130am Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to main server.Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to main server.10-Oct9:30 AM11:30 AM2.00
610/10 1130-3pm Review all system projects and collect dataReview all system projects and collect data10-Oct11:30 AM3:00 PM3.50
710/8 2-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct2:00 PM5:30 PM3.50
810/9 11am-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
910/9 230-5pm Work on reviewsWork on reviews9-Oct2:30 PM5:00 PM2.50
1010/10 930-1130am Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to mainserveWorked on installing local program accounts and called service to get software downloads, after install setup attached its database to mainserve10-Oct9:30 AM11:30 AM2.00
1110/10 1130-3pm Review all system projects and collect dataReview all system projects and collect data10-Oct11:30 AM3:00 PM3.50
1210/8 8-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct8:00 AM5:30 PM9.50
1310/9 11-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
1410/9 230-1am Work on reviewsWork on reviews9-Oct2:30 PM1:00 AM10.50
1510/9 230pm-1am Work on reviewsWork on reviews9-Oct2:30 PM1:00 AM10.50
1610/10 630-1130am Worked on installing local program accounts and called service toWorked on installing local program accounts and called service to10-Oct6:30 AM11:30 AM5.00
1710/10 1045-8pm Review all system projects and collect dataReview all system projects and collect data10-Oct10:45 AM8:00 PM9.25
1810/10 230pm-1am Worked on installing local program accounts and called service toWorked on installing local program accounts and called service to10-Oct2:30 PM1:00 AM10.50
1910/10 4-2am Review all system projects and collect dataReview all system projects and collect data10-Oct4:00 PM2:00 AM10.00
2011/22 12-4pm fixed stufffixed stuff22-Nov12:00 PM4:00 PM4.00
2111/22 1215-4pm fixed stufffixed stuff22-Nov12:15 PM4:00 PM3.75
2211/22 1230-4pm fixed stufffixed stuff22-Nov12:30 PM4:00 PM3.50
2311/22 1230-4am fixed stufffixed stuff22-Nov12:30 AM4:00 AM3.50
2411/22 12-4am fixed stufffixed stuff22-Nov12:00 AM4:00 AM4.00
Sheet4
 
Upvote 0
Hmm, I wondered from early on about the wisdom of putting the AM/PM in a separate column, but it was what you had asked for. Do you really need that?
If the Start/End times could be displayed as below, many of the calculations, including the hours calculation would be easier. Also, why not have the macro calculate those hours. You did not specify where that formula was so below I have put those results in col H. If they need to be in a different column then that could be arranged.

To help check, I have included my sample data in column C. So before the code, that same data was also in column D.
Would this be any use?

VBA Code:
Sub Date_Time_v4()
  Dim a As Variant, b As Variant, Bits As Variant, t As Variant
  Dim i As Long
  Dim ampm1 As String, ampm2 As String
 
  With Range("D2", Range("D" & Rows.Count).End(xlUp))
    a = .Value
    If IsNumeric(Left(a(1, 1), 1)) Then
      ReDim b(1 To UBound(a), 1 To 5)
      For i = 1 To UBound(a)
        Bits = Split(Replace(Replace(a(i, 1), "-", " "), "/", " "), , 5)
        ampm1 = ""
        b(i, 1) = Bits(4)
        b(i, 2) = DateSerial(Year(Date), Bits(0), Bits(1))
        ampm2 = UCase(Right(Bits(3), 2))
        t = Replace(Bits(3), ampm2, "", , , 1)
        b(i, 4) = TimeValue(IIf(Len(t) < 3, t, Format(t, "00:00")) & ampm2)
        If UCase(Right(Bits(2), 1)) = "M" Then ampm1 = UCase(Right(Bits(2), 2))
        t = Replace(Bits(2), ampm1, "", , , 1)
        b(i, 3) = TimeValue(IIf(Len(t) < 3, t, Format(t, "00:00")) & IIf(ampm1 = "", ampm2, ampm1))
        If b(i, 3) > b(i, 4) And ampm1 = "" Then b(i, 3) = b(i, 3) + IIf(ampm2 = "AM", 0.5, -0.5)
        b(i, 5) = 24 * IIf(b(i, 3) > b(i, 4), b(i, 3) - b(i, 4), b(i, 4) - b(i, 3))
        If b(i, 5) > 12 Then b(i, 5) = 24 - b(i, 5)
      Next i
      With .Resize(, 5)
        .Value = b
        .Columns(2).NumberFormat = "d-mmm"
        .Columns(3).Resize(, 2).NumberFormat = "h:mm AM/PM"
        .Columns(5).NumberFormat = "0.00"
      End With
    End If
  End With
End Sub

Bond00.xlsm
CDEFGH
1OriginalDescriptionDescriptionDateStart TimeEnd TimeHrs
210/8 2-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct2:00 PM5:30 PM3.50
310/9 11am-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
410/9 230-5pm Work on reviewsWork on reviews9-Oct2:30 PM5:00 PM2.50
510/10 930-1130am Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to main server.Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to main server.10-Oct9:30 AM11:30 AM2.00
610/10 1130-3pm Review all system projects and collect dataReview all system projects and collect data10-Oct11:30 AM3:00 PM3.50
710/8 2-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct2:00 PM5:30 PM3.50
810/9 11am-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
910/9 230-5pm Work on reviewsWork on reviews9-Oct2:30 PM5:00 PM2.50
1010/10 930-1130am Worked on installing local program accounts and called service to get software downloads, after install setup attached its database to mainserveWorked on installing local program accounts and called service to get software downloads, after install setup attached its database to mainserve10-Oct9:30 AM11:30 AM2.00
1110/10 1130-3pm Review all system projects and collect dataReview all system projects and collect data10-Oct11:30 AM3:00 PM3.50
1210/8 8-530pm Work on system checks, fixed any errors foundWork on system checks, fixed any errors found8-Oct8:00 AM5:30 PM9.50
1310/9 11-230pm Work on system repairs and firmware upgradesWork on system repairs and firmware upgrades9-Oct11:00 AM2:30 PM3.50
1410/9 230-1am Work on reviewsWork on reviews9-Oct2:30 PM1:00 AM10.50
1510/9 230pm-1am Work on reviewsWork on reviews9-Oct2:30 PM1:00 AM10.50
1610/10 630-1130am Worked on installing local program accounts and called service toWorked on installing local program accounts and called service to10-Oct6:30 AM11:30 AM5.00
1710/10 1045-8pm Review all system projects and collect dataReview all system projects and collect data10-Oct10:45 AM8:00 PM9.25
1810/10 230pm-1am Worked on installing local program accounts and called service toWorked on installing local program accounts and called service to10-Oct2:30 PM1:00 AM10.50
1910/10 4-2am Review all system projects and collect dataReview all system projects and collect data10-Oct4:00 PM2:00 AM10.00
2011/22 12-4pm fixed stufffixed stuff22-Nov12:00 PM4:00 PM4.00
2111/22 1215-4pm fixed stufffixed stuff22-Nov12:15 PM4:00 PM3.75
2211/22 1230-4pm fixed stufffixed stuff22-Nov12:30 PM4:00 PM3.50
2311/22 1230-4am fixed stufffixed stuff22-Nov12:30 AM4:00 AM3.50
2411/22 12-4am fixed stufffixed stuff22-Nov12:00 AM4:00 AM4.00
Sheet4
hmm yeah, i guess it isn't as important now the way it was before now with this code. There is some other things that read off of that data too from that format, but the reason it was separated like that originally was to make it easier for entering in the data. Like they could just type 1: Tab and it would enter in 1:00 and then type A or P tab and it would auto put in AM or PM etc.. so it was just faster that way than typing in the full dates and times into 1 cell since typing in full times can be finicky with the format etc.
A16:L16 is the starting row

Also not sure if it matters much. but does this clear the variables at the end?
Erase a ' Each element set to Empty/Free memory used by array
Erase b
Erase Bits

But well i guess if you're going to do that, this is what the whole thing looks like in this sheet.
Book1
ABCDEFGHIJKL
37IDCustomerTicket #Work DescriptionDateTime StartAMTime EndAMRangeQtyInternal Notes
381Text description etc..29-Sep11:00AM2:30PM11:00-2:30PM3.5
392Text description etc..29-Sep2:30PM5:00PM2:30-5:00PM2.5
Sheet2
Cell Formulas
RangeFormula
J38:J39J38=IF(K38>12,TEXT(F38, "h:mm")&G38&"-"&TEXT(H38, "h:mm")&I38,TEXT(F38, "h:mm")&"-"&TEXT(H38, "h:mm")&I38)
K38:K39K38=IF(F38<>"",SUM(MOD(TIMEVALUE(TEXT(H38, "h:mm")&" "&I38)-TIMEVALUE(TEXT(F38, "h:mm")&" "&G38),1)*1440)/60,"")


Also in the range i only show the AM/PM if its over 12 hrs range
1728726045864.png
 
Last edited:
Upvote 0
It also had the same issue if it ended with 12pm too making the am time into PM

Book1
DEFGHI
2210/9 11-1230pm Work on system repairs and firmware upgrades9-Oct11:00PM12:30PM
Sheet1
 
Upvote 0
I'm getting lost.

I thought the request was to break out elements from a longer text in col D. What does this have to do with that?
Like they could just type 1: Tab and it would enter in 1:00 and then type A or P tab and it would auto put in AM or PM et



Also not sure if it matters much. but does this clear the variables at the end?
No, but you can add that if you want. I have rarely found it to be an issue.
 
Upvote 0
I'm getting lost.

I thought the request was to break out elements from a longer text in col D. What does this have to do with that?
I was just answering you that's what was done before, that's why it was separated out (Time and the PM/AM in different cells) so it could be manually typed faster (in the past)
 
Upvote 0
If somebody has already typed it into column D (with other text) and it has then been split up, why would that data be over-written? And would that be common?

Anyway, are any of the codes of use to you?
 
Upvote 0
If somebody has already typed it into column D (with other text) and it has then been split up, why would that data be over-written? And would that be common?

Anyway, are any of the codes of use to you?
no, now that you made this code it wont need to be entered manually ever again probably. but yeah that last bit of code is useful. but can you make that go into col K since theres other things pulling from that column already. I dont see in your code where its specifying the col for some reason unless its the (i, 1)-(i, 6) and its just using an offset instead of actual col #?

Oh also can i move the if check here so it will still run and try to find any with a # in front in case more is added later after it was ran once already?
1728758661571.png
 
Upvote 0
but yeah that last bit of code is useful. but can you make that go into col K
Needs clarification. The code outputs 6 columns & that obviously cannot all fit into column K. Do you mean send the out to start at col K, thereby filling cols K:O or do you mean something else?

also can i move the if check here so it will still run and try to find any with a # in front
Definitely not. That would require a different code. How much different might depend on the answer to the first question above.
Would any additional data always just be added at the bottom or could you end up with lines that do not start with a digit and then lines that do and more lines that don't and then more lines that do etc?
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,103
Members
452,094
Latest member
Roberto Saveru

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