Save CSV without carriage return (CR)

cutiedanger

New Member
Joined
Sep 17, 2014
Messages
2
I am attempting to create a comma-separated text file that will be used to import data into a third-party program. The program requires that there not be carriage returns (CR) at the end of each line.

I have attempted to save as CSV (Comma delimited) (*.csv), CSV (Macintosh) (*.csv), and CSV (MS-DOS) (*.csv), but all are adding carriage returns at the end of each line.

MDMp2Jz.png


I found this macro, but it does not seem to work either.

Code:
Sub Save_As_CSV_No_CR()
    Range("CJ1").Select

    Dim myRng As Range
    Set myRng = ActiveCell.EntireColumn
    myRng.Replace What:=vbCr, Replacement:="", _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
    Set myRng = Nothing

    ChDir "C:\Users\cutiedanger\Desktop"
    ActiveWorkbook.SaveAs Filename:= _
        "C:\Users\cutiedanger\Desktop\test-file.txt", FileFormat:=xlCSV, _
        CreateBackup:=False
End Sub

Is there a way to save the file as a comma delimited text file without carriage returns?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
While I'd like to save it in Excel without CRs, this file will ultimately end up being processed with batch. I can use Dos2Unix to convert the file to Unix format.

Code:
dos2unix test-file.txt
 
Upvote 0
Couple Options:

Code:
Sub Creat_Txt_File()

myfile = "c:\test.csv"
If Dir(myfile) <> "" Then Kill myfile ' deletes file if it exists

For r = 1 To Range("A65536").End(xlUp).Row
    lcol = Cells(r, 256).End(xlToLeft).Column
    For c = 1 To lcol
    delim = ""
    If c < lcol Then delim = ","
    data = data & Cells(r, c) & delim
    Next c
Open myfile For Append As #1
Print #1, data
Close #1
data = ""
Next r
or
Code:
End Sub
Sub outputtotextfile()
wline = ""

For r = 1 To Range("A65536").End(xlUp).Row
    lcol = Cells(r, 256).End(xlToLeft).Column
    wline = wline & Join(Application.Transpose(Application.Transpose(Range("A" & r).Resize(, lcol))), ",") & vbNewLine
Next r
    
    

    Open "C:\test2.txt" For Output As #1 'Replaces existing file
    Print #1, wline
    Close #1

End Sub
 
Upvote 0
or
Code:
Sub M_snb()
   ThisWorkbook.Sheets(1).UsedRange.Copy
  
    With GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
          .GetFromClipboard
          c00 = Replace(Replace(.GetText, vbCr, ""), vbTab, ",")
    End With
    CreateObject("scripting.filesystemobject").createtextfile("G:\OF\example.txt").write c00
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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