INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

(OP)
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
Print #1, Sheets("TXT").Cells(LINHA, 1).Value
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!

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

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

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

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 

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

(OP)
smileThank you so much DjangMan! It works!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close