Notifications
Clear all

Importar TXT linha a linha no VBA

5 Posts
4 Usuários
0 Reactions
1,065 Visualizações
(@abeniti)
Posts: 0
New Member
Topic starter
 

Tenho um TXT e precisaria realizar a importação deste linha a linha.
Hoje faço o comando de importação integral da seguinte forma:

Sub ImportarTXT_Entrada()

Dim Campos As Variant
Dim Arquivo As String

Application.ScreenUpdating = False

Sheets("Entradas").Visible = True

Sheets("Entradas").Select
    Cells.Select
    Selection.ClearContents
    
' aqui abre a caixa de diálogo para escolher o arquivo

Arquivo = Application.GetOpenFilename("Arquivos Texto(*.txt), *.txt")

Open Arquivo For Input As #1

' aqui popula as tabelas e transporta em texto

    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & Arquivo, Destination:=Range("$A$1"))
            .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, _
            2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    End With
Close #1
    
Sheets("Entradas").Visible = False
Application.ScreenUpdating = True

Sheets("Executar").Select
End Sub
 
Postado : 30/08/2016 2:40 pm
(@djunqueira)
Posts: 0
New Member
 

Se a sua versão do Excel for a 2013 ou 2016 o Power Query / Nova Consulta pode fazer isso p/ vc.
Veja como fica no arquivo anexo.

 
Postado : 30/08/2016 6:23 pm
(@edsonbr)
Posts: 0
New Member
 

Olá, Abeniti, seja bem vindo.

Para adaptar uma leitura linha a linha no seu código já existente a direção seria a seguinte:

Sub ImportarTXT_Entrada()
   . . .
   . . .
   Dim LinhaTexto as String
   Open Arquivo For Input As #1
   . . .
      Do While Not EOF(1)
         Line Input #1, LinhaTexto
         'Consumir a variável LinhaTexto conforme seu propósito
      Loop
   . . .
   Close #1
   . . .
   . . .
End Sub
 
Postado : 30/08/2016 11:42 pm
(@abeniti)
Posts: 0
New Member
Topic starter
 

Boa Edson,

Era isso mesmo que procurava. Muito Obrigado!

 
Postado : 02/09/2016 2:09 pm
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

ABeniti

Como você pulou a etapa de apresentação, não foi sugerido para você tomar conhecimentos sobre as regras e procedimentos do fórum.

Percebi que você nem sabe como agradecer ao colaborares.

Para facilitar tua participação no fórum, acesse os links abaixo:

viewtopic.php?f=7&t=203
viewtopic.php?f=7&t=7903
viewtopic.php?f=7&t=3841
viewtopic.php?f=7&t=16757
viewtopic.php?f=7&t=12600
viewtopic.php?f=7&t=3371

[]s

 
Postado : 02/09/2016 2:53 pm