Importando TXT a pa...
 
Notifications
Clear all

Importando TXT a partir da ultima linha

2 Posts
2 Usuários
0 Reactions
1,034 Visualizações
(@wfranca)
Posts: 297
Honorable Member
Topic starter
 

Pessoal, o código abaixo me atende para importar meu arquivo txt ou CSV, uso a extenção CSV para que ele organize meu dados nas respectivas colunas, porém eu preciso que ao importar mais arquivos, ele importe a partir da ultima linha...

esse está importando para as colunas ao lado...

Dim sPath As String
Dim fName As String
Dim s As String
s = CurDir
'mudar para onde deseja que o diálogo seja apontado
'para quando ele é exibido
sPath = "C:Seu dirtorioSeus arquivos"
ChDrive sPath

fName = Application.GetOpenFilename( _
Filefilter:="CSV Files (*.CSV),*.CSV")
ChDrive s
ChDir s
If LCase(fName) = "false" Then Exit Sub
With ActiveSheet.QueryTables.Add _
(Connection:="TEXT;" & fName, _
Destination:=Range("A1"))
.Name = Replace(LCase(fName), ".xls", "")
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

Segue anexo o modelo de teste..

 
Postado : 24/10/2013 10:40 am
(@fernandofernandes)
Posts: 43750
Illustrious Member
 

Veja se é assim que espera?

Private Sub CommandButton1_Click()
Dim sPath As String, fName As String
  Dim s As String, lInic As Long, ls As Integer
  s = CurDir
'Variavel lInic define a linha da planilha onde os dados serão depositados
'Variavel ls define em qual linha do arquivo .csv irá iniciar a importação
If Cells(Cells.Rows.Count, "A").End(xlUp).Row = 1 Then
lInic = 1
ls = 1
Else
lInic = Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1
ls = 3
End If
  'mudar para onde deseja que o diálogo seja apontado
  'para quando ele é exibido
  sPath = "C:Seu dirtorioSeus arquivos"
  ChDrive sPath

  fName = Application.GetOpenFilename( _
   Filefilter:="CSV Files (*.CSV),*.CSV")
  ChDrive s
  ChDir s
  If LCase(fName) = "false" Then Exit Sub
    With ActiveSheet.QueryTables.Add _
        (Connection:="TEXT;" & fName, _
        Destination:=Range("A" & lInic))
        .Name = Replace(LCase(fName), ".xls", "")
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .TextFileStartRow = ls
        .Refresh BackgroundQuery:=False
    End With
End Sub
 
Postado : 24/10/2013 10:55 am