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

Bond00

Board Regular
Joined
Oct 11, 2017
Messages
142
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!
 
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?


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?
your Date_Time_v4 code also gives that # output along with the times now, so was wondering if that can go to col K like it was before instead of col H like in your example.

Also any additional data would always go at the bottom. so it would be added to so you wouldnt need to worry about anything mixed in the middle. You could probably do a scan of the rows starting at D16 finding the first row in D col that has a # and then make that the range or something?
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You could probably do a scan of the rows starting at D16
OK, so I am assuming headers in row 15 - see 'Const' line in the code

Try this with a copy of your data

VBA Code:
Sub Date_Time_v5()
  Dim a As Variant, b As Variant, Bits As Variant, t As Variant
  Dim i As Long
  Dim ampm1 As String, ampm2 As String
  
  Const HdrRow As Long = 15 '<- Header row
  
  a = Range("D" & HdrRow + 1, Range("D" & Rows.Count).End(xlUp).Offset(1)).Value
  i = 1
  Do Until IsNumeric(Left(a(i, 1), 1)) Or i = UBound(a)
    i = i + 1
  Loop
  If i < UBound(a) Then
    With Range("D" & HdrRow + i, Range("D" & Rows.Count).End(xlUp).Offset(1))
      a = .Value
      ReDim b(1 To UBound(a), 1 To 5)
      For i = 1 To UBound(a) - 1
        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(, 4)
        .Value = b
        .Columns(2).NumberFormat = "d-mmm"
        .Columns(3).Resize(, 2).NumberFormat = "h:mm AM/PM"
        .Columns(8).Value = Application.Index(b, 0, 5)
        .Columns(8).NumberFormat = "0.00"
      End With
    End With
  End If
End Sub
 
Upvote 0
OK, so I am assuming headers in row 15 - see 'Const' line in the code

Try this with a copy of your data

VBA Code:
Sub Date_Time_v5()
  Dim a As Variant, b As Variant, Bits As Variant, t As Variant
  Dim i As Long
  Dim ampm1 As String, ampm2 As String
 
  Const HdrRow As Long = 15 '<- Header row
 
  a = Range("D" & HdrRow + 1, Range("D" & Rows.Count).End(xlUp).Offset(1)).Value
  i = 1
  Do Until IsNumeric(Left(a(i, 1), 1)) Or i = UBound(a)
    i = i + 1
  Loop
  If i < UBound(a) Then
    With Range("D" & HdrRow + i, Range("D" & Rows.Count).End(xlUp).Offset(1))
      a = .Value
      ReDim b(1 To UBound(a), 1 To 5)
      For i = 1 To UBound(a) - 1
        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(, 4)
        .Value = b
        .Columns(2).NumberFormat = "d-mmm"
        .Columns(3).Resize(, 2).NumberFormat = "h:mm AM/PM"
        .Columns(8).Value = Application.Index(b, 0, 5)
        .Columns(8).NumberFormat = "0.00"
      End With
    End With
  End If
End Sub
sorry wasn't able to test it for a few days there. but yeah thats perfect, everything works great now. Thanks a lot for all the help with this!

and yes headers on row 15, but whats that affect? just for knowing the starting row # is 16?
 
Upvote 0
Thanks a lot for all the help with this!
You are welcome. Glad we seem to have got there in the end. :)


and yes headers on row 15, but whats that affect? just for knowing the starting row # is 16?
No significant difference really, it is just that my thinking (& code) were based on what happens below the header row rather than what happens on the first row and below.
Also, in the forum when you are told what the first row is, sometimes it turns out to be the row that the headers are on and sometimes it turns out to be the first row of data below the headers. I was just clarifying that as well. :cool:
 
Upvote 0

Forum statistics

Threads
1,223,876
Messages
6,175,123
Members
452,614
Latest member
MRSWIN2709

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