Pasting data with line returns, line returns not initializing?

ruinedelf

New Member
Joined
Dec 6, 2023
Messages
35
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. MacOS
Hiya, got a bit of a question! I have some data that is copied and pasted around. However, for some reason, line returns aren't processed until the cell formula is actually entered: Even selecting the cell and copying it around doesn't trigger the line break.

Is there any way of forcing all cells in a worksheet to re-initialize so all line breaks are processed? Ideally I would like a VBA script for this as part of my automation setup, but I figured I'd first try and find out what's causing this to happen.

Thanks!
 

Attachments

  • Line Returns 2.png
    Line Returns 2.png
    4.2 KB · Views: 19
  • Line Returns.png
    Line Returns.png
    5.2 KB · Views: 19

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
For more info, I've been using a VBA script to detect line returns and replace them with spaces:

VBA Code:
Sub ReplaceLineReturns()
    Dim targetRow As Integer
    Dim cell As Range

    ' Set the target row where you want to check for paragraph breaks
    targetRow = 61 ' Change this to your desired row number
   
    ' Loop through each cell in the specified row
    For Each cell In Rows(targetRow).Cells
        ' Check if the cell is not empty and contains a paragraph break
        If Not isEmpty(cell.Value) Then
            If InStr(1, cell.Value, vbLf) > 0 Then
                ' Replace paragraph breaks with spaces
                cell.Value = Replace(cell.Value, vbLf, " ")
            End If
        End If
    Next cell
End Sub

It works when the line break has taken effect, but not when it hasn't (it works in Line Returns 2.png, not in Line Returns.png). A bit of an interesting thing. Another funny thing is that "actioning" the line break can actually be undone (Ctrl+Z). Not sure what's actually going on here.
 
Upvote 0
Where the line return doesn't seem to work. What happens when you go turn Wrap Text on in the cell ?
Assuming that works then in your macro that copies in the data you can use something like this:
Rich (BB code):
Range("A2:C10").WrapText = True
 
Upvote 0
Turning on Wrap Text wraps the text without processing the line return. Please see attached image, thanks!
 

Attachments

  • LR3.png
    LR3.png
    5 KB · Views: 13
Upvote 0
This is still confusing me, I've tried a script that appends and then removes a space to all the cells, but while it does do that, it doesn't actually initialize the cell to trigger the line return. Any other ideas?
 
Upvote 0
Just to test, here's the MiniSheet in XL2BB format. The issue doesn't carry over directly, instead the line break is kind of ignored by the BBCode and the parse completes properly. I've attached pictures of what the original data in Excel and the BBCode look like. Note that Word Wrap is on for all cells.

Upload Test Sheet.xlsx
IJKLMNOPQRSTUV
1Date analysedTRH C6 - C9TRH C6 - C10vTPH C6 - C10 less BTEX (F1)BenzeneTolueneEthylbenzenem+p-xyleneo-XyleneNaphthaleneTotal +ve XylenesSurrogate aaa-TrifluorotolueneDate extractedDate analysed
Sheet1
 

Attachments

  • LR4.png
    LR4.png
    8.8 KB · Views: 15
  • xl2bb.png
    xl2bb.png
    34.5 KB · Views: 23
Upvote 0
Found the solution. The carriage return is not CHAR(10) as I thought it was, it was CHAR(13). Targeting the right special character solved it!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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