Inserting carriage returns into text strings of varying lengths

pfisher99

New Member
Joined
Jun 14, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello - We are working on a CRM data migration project and I would like to insert a carriage return to create separate rows from a text string in excel. The export from the old CRM did not maintain the format of our notes, and instead can only export into a continuous text string. I would like to have them in their original format with carriage returns between each note.

Any help would be much appreciated, thanks!

An illustrative example of what I am looking to format below:

CURRENT FORMAT:
3/6/24: PF reach out to Craig to schedule lunch 2/7/24: PF reach out to Craig to schedule lunch 1/31/24: PF spoke with Craig at conference

WOULD LIKE TO APPEAR IN THE BELOW FORMAT:
3/6/24: PF reach out to Craig to schedule lunch
2/7/24: PF reach out to Craig to schedule lunch
1/31/24: PF spoke with Craig at conference
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try on a copy.
VBA Code:
Sub InsertLineBreaks()
    Dim regex As Object
    Dim matches As Object
    Dim inputText As String
    Dim outputText As String
    Dim match As Object
    Dim lastRow As Long
    Dim ws As Worksheet
    Dim data As Variant
    Dim i As Long
 
    Set ws = ThisWorkbook.Sheets("Sheet1") 'change sheetname as needed
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    data = ws.Range("A2:A" & lastRow).Value 'assumes data starts in A2
 
    Set regex = CreateObject("VBScript.RegExp")
    regex.Global = True
    regex.IgnoreCase = True
    regex.pattern = "(\d{1,2}/\d{1,2}/\d{2}:\s)"
    For i = 1 To UBound(data, 1)
        If Not IsEmpty(data(i, 1)) Then
            inputText = data(i, 1)
            outputText = inputText
            Set matches = regex.Execute(inputText)
            For Each match In matches
                outputText = Replace(outputText, match.Value, vbLf & match.Value, 1, 1)
            Next match
            If Left(outputText, 1) = vbLf Then
                outputText = Mid(outputText, 2)
            End If
            data(i, 1) = outputText
        End If
    Next i
 
    'Output to column B
    ws.Range("B2:B" & lastRow).Value = data
    ws.Range("B2:B" & lastRow).WrapText = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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