• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Break line on txt file is not desired

Dear Chandoo Team

This part of my code will save an excel file as txt file.

I select the range to be copied (in excel) to a new Workbook. This workbook will be saved as txt file. The range will always be a string with 15 columns and 1, 2 or 3 lines.

After being saved as txt file, this creates an extra break line after the pasted string. But i don't want that...

Can you help, please? Thank you


>>> use code - tags <<<
Code:
Sub Gravar_TXT()

Dim wb As Workbook
Dim saveFile As String
Dim RangeTXT As Range
On Error Resume Next

Dim wbEmail As Workbook, strGetFilename As String 'abrir ficheiro "encomenda"
strGetFilename = Application.GetOpenFilename(, , "Open Import Workbook")

Dim wsEmail As Worksheet, wsTXT As Worksheet

Set wbEmail = Workbooks.Open(strGetFilename)
Set wsTXT = wbEmail.Sheets("TXT")
  With wbEmail
   wsTXT.Activate

xTitleId = "Selecionar Range para incluir no ficheiro TXT"
Set RangeTXT = Application.Selection
  Set RangeTXT = Application.InputBox("Selecionar Range", xTitleId, RangeTXT.Address, Type:=8)
   Application.ScreenUpdating = False
    Application.DisplayAlerts = False
   
Set wb = Application.Workbooks.Add
RangeTXT.Copy
  With wb.Worksheets(1).Range("A1")
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial Paste:=xlPasteValuesAndNumberFormats

  End With
    saveFile = Application.GetSaveAsFilename(fileFilter:="Text Files (*.txt), *.txt")
     wb.SaveAs FileName:=saveFile, FileFormat:=xlText, CreateBackup:=False
      wb.Close
   
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End With
End Sub
 
Last edited by a moderator:
Search for "FreeFile" in the forum. You'll find few example for exporting data into text file.

That will eliminate the unwanted CrLf (Carriage return/linefeed character) at the end.
 
Back
Top