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.
Related
Does anyone know why Excel 2013 is posting the last number in the row below when I paste this text in a cell ;
075680 TYSNES 95759753
95759753
Paste it without the previous format rules:
try to:
go in insertion mode (double click on your target cell)
Paste the content using the short cut "ctrl+shift+v"
In order to clean-up your data source from non-printable characters there are already some question about it. I think that this can explain everything you need. I can simply replace all of them with nothing and that's it.
I have a requirement in excel where i want to copy each text that ends with pulistop into a row of another sheet. ex:
Setting up the configuration.
Creating environment.
Pushing the tasks.
Now assume that above text is in one cell and i want to copy each sentence that ends with pulistop into individual rows of a new sheet. like below.
Setting up the configuration.
Creating environment.
Pushing the tasks.
Please help me in doing this as i have many no. of sheets which needs this modification.
Thanks in advance.
Chakri.
Check if this can be useful for you:
Copy the cell(s) into clipboard (Ctrl+C).
Paste into Notepad or similar text editor. Note: in Notepad the lines will appear next to each other, but nevermind, the line jumps are still there.
Replace in text editor all " (double quotes) with nothing.
Copy the whole text of the editor.
Paste into Excel.
This works in my case. If you have many cells like this next to each other, with this method you can process all at once with the same effort. Will this work for you?
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
I have an XLS file creates a CSV file with a macro on Excel 2003, I have 40+ columns and the last 3 ones are optional, therefore there are a lot of empty values on the XLS, when I run the exporting subroutine it wont put a trailing comma on all of the rows, why ? Because : http://support.microsoft.com/kb/77295 -.-
In Microsoft Office Excel, if you save a file in the text or in the CSV (comma separated value) format, Excel puts tabs or commas between each column of the worksheet. However, certain text files may be saved with a different number of tabs or commas in 16-row blocks.
here is their suggested work around:
To make sure that Excel saves tab or comma delimiters for all empty columns, verify that the last column in the file contains some data in at least every 16 rows throughout the file. If the blocks of rows do not contain data, add spaces or other characters in every 16 rows to the cells in the last column, or reorder the columns in the worksheet so that the last column on the worksheet always contains information.
-.- way to go microsoft ! -.-
Ok so my main issue is that the generated file will be parsed by another program out of my scope which needs that specific format as it is right now I'm adding a blank every 16 row if the field is empty, this seems to do it but Data Processing Deparment is complaining about that white space... can you believe them!?
Anyway I also tried to add a flag and remove it with the find function, but ofc when you save the file it will take away the delimiters again...
thanks for reading my history ;p
Any suggestions ?
Edit: Unfortunately using Excel is must, the data is manually inputted through the excel sheet by different users, the vba codes does a lot more like template generation etc.. this is the only issue that I'm having and it is not feasible to change the whole process.
Manual example;
Dim r As Range: Set r = Range("A1:C10") '// or ActiveSheet.UsedRange for everything
Dim buffer As String, delimiter As String, c As Range
Dim i As Long
For Each c In r
If (i Mod r.Columns.Count) = 0 Then
If (Len(buffer)) Then delimiter = vbCrLf
Else
delimiter = ","
End If
buffer = buffer & delimiter & """" & CStr(c.Value) & """" '//or c.text to preserve formatting
i = (i + 1)
Next
Open "c:\xxx.csv" For Output As #1
Print #1, buffer
Close #1
If you don't have commas in your data, it would be very easy to write a text reader that goes through and counts the number of columns in a line and just add commas to the end if there aren't enough. It's not ideal, but it's probably easier than writing a custom Excel to CSV converter.
Simplest way:
ActiveWorkbook.SaveAs "MyFileNameAndDir.csv", xlCSV, Local:=True
Suggestion 1) Stop using Excel to generate a CSV file.
What's your data source? Where does Excel get the data from? Perhaps you can do something with the original data source to generate a CSV instead of using Excel as the middleman. If you like really big hammers, Biztalk is the ultimate MS tool for data input/output manipulation. Few can pull that one out of their toolbox, but throwing together a custom c# program to generate a CSV file can be done in a couple hours.
In short: If at all possible, use a better tool!
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.