Excel newline (char(10)) copy/paste issue - excel

I'm trying to use CHAR(10) to create some nice formatting for some scripting I am generating, but copying and pasting is not working for me. I am running Windows 8.1 atm. Here's a clarifying example. Using the formula:
="hello"&CHAR(10)&"world"
I get hello[newline]world in Excel. Copying/pasting into Notepad, I would expect
hello
world
But when I actually perform that copy/paste, I instead get:
helloworld
Now, I know that the newline is in there, because if I copy that output from notepad into this very window, I get the newline reinserted. I ultimately need to c/p out from Excel into plaintext with (visible) line breaks. Any ideas how I can accomplish this?

A "newline", in Windows, is typically a carriage return followed by a line feed. Those are two separate character codes: 13 & 10, respectively.
So your formula in Excel would need to be:
="hello"&CHAR(13)&CHAR(10)&"world"

Related

Using RPGLE what hex characters do I use to simulate a keyboard ALT/Enter

I have several comments that need to go into one EXCEL cell and I want them broken out as each comment. If I'm using EXCEL and I want to perform this, I would use ALT and ENTER. What HEX characters do I use to make this happen?
So if you are using Apache POI to create your spreadsheet, you insert an x'25' which is a newline character. I believe that you also have to set the cell to word wrap to make this work. This code is an EBCDIC new line code, Java converts that to an ASCII new line for you.

Add programatically a line break that will work on every version

I'm writing an excel file using javascript (the language doesn't really matter) and came across the following issue...
I need to write a cell with line breaks. I have read that I just need to add CHAR(10) to my formula and voila! However, I will have to send this file to different people using different language versions of Excel. Therefore if a French opens the sheet, he will see an error because CHAR(10) should be CAR(10) same for other languages...
Is it possible to insert a line break programatically so it works on every versions?

Convert line endings when copying from excel to word

Is there an elegant / correct way to deal with CRLF line endings when copying from Excel to Word?
We have some text stored in a database that uses \r\n (i.e. CRLF) for new lines. We use a tool[1] to query the database and pull the values into Excel. It looks fine in Excel but if we then copy a cell into Word there are two line breaks where there should only be one.
For example the string This is line #1\r\nThis is line #2. looks fine in excel but if we copy from Excel and paste into Word it's 3 lines long.
I've got a macro that removes the CR but it's a bit of a nasty hack and I'd rather not push it out to all users if there is a better solution.
Sub UpdateLineEndings()
'
' UpdateLineEndings Macro
'
ActiveCell.Value = Replace(Selection.Text, vbCr, "")
End Sub
[1] We're useing Sharperlight but I'm sure there are lot's of similar tools out there.
Thanks for that #tim-williams I had been hoping there was a way to do it when copy / pasting (a bit like the "keep text only" option).
As you said, I have to do a replace somewhere, I ended up doing it in the SQL of the Sharperlight data model. The column was text, so I cast it as varchar and then did the replace.
REPLACE(CAST({_Table.Alias}.COMMENTS as varchar(MAX)), CHAR(13)+CHAR(10), CHAR(10))
If you want to make your comment as an answer I'm happy to mark it as correct.

Importing txt files to excel makes linebreaks disappear

I am trying to import a text file into excel (2007). The file was exported from a C# text box and it contains linebreaks. Although when I import it (with the text import wizard that comes with excel), the linebreaks disappears completely. I would prefer not to have to write a VBA file and place in an excel file to run but instead change this with a neat method in C#, before it turns the text box data into a txt file. Is this possible in any way?
I figured this out. If you put quotes around text any embedded line feeds (ASCII 010) will be imported into Excel as embedded line feeds. In other words, these line feeds will not cause the text to split across Excel rows.
Try it. Create two files in Notepad.exe. In the first terminate the first line by pressing Alt-0010:
Test line 1 terminated with alt-0010
Test line 2
In the second, begin lines with " and terminate with ". For the first line insert an Alt-0010 just before the ":
"Test line 1 terminated with alt-0010 prior to the quote"
"Test line 2"
Now import both into Excel and see the difference.
See IETF RFC 4180 for more information
In excel a line break within a cell is encoded as ascii code 10 (i.e. \n) (determined through the handy use of the macro recorder and inspection of the generated VBA). I think the 'disappearance' of new lines probably is a result of you're C# emitting \n\r, so you might try doing a global replacement of "\r" with "" in your C# code before outputting it.
Thanks for the help! I tried starting the text with \" and finish with \" but when I go to excel, I get each line in a separate cell and my hopeful plan was to get all the text in one single excel cell.

import text file containing line breaks into excel

I have a plain text file looking like this:
"some
text
containing
line
breaks"
I'm trying to talk excel 2004 (Mac, v.11.5) into opening this file correctly. I'd expect to see only one cell (A1) containing all of the above (without the quotes)...
But alas, I can't make it happen, because Excel seems to insist on using the CR's as row delimiters, even if I set the text qualifier to double quote. I was sort of hoping that Excel would understand that those line breaks are part of the value - they are embedded in double quotes which should qualify them as part of the value. So my Excel sheet has 5 rows, which is not what I want.
I also tried this Applescript to no avail:
tell application "Microsoft Excel"
activate
open text file filename ¬
"Users:maximiliantyrtania:Desktop:linebreaks" data type delimited ¬
text qualifier text qualifier double quote ¬
field info {{1, text format}} ¬
origin Macintosh with tab
end tell
If I could tell Excel to use a row delimiter other than CR (or LF), well, I'd be a happy camper, but excel seems to allow the change of the field delimiter only, not the row delimiter.
Any pointers?
Thanks,
Max
Excel's open
Looks like I just found the solution myself. I need to save the initial file as ".csv". Excel honors the line breaks properly with CSV files. Opening those via applescript works as well.
Thanks again to those who responded.
Max
The other option is to create a macro to handle the opening. Open the file for input, and then read the text into the worksheet, parsing as you need, using a Range object.
If your file has columns separated by list separators (comma's, but semicolons for some non-English region settings), rename it to .csv and open it in Excel.
If your file has columns separated by TABs, rename it to .tab and open it in Excel.
Importing (instead of opening) a csv or tab file does not seem to understand line feeds in between text delimiters. :-(
Is it just one file? If so, don\'t import it. Just copy paste the content of your text file into the first cell (hit f2, then paste).
If you absolutely must script this, Excel actually uses only one of those two chars (cr, lf) as the row delimiter, but I'm not sure which. Try first stripping out the lf's with an external util (leave the cr's) and then import it... if that does't work, strip out the cr's (leave the lf's) and thenimport it.

Resources