Este fórum está sendo desativado

Depois de 9 anos, este fórum será desativado. Mas calma.... estamos migrando para uma comunidade no DISCORD. Junte-se a nós.

ENTRAR PARA DISCORD

Tópicos relacionados a códigos VBA, gravação de macros, etc.
  • Avatar do usuário
#44339
Amigos boa noite,

Estou com um problema ao exportar um arquivo para .txt. Está tudo perfeito, porém fica uma linha vazia no .txt que é gerado, aí tenho que abrir ele e excluir com um backspace a última linha em branco. Alguém saberia me ajudar?

Obrigado.
#44343
Olá Andre,

Se o problema está na geração do TXT, seria muito interessante se você colocasse aqui a planilha que o gera.

Jimmy San Juan
#44354
Esse é o código que estou usando:

Sub Salvar3()
Dim Nome As String

Dim Quantdados As Long
Dim Linha As Long

Quantdados = Sheets("Plan1").Range("A1000000").End(xlUp).Row
Linha = 2

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

While Linha < Quantdados + 1

Sheets("Plan1").Select

Nome = Range("A" & Linha).Value


Sheets("Plan2").Select

UltimaCel = Range("A1000000").End(xlUp).Row + 1

Range("A" & UltimaCel).Value = Nome

Linha = Linha + 1

Wend

Sheets("Plan1").Select

On Error GoTo Erro

Dim Arquivo As Workbook
Dim Plan As String

Plan = "Plan2"

Set Arquivo = Application.Workbooks.Add

ThisWorkbook.Sheets(Plan).Copy Before:=Arquivo.Sheets(1)

Arquivo.SaveAs ThisWorkbook.Path & "\" & "emis" & "_" & ActiveSheet.Range("A1048576") & "_" & ActiveSheet.Range("B1048576") & "_" & "993" & ".txt", FileFormat:=xlTextPrinter, _
CreateBackup:=False

Arquivo.Close
MsgBox "Copia realizada com sucesso!", vbInformation, "CÓPIA"

Sheets("Plan2").Select

Range("A1:A1048576").Value = ""

Sheets("Plan1").Select

Application.Calculation = xlCalculationAutomatic

Exit Sub

Erro:
MsgBox "Erro!", vbCritical, "COPIAR"


End Sub
#44928
Bom dia André,

Não sei se ainda precisa da solução, pois já se passou algum tempo.

Apresento abaixo uma proposta, mas não sei se atenderá pois tenho dúvidas quanto ao papel da Plan2 na macro que você postou.

Como a macro limpa seu conteúdo no final, assumi que ela é apenas uma planilha transitória entre a Plan1 e a geração do TXT, com o objetivo de não salvar no TXT da linha 1 da Plan1. Por outro lado, a macro preserva e grava possível conteúdo da Plan2.

Na proposta abaixo não considero a Plan2, mas se quiser que seu conteúdo também seja incluído no TXT, me aviso que faço pequenas alterações na macro.

Jimmy San Juan
Código: Selecionar todos
Sub Salvar3()

Open ThisWorkbook.Path & "\" & "emis" & "_" & _
     Sheets("Plan1").Range("A1048576").Value & "_" & _
     Sheets("Plan1").Range("B1048576").Value & "_" & "993" & ".txt" _
     For Output As #1
     
Qtd = Sheets("Plan1").Range("A" & Rows.Count).End(xlUp).Row
For Lin = 2 To Qtd
    Valor = Sheets("Plan1").Range("A" & Lin).Value
    If Lin <> Qtd Then Print #1, Valor Else Print #1, Valor;
Next

Close
MsgBox "Copia realizada com sucesso!", vbInformation, "CÓPIA"

End Sub
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]

Estamos migrando para uma comunidade no Discord