Ir ao conteúdo

Dúvida com o VBA Excel - Gerar arquivo txt


ewertonfatos

Posts recomendados

Postado

Boa Tarde Pessoal,

Sou novato em VB e estou necessitando criar uma planilha onde serão colocados dados de uma nota fiscal e, ao clicar no botão será gerado um arquivo txt com todos os dados da planilha.

Entretanto, devo estar utilizando algum argumento erroneo, pois está dando erro ao executar a macro.

Segue código:

Private Sub btn_gera_txt_Click()

Dim k As Long
Dim r As Long
r = Cells(Rows.Count, "D").End(xlUp).Row
k = 2
Do While k <= r
Cells(k, 4) = Cells(k, 4).Text
k = k + 1
Loop

Dim cl As Range
Sheets("Dados_Saidas").Select
Range("A3").Select


Open "C:\Documents and Settings\renan.costa\desktop\INF_REND_2011" + Format(Now, " ddmmyyyy-hhmmss") + ".txt" For Output As #1


ÚltimaLinha = Range("A65536").End(xlUp).Row
For Each cl In Range("A3:A" & ÚltimaLinha)

If cl.Value <> "" Then
If Cells(cl.Row, 6).Value <> "" And Cells(cl.Row, 6).Value > 0 Then

Print #1, Cells(cl.Row, 1) & Space(4 - Len(Cells(cl.Row, 1))); _
Cells(cl.Row, 2) & Space(10 - Len(Cells(cl.Row, 2))); _
Cells(cl.Row, 3) & Space(3 - Len(Cells(cl.Row, 3))); _
Cells(cl.Row, 4) & Space(5 - Len(Cells(cl.Row, 4))); _
Cells(cl.Row, 5) & Space(30 - Len(Cells(cl.Row, 5))); _
Cells(cl.Row, 6) & Space(14 - Len(Cells(cl.Row, 6))); _
Cells(cl.Row, 7) & Space(15 - Len(Cells(cl.Row, 7))); _
Cells(cl.Row, 8) & Space(2 - Len(Cells(cl.Row, 8))); _
Cells(cl.Row, 9) & Space(4 - Len(Cells(cl.Row, 9))); _
Cells(cl.Row, 10) & Space(3 - Len(Cells(cl.Row, 10))); _
Cells(cl.Row, 11) & Space(6 - Len(Cells(cl.Row, 11))); _
Cells(cl.Row, 12) & Space(6 - Len(Cells(cl.Row, 12))); _
Cells(cl.Row, 13) & Space(13 - Len(Cells(cl.Row, 13))); _
Cells(cl.Row, 14) & Space(13 - Len(Cells(cl.Row, 14))); _
Cells(cl.Row, 15) & Space(13 - Len(Cells(cl.Row, 15))); _
Cells(cl.Row, 16) & Space(4 - Len(Cells(cl.Row, 16))); _
Cells(cl.Row, 17) & Space(11 - Len(Cells(cl.Row, 17))); _
Cells(cl.Row, 18) & Space(11 - Len(Cells(cl.Row, 18))); _

Print #2, Cells(cl.Row, 19) & Space(13 - Len(Cells(cl.Row, 19))); _
Cells(cl.Row, 20) & Space(13 - Len(Cells(cl.Row, 20))); _
Cells(cl.Row, 21) & Space(1 - Len(Cells(cl.Row, 21))); _
Cells(cl.Row, 22) & Space(4 - Len(Cells(cl.Row, 22))); _
Cells(cl.Row, 23) & Space(13 - Len(Cells(cl.Row, 23))); _
Cells(cl.Row, 24) & Space(13 - Len(Cells(cl.Row, 24))); _
Cells(cl.Row, 25) & Space(13 - Len(Cells(cl.Row, 25))); _
Cells(cl.Row, 26) & Space(13 - Len(Cells(cl.Row, 26))); _
Cells(cl.Row, 27) & Space(15 - Len(Cells(cl.Row, 27))); _
Cells(cl.Row, 28) & Space(5 - Len(Cells(cl.Row, 28))); _
Cells(cl.Row, 29) & Space(5 - Len(Cells(cl.Row, 29))); _
Cells(cl.Row, 30) & Space(2 - Len(Cells(cl.Row, 30))); _
Cells(cl.Row, 31) & Space(2 - Len(Cells(cl.Row, 31))); _
Cells(cl.Row, 32) & Space(2 - Len(Cells(cl.Row, 32))); _
Cells(cl.Row, 33) & Space(2 - Len(Cells(cl.Row, 33))); _
Cells(cl.Row, 34) & Space(13 - Len(Cells(cl.Row, 34))); _
Cells(cl.Row, 35) & Space(13 - Len(Cells(cl.Row, 35))); _
Cells(cl.Row, 36) & Space(13 - Len(Cells(cl.Row, 36))); _
Cells(cl.Row, 37) & Space(5 - Len(Cells(cl.Row, 37))); _
Cells(cl.Row, 38) & Space(2 - Len(Cells(cl.Row, 38))); _
Cells(cl.Row, 39) & Space(4 - Len(Cells(cl.Row, 39))); _
Cells(cl.Row, 40) & Space(13 - Len(Cells(cl.Row, 40))); _
Cells(cl.Row, 41) & Space(2 - Len(Cells(cl.Row, 41))); _
Cells(cl.Row, 42) & Space(13 - Len(Cells(cl.Row, 42))); _
Cells(cl.Row, 43) & Space(13 - Len(Cells(cl.Row, 5)));


End If
End If
Next
Close #1
Close #2
Sheets("Dados_Saidas").Select
Range("A3").Select
MsgBox "Arquivo exportado com sucesso.", vbInformation, "Confirmação"
End Sub

Outra questão é que gostaria que o sistema imprimisse direto até a coluna 43, mas ele estava mostrando mensagem de erro, informando que haviam muitas linhas. Então tive que dividir, mas será que está correto?

Por favor, se puderem me ajudar eu agradeço muito!

Obrigado

Atenciosamente

Ewerton Ventura

  • 2 semanas depois...
Postado

Everton, se você quer realizar uma segunda impressao com o comando Print #2, é preciso atribuir um caminho com o nome do arquivo para realizar a tarefa.

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!