vba extract data from text file

drop05

Active Member
Joined
Mar 23, 2021
Messages
285
Office Version
  1. 365
Platform
  1. Windows
Hello, I am wondering if there is a way to select a text file and loop through the text file line by line.
each line is similar to this structure

"FORM 1","1","0","0","NAME","Diego","O"
"FORM 1","2","0","0","NAME","Pedro","O"
etc. . .

and put it into a dictionary of sorts to store the information collected from the text file and then put it into a table on a sheet in the workbook. There is 8 areas that are separated by the comma, so area 1 = form 1, area 2 is the 1 and 2, etc.

Hopefully I am thinking correctly of the process
 
One thing to remember is that the text file is driving the size of the tables in terms of column count and row count. I was able to make it a drop more flexible but it has to conform to your text. I commented as much as I could without being overly verbose.

VBA Code:
Sub TextFlie()

    Dim arr, arr2, crit, boolines
    Dim ws As Worksheet: Set ws = Worksheets("Keys")
    Dim row_number As Long, col_number As Long, i As Long, ct As Long, c As Long, x As Long
    Dim a As Long, b As Long, d As Long, e As Long, lrow As Long, leng As Long
    Dim LineFromFile As Variant
    Dim FilePath As String, boo As String
    Dim tbl As ListObject, rng As Range
    
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
    
    'Loop to determine the number of lines and columns in the text file.
    Do Until EOF(1)
        Line Input #1, LineFromFile
        ct = ct + 1
        leng = UBound(Split(LineFromFile, ",")) + 1
    Loop
    '*********************
    'This assigns the DataBodyRange of Table1 to the array named "crit"
    crit = ws.ListObjects("Table1").DataBodyRange
    '*********************
    'Dimension two arrays to the size of the text file
    ReDim arr(1 To ct, 1 To leng)
    ReDim arr2(1 To ct, 1 To leng)
    c = 1
    Close #1
    Open FilePath For Input As #1
    'Loop to split the text file and write it to an array named "arr"
    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For x = 0 To UBound(LineItems)
            arr(c, x + 1) = Replace(LineItems(x), """", "")  'Replace is removing the quotes from the text between the commas
        Next
        c = c + 1
    Loop
    '**********************************
    'Check to see which rows of Table1 are FALSE and put that criteria in a string
    For a = 1 To UBound(crit)
        If crit(a, 4) = False Then
            boo = boo & "," & crit(a, 2)
        End If
    Next
    '******************************
    ct = 1
    'Split out the text of the criteria from the created string
    boo = Mid(boo, 2)
    boolines = Split(boo, ",")
    '***********************************
    'Apply criteria to the array "arr" that contains the contents of the text file
    'Write the filtered array "arr" to a new array named "arr2"
    For b = 1 To UBound(arr)
        For d = 0 To UBound(boolines)
            If arr(b, 5) = boolines(d) Then
                arr(b, 5) = ""
            End If
        Next
        If arr(b, 5) <> "" Then
             For e = 1 To leng
                arr2(ct, e) = arr(b, e)
            Next
            ct = ct + 1
        End If
    Next
    '*************************************
    'Clear existing Table2
    Set tbl = ActiveSheet.ListObjects("Table2")
    With tbl.DataBodyRange
        If .Rows.Count > 1 Then
            .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
        End If
    End With
    '**************************
    'Write new data to Table2 and resize
    tbl.DataBodyRange(1, 1).Resize(UBound(arr2, 1), 6) = arr2
    lrow = Range("F1").End(xlDown).Row
    Set rng = Range("Table2[#All]").Resize(lrow, leng)
    ws.ListObjects("Table2").Resize rng
    '*****************************
End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
One thing to remember is that the text file is driving the size of the tables in terms of column count and row count. I was able to make it a drop more flexible but it has to conform to your text. I commented as much as I could without being overly verbose.

VBA Code:
Sub TextFlie()

    Dim arr, arr2, crit, boolines
    Dim ws As Worksheet: Set ws = Worksheets("Keys")
    Dim row_number As Long, col_number As Long, i As Long, ct As Long, c As Long, x As Long
    Dim a As Long, b As Long, d As Long, e As Long, lrow As Long, leng As Long
    Dim LineFromFile As Variant
    Dim FilePath As String, boo As String
    Dim tbl As ListObject, rng As Range
   
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
   
    'Loop to determine the number of lines and columns in the text file.
    Do Until EOF(1)
        Line Input #1, LineFromFile
        ct = ct + 1
        leng = UBound(Split(LineFromFile, ",")) + 1
    Loop
    '*********************
    'This assigns the DataBodyRange of Table1 to the array named "crit"
    crit = ws.ListObjects("Table1").DataBodyRange
    '*********************
    'Dimension two arrays to the size of the text file
    ReDim arr(1 To ct, 1 To leng)
    ReDim arr2(1 To ct, 1 To leng)
    c = 1
    Close #1
    Open FilePath For Input As #1
    'Loop to split the text file and write it to an array named "arr"
    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For x = 0 To UBound(LineItems)
            arr(c, x + 1) = Replace(LineItems(x), """", "")  'Replace is removing the quotes from the text between the commas
        Next
        c = c + 1
    Loop
    '**********************************
    'Check to see which rows of Table1 are FALSE and put that criteria in a string
    For a = 1 To UBound(crit)
        If crit(a, 4) = False Then
            boo = boo & "," & crit(a, 2)
        End If
    Next
    '******************************
    ct = 1
    'Split out the text of the criteria from the created string
    boo = Mid(boo, 2)
    boolines = Split(boo, ",")
    '***********************************
    'Apply criteria to the array "arr" that contains the contents of the text file
    'Write the filtered array "arr" to a new array named "arr2"
    For b = 1 To UBound(arr)
        For d = 0 To UBound(boolines)
            If arr(b, 5) = boolines(d) Then
                arr(b, 5) = ""
            End If
        Next
        If arr(b, 5) <> "" Then
             For e = 1 To leng
                arr2(ct, e) = arr(b, e)
            Next
            ct = ct + 1
        End If
    Next
    '*************************************
    'Clear existing Table2
    Set tbl = ActiveSheet.ListObjects("Table2")
    With tbl.DataBodyRange
        If .Rows.Count > 1 Then
            .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
        End If
    End With
    '**************************
    'Write new data to Table2 and resize
    tbl.DataBodyRange(1, 1).Resize(UBound(arr2, 1), 6) = arr2
    lrow = Range("F1").End(xlDown).Row
    Set rng = Range("Table2[#All]").Resize(lrow, leng)
    ws.ListObjects("Table2").Resize rng
    '*****************************
End Sub
This is awesome, i am trying it with another one and i think because its like an address so really long and maybe because it has commas in it it because when i get rid of the commas it works but when add it back it get the error at the line in the text file, it is getting a subscript out of range error here,

'Loop to split the text file and write it to an array named "arr"
Do Until EOF(1)
Line Input #1, LineFromFile
Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
For x = 0 To UBound(LineItems)
arr(c, x + 1) = Replace(LineItems(x), """", "") 'Replace is removing the quotes from the text between the commas ***ERROR HAPPENS HERE***
Next
c = c + 1


Inside the text file here is an example
"FORM 1","1","0","0","ADDRESS","123 willow way, San Deigo, California","O"
 
Upvote 0
Yes, because the Split function is splitting the text between the commas. It splits out the address at the commas and then looks for quotation marks, which it does not find so it fails. When you take the commas out it sees the entire address as one item and that one item is surrounded by quotation marks.
I hope that makes sense. We can probably trap that error so the code does not fail.
 
Upvote 0
Yes, because the Split function is splitting the text between the commas. It splits out the address at the commas and then looks for quotation marks, which it does not find so it fails. When you take the commas out it sees the entire address as one item and that one item is surrounded by quotation marks.
I hope that makes sense. We can probably trap that error so the code does not fail.
Oh like adding a on error resume next? Trying to add something so it can still print out the address with the commas and not get the error
 
Upvote 0
Nah, no error resume stuff, I am not really fond of that. It may work, but there should be better ways.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top