Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eclude lines in a TXT file, created by a excel macro VBA. 1

Status
Not open for further replies.

Dayene Dias

Programmer
Sep 4, 2017
2
BR
Hello!

I created a macro with VBA on EXCEL to export a table to a TXT file. But, I did a "for" function that returns all lines of my table to a TXT file; and in the end of that file, the function "Print" adds a plus line. I need to exclude this line, because when I use this file, it doesn´t work. My code is:

Sub Exportar_TXT()
'REPLICAR A FORMULA CONCATENADORA PARA TODAS AS LINHAS DA TABELA
'CONTAR QUANTAS LINHAS NA TABELA DE CLIENTES
linhas = Sheets("CLIENTES").Range("A9").CurrentRegion.Rows.Count - 1
Sheets("txt").Range("A10:iv64000").Clear
Sheets("txt").Range("A9:A" & 9 + linhas).FillDown
'EXPORTAR A PLANILHA TXT
NOME_ARQUIVO = ThisWorkbook.Path & "\" & Sheets("CLIENTES").Range("CONVENIO").Value & "_" & Sheets("CLIENTES").Range("NOME_MES").Value & "_" & Sheets("CLIENTES").Range("ANO_REF").Value & ".TXT"
Open NOME_ARQUIVO For Output As 1
For LINHA = 9 To 9 + linhas
[highlight #CC0000] Print #1, Sheets("TXT").Cells(LINHA, 1).Value[/highlight]
Next
Close 1
End Sub

I need find some function to overrite the "Print", that doesn´t add a line in the end of the file.

I am sorry about my English and thank you!
 
Hi,

If I understand your intent, I think you're making this much more difficult than it need to be.

Either COPY the source data SHEET and name the sheet txt or copy the UsedRange to the txt sheet.

DELETE all the data above the table data of interest leaving only the data you want in you text file.

Once you have your new txt sheet filled, all you need do is SaveAs... and then choose the appropriate text file type from the SaveAs popup list.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
If you add a semi-colon to the end of the Print line you will stop the cr/lf from appearing:

Code:
Print #1, Sheets("TXT").Cells(LINHA, 1).Value;

Since you will want the cr/lf on all of the lines except the last one this is one way to achieve the output:
Code:
For LINHA = 9 To 9 + linhas
    if LINHA < (9 + linhas)
        Print #1, Sheets("TXT").Cells(LINHA, 1).Value
    else
        Print #1, Sheets("TXT").Cells(LINHA, 1).Value;
    end if
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top