Import some columns from TXT to VBA

Asked

Viewed 45 times

0

Good afternoon!

I’m quite a beginner and I’m having doubts about how to proceed. I have a TXT and could import it to VBA and until I could get the first line that I didn’t need.

However, I need to redo and only import 5 columns that interest me, since the spreadsheet goes from A to the GI column.

And on top of that I need to have the first row with the name I will give to the 5 imported columns. That’s possible and how I would do it?

The code I used:

Sub importar_arquivo()
    Application.ScreenUpdating = False
    importaArquivo
End Sub

Private Function importaArquivo()
    Dim arquivo As String
    arquivo = abrirArquivo
    If arquivo <> Empty Then
        Debug.Print "ok"
        ActiveWorkbook.Worksheets.Add 'nova planilha
        With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & arquivo & "", Destination:=Range("A1"))
            .Name = "teste"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 2
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End If
    arquivo = ""
End Function

Function abrirArquivo() As String
    Dim arquivo As String
    On Error GoTo sair:
    arquivo = ""
    With Application.FileDialog(msoFileDialogFilePicker)
          'Selecção de apenas um aquivo
          .AllowMultiSelect = False
          'Filtro de extenção
          .Filters.Add "All files", "*.txt; *.csv"
          'Mostra a caixa de dialogo
          .Show
          'Armazena o caminho do arquivo
          arquivo = .SelectedItems.Item(1)
    End With
    abrirArquivo = arquivo
sair:
    arquivo = ""
End Function

And the code I need to do is just take what I left in bold on the first line.

[H2]1|2|||10|49.92|1818481.6|0|S0011||||||||||||S0011|||||||||||||**51**|20201030000000||20201030000000|||||||||||||**2**||||||988788|9770|**9770**|||||||0|0|0||||False|**UNITS**|**12**||||20250220000000|||||||12|12|0|6|988788||||||||||0|0|0|0|0|0|0|0|0|0|||||||||||||||||||||0|0|0|0|0||||||||||||||||||||||||

Hugs,

Arthur Esteves

  • 1

    What is the initial code used? , what’s the problem, have you tried any code?

  • The code I used worked, but it’s the basic one. I need to get some columns of TXT only. And not the whole file as I did. and I still have to add a first line defining the name I need.

  • What code?...

  • There’s no code in here

  • you have to assemble a minimum example of code and example of the file, so do not see how to answer

  • 2

    edited the question and put.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.