How to insert CR before text pattern #:## in Excel cell text

0

I have large amounts of text in cells of a multi-row Excel spreadsheet. The text blocks in the cells are broken up by time stamps, e.g., “16:43” or, in some cases “4:43 PM”. I'd like to parse for the time stamps and insert a CHR(10) before the time stamps.

Before:

3:33 AM Waiting for customer permission to remote control desktop.3:33 AM Remote Control successfully initiated.3:35 AM Remote Control ended.3:36 AM Remote Control successfully initiated.3:40 AM The connection to the customer was lost. This session will reconnect if and when the customer reestablishes a network connection.3:40 AM Disconnected (Applet)3:40 AM Remote Control ended.3:45 AM Connecting to: control.app02-01.l

After:

3:33 AM Waiting for customer permission to remote control desktop.
3:33 AM Remote Control successfully initiated.
3:35 AM Remote Control ended.3:36 AM Remote Control successfully initiated.
3:40 AM The connection to the customer was lost. This session will reconnect if and when the customer reestablishes a network connection.
3:40 AM Disconnected (Applet)
3:40 AM Remote Control ended.3:45 AM Connecting to: control.app02-01.l

OKS

Posted 2017-04-04T20:04:14.767

Reputation: 11

(1) Why did you modify your example to have blank lines between the lines of text? Is that what you want? If so, please [edit] the question to say so.  (2) You missed a couple of time stamps in your example data.  (3) Can we assume a maximum number of time stamps in each cell?  (4) Have you tried anything?  What?  (5) What’s the big picture of how you want to use this functionality? Do you want to do it (a) once, (b) occasionally, on demand, or (c) continually (i.e., the output should change immediately whenever the input changes)?  (6) Do you have any preference of worksheet function vs. VBA? – Scott – 2017-04-04T23:26:52.210

Answers

0

This small UDF() scans an input string backwards, looking for a colon.

When it finds a colon, it inserts a CHR(10) in the appropriate spot before the colon:

Public Function Fragment(sIN As String) As String
    Dim L As Long, i As Long
    Dim temp As String, CH As String
    L = Len(sIN)
    temp = ""
    For i = L To 1 Step -1
        CH = Mid(sIN, i, 1)
        If CH <> ":" Then
            temp = CH & temp
        Else
            temp = CH & temp
            i = i - 1
            temp = Mid(sIN, i, 1) & temp
            i = i - 1
            CH = Mid(sIN, i, 1)
            If IsNumeric(CH) Then
                temp = Chr(10) & CH & temp
            Else
                temp = CH & Chr(10) & temp
            End If
        End If
    Next i
    Fragment = temp
End Function

enter image description here

It can handle both one and two digit hours.
Make sure the output cell is formatted with wrap on.
Another approach is to use Regex.

Gary's Student

Posted 2017-04-04T20:04:14.767

Reputation: 15 540