Carriage Return in Notepad - excel

I have between 1-2 thousand notepad files that I need to add a new line to. I have an excel macro that can automatically find and replace text in notepad files, which I can use to add in the text I need. The excel macro has one cell where the user types the text to be found, and another where the user types the text that will replace that text. The problem is, I need to replace one line with two, and putting in a linebreak in the 'replace with' cell in excel (using alt-enter) does not put the text on a new line in notepad.
Interestingly, when I open the notepad file in Word, it does show up on a new line, with a carriage return between the two lines, but is still on the same line in notepad. Is there any way that I can use the excel macro to add the carriage return to show up in notepad?

ALT+Enter will only put a line feed into the string.
Notepad does not understand the "UNIX" style of encoding, but more advanced programs do.
if you replace the line feed with a full DOS newline, you should find your problem goes away:
NewString=Replace(OldString,vbLf,vbCrLf)
vbLf is the excel constant for the line feed.
vbCrLf is the excel constant for the DOS newline.

Related

In Interop.Excel, the Worksheet.SaveAs has a CSV option. How can I use this if my *.xlsx file has line feeds in some cells (Alt+Enter)

Writing a c# app to digest some old spreadsheets. When reading an Excel file that contains line feeds (by using Alt-Enter while in a cell), I can't seem to use "Worksheet.SaveAs" to save a CSV file. It treats the linefeed as a carriage return and a new cell.
Interop.Excel should recognize the linefeeds and remove them during the save.
Sample Table and result

Viewing a txt file with row separator "\x00"

I got a program that outputs a txt with column separator "\t" and row separator "\x00" (hex code)
But when I open the txt with MS Excel, Notepad++, LibreOffice, all the contents are put in one row.
But I want to open this in either MS Excel or LibreOffice in the "normal" view so that I can edit it easily.
I tried to find some type of buttons in LibreOffice to change the separator but I couldn't.
I changed encoding of it using Notepad++ to all encodings, but changing encoding didn't help at least at notepad++.
How can I open this file with row separator actually being displayed as it should?
I want to see this in multiple rows and edit it efficiently.
In Notepad++, you can perform a Find and Replace (ctrl+h).
Set the Search Mode to Extended
For the Find what, enter \\x00
For the Replace with, enter \n
Hit Replace All
Then to replace the \t with tabs, you can:
For the Find what, enter \\t
For the Replace with, enter \t
Hit Replace All

Import txt file with line breaks into Excel

while working on an export to Excel I discovered the following problem.
If you create a table where one cell has a line break and you save the document as a txt file it will look like this:
"firstLine<LF>secondLine"<TAB>"secondColoumn"
When I open this file in Excel the line break is gone and the first row has only one cell with the value firstLine
Do you know if it is somehow possible to keep the line breaks?
EDIT: Applies to Excel2010. Don't know if other versions behave different.
EDIT2: Steps to reproduce:
Open blank excel sheet
Enter text (first column with line break, second colum not important)
Save as Unicode Text (txt) // all other txt don't work as well
Close Excel file
File->Open
No changes in the upcoming dialog.
The excel file has now 2 rows which is wrong.
I was finally able to solve the problem! yay :D
CSV:
The german Excel needs a semicolon as a separator. Comma doesn't work.
Note: This is only true when the file is encoded as UTF-8 with BOM at the beginning of the file. If it's ASCII encoded comma does work as a delimiter.
TXT:
The encoding has to be UTF-16LE. Also it needs to be tab delimited.
Important:
The files will still be displayed incorrect if you open them with the "File->Open" dialog and "import" them. Draging them into Excel or opening with double click works.
It isn't a problem - in the sense of expected behaviour - this is inherent when you save text as Unicode or as Text (tab delimited)
If you save the file as unicode and then either
Open it in Notepad
Import it in Excel
you will see that the cells with linebreaks are surrounded by ""
The example below shows two linebreaks
A1 has an entry separated using Alt+Enter
B1 has an enry using the formula CHAR(10)
The picture also shows what notepad sees on a saved Unicode version
Suggested Workaround 1- Manual Method
In Excel, choose Edit>Replace
Click in the Find What box
Hold the Alt key, and (on the number keypad), type 0010
Replace with a double pipe delimiter
Save as Unicode
Then reverse the process when needed to reinsert the linebreaks
This can be done easily in VBA
Suggested Workaround 2 - VBA alternative
Const strDelim = "||"
Sub LBtoPIPE()
ActiveSheet.UsedRange.Replace Chr(10), strDelim, xlPart
ActiveSheet.UsedRange.Replace "CHAR(10)", strDelim, xlPart
End Sub
Sub PIPEtoLB()
ActiveSheet.UsedRange.Replace strDelim, Chr(10), xlPart
ActiveSheet.UsedRange.Replace strDelim, "CHAR(10)", xlPart
End Sub

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