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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
This would be a very basic example of what I think you are trying accomplish. You will have to change the code to place the text where you want it. I took your example text and expanded a couple of lines so that it looks like this and is named "drop05.txt". The path in the code is where is resides on my computer and you will have to change to meet your requirements.

"FORM 1","1","0","0","NAME","Diego","O"
"FORM 1","2","0","0","NAME","Pedro","O"
"FORM 1","3","0","0","NAME","Juan","O"
"FORM 1","4","0","0","NAME","Jose","O"

This is the code:
VBA Code:
Sub TextFlie()

    Dim row_number As Single
    Dim LineFromFile As Variant
    Dim col_number As Single
    Dim FilePath As String
    Dim i As Long
    
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
    row_number = 0
    col_number = 0
    Range("a1").Select

    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For i = 0 To UBound(LineItems)
            ActiveCell.Offset(row_number, col_number + i).Value = LineItems(i)
        Next
        row_number = row_number + 1
    Loop

    Close #1

End Sub
And my worksheet looks like this after the code has run:
Text from test.txt file.xlsm
ABCDEFGH
1"FORM 1""1""0""0""NAME""Diego""O"
2"FORM 1""2""0""0""NAME""Pedro""O"
3"FORM 1""3""0""0""NAME""Juan""O"
4"FORM 1""4""0""0""NAME""Jose""O"
5
Sheet7
 
Upvote 0
This would be a very basic example of what I think you are trying accomplish. You will have to change the code to place the text where you want it. I took your example text and expanded a couple of lines so that it looks like this and is named "drop05.txt". The path in the code is where is resides on my computer and you will have to change to meet your requirements.

"FORM 1","1","0","0","NAME","Diego","O"
"FORM 1","2","0","0","NAME","Pedro","O"
"FORM 1","3","0","0","NAME","Juan","O"
"FORM 1","4","0","0","NAME","Jose","O"

This is the code:
VBA Code:
Sub TextFlie()

    Dim row_number As Single
    Dim LineFromFile As Variant
    Dim col_number As Single
    Dim FilePath As String
    Dim i As Long
   
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
    row_number = 0
    col_number = 0
    Range("a1").Select

    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For i = 0 To UBound(LineItems)
            ActiveCell.Offset(row_number, col_number + i).Value = LineItems(i)
        Next
        row_number = row_number + 1
    Loop

    Close #1

End Sub
And my worksheet looks like this after the code has run:
Text from test.txt file.xlsm
ABCDEFGH
1"FORM 1""1""0""0""NAME""Diego""O"
2"FORM 1""2""0""0""NAME""Pedro""O"
3"FORM 1""3""0""0""NAME""Juan""O"
4"FORM 1""4""0""0""NAME""Jose""O"
5
Sheet7
Yes this does help a lot, gives me an idea and the code makes sense
Questions,

what if in the table i have key words that are located within the txt file. So like some lines may not contain it but some will
so say the table i have has the key words in column A2 and down column A.
So for example
"FORM 1"
"FORM 2"
"FORM 3"
are three key words id like to find within the text file and once it finds it in the text file then it grabs the values to the right of it so the
1","0","0","NAME","Diego","O"
2","0","0","NAME","Pedro","O"
3","0","0","NAME","Juan","O"
4","0","0","NAME","Jose,"O"
 
Upvote 0
Can you show me an example of what the text file might look like and also an example of what the worksheet would look like before and after. If you could use XL2BB that would be great. It is really a matter of inserting code that will look for your keywords and then having the text moved around. If I could see it I could better try to help.
 
Upvote 0
Can you show me an example of what the text file might look like and also an example of what the worksheet would look like before and after. If you could use XL2BB that would be great. It is really a matter of inserting code that will look for your keywords and then having the text moved around. If I could see it I could better try to help.
yes give me a moment and i can upload an example, i appreciate you!
 
Upvote 0
It is getting late here on the East Coast of the US (around midnight), so take your time. I probably will not get to it until tomorrow morning...
 
Upvote 0
It is getting late here on the East Coast of the US (around midnight), so take your time. I probably will not get to it until tomorrow morning...
I am not too familiar with using the xl2bb but think i did it right. I am not sure how to include a txt file but at the bottom of the tables i have inlcuded an example of what is inside the txt file and also a screen shot of how it looks since the excel file will have text to column
But over night i thought maybe use a table to have the form and key words looking for and boolval if it is true then those are the ones where id like the values for labeled "Section", "Group", "Subgroup" and "Value" as well as the form and key names from the txt file and fill out the table, and the table has the values from the text file how i am trying to get it to look like so you will see that all the values from the txt file with the boolval being true are in the second table on the right
anything that is false can be ignored. and the txt file can be selected by a user since i will have multiple file paths


Book2
ABCDEFGHIJKLM
1Form2FindKey2FindSheetBoolValFormKeySectionGroupSubgroupValueSheet
2FORM 1FIRST NAMESheet ATRUEFORM 1FIRST NAME110Jose
3FORM 1LAST NAMESheet ATRUEFORM 1FIRST NAME120Joseph
4FORM 1MIDDLE NAMESheet ATRUEFORM 1FIRST NAME130Pedro
5FORM 1GRADE LEVELSheet AFALSEFORM 1FIRST NAME210Christian
6FORM BBEG GRADE 1Sheet BTRUEFORM 1FIRST NAME220Pablo
7FORM BEND GRADE 1Sheet BFALSEFORM 1FIRST NAME230Tristan
8FORM BEND GRADE NAMESheet BTRUEFORM 1FIRST NAME310Chris
9FORM 1FIRST NAME320Preslee
10FORM 1FIRST NAME330Tyler
11FORM 1LAST NAME110Garcia
12FORM 1LAST NAME120Guerra
13FORM 1LAST NAME130Gonzales
14FORM 1LAST NAME210Garza
15FORM 1LAST NAME220Houston
16FORM 1LAST NAME230Brady
17FORM 1LAST NAME310Garcia
18FORM 1LAST NAME320Preslee
19FORM 1LAST NAME330Hal
20FORM BBEG GRADE 111090
21FORM BBEG GRADE 1120100
22FORM BBEG GRADE 113060
23FORM BBEG GRADE 121073
24FORM BBEG GRADE 122088
25FORM BBEG GRADE 123055
26FORM BBEG GRADE 131077
27FORM BBEG GRADE 132075
28FORM BBEG GRADE 133081
29FORM BEND GRADE NAME110Jose
30FORM BEND GRADE NAME120Joseph
31FORM BEND GRADE NAME130Pedro
32FORM BEND GRADE NAME210Christian
33FORM BEND GRADE NAME220Pablo
34FORM BEND GRADE NAME230Tristian
35FORM BEND GRADE NAME310Chris
36FORM BEND GRADE NAME320Preslee
37FORM BEND GRADE NAME330Tyler
38
39
40FORM 1110FIRST NAMEJoseO
41FORM 1120FIRST NAMEJosephO
42FORM 1130FIRST NAMEPedroO
43FORM 1210FIRST NAMEChristianO
44FORM 1220FIRST NAMEPabloO
45FORM 1230FIRST NAMETristanO
46FORM 1310FIRST NAMEChrisO
47FORM 1320FIRST NAMEPresleeO
48FORM 1330FIRST NAMETylerO
49FORM 1110LAST NAMEGarciaO
50FORM 1120LAST NAMEGuerraO
51FORM 1130LAST NAMEGonzalesO
52FORM 1210LAST NAMEGarzaO
53FORM 1220LAST NAMEHoustonO
54FORM 1230LAST NAMEBradyO
55FORM 1310LAST NAMEGarciaO
56FORM 1320LAST NAMEPresleeO
57FORM 1330LAST NAMEHalO
58FORM 1110GRADE LEVEL1O
59FORM 1120GRADE LEVEL1O
60FORM 1130GRADE LEVEL2O
61FORM 1210GRADE LEVEL3O
62FORM 1220GRADE LEVEL3O
63FORM 1230GRADE LEVEL1O
64FORM 1310GRADE LEVEL4O
65FORM 1320GRADE LEVEL5O
66FORM 1330GRADE LEVEL3O
67FORM B110BEG GRADE 190O
68FORM B120BEG GRADE 1100O
69FORM B130BEG GRADE 160O
70FORM B210BEG GRADE 173O
71FORM B220BEG GRADE 188O
72FORM B230BEG GRADE 155O
73FORM B310BEG GRADE 177O
74FORM B320BEG GRADE 175O
75FORM B330BEG GRADE 181O
76FORM B110END GRADE 190O
77FORM B120END GRADE 195O
78FORM B130END GRADE 191O
79FORM B210END GRADE 1179O
80FORM B220END GRADE 170O
81FORM B230END GRADE 190O
82FORM B310END GRADE 180O
83FORM B320END GRADE 185O
84FORM B330END GRADE 175O
85FORM B110END GRADE NAMEJoseO
86FORM B120END GRADE NAMEJosephO
87FORM B130END GRADE NAMEPedroO
88FORM B210END GRADE NAMEChristianO
89FORM B220END GRADE NAMEPabloO
90FORM B230END GRADE NAMETristianO
91FORM B310END GRADE NAMEChrisO
92FORM B320END GRADE NAMEPresleeO
93FORM B330END GRADE NAMETylerO
94
Sheet1


i thought id just paste what was in the txt file the header is not included in the txt file just showing what it aligns to for the table, each section has a group and each group can have a subgroup but most for now are 0 but some can be 1,2,3, etc

Form, Section, Group, SubGroup, Key, Value

"FORM 1","1","1","0","FIRST NAME","Jose","O"
"FORM 1","1","2","0","FIRST NAME","Joseph","O"
"FORM 1","1","3","0","FIRST NAME","Pedro","O"
"FORM 1","2","1","0","FIRST NAME","Christian","O"
"FORM 1","2","2","0","FIRST NAME","Pablo","O"
"FORM 1","2","3","0","FIRST NAME","Tristan","O"
"FORM 1","3","1","0","FIRST NAME","Chris","O"
"FORM 1","3","2","0","FIRST NAME","Preslee","O"
"FORM 1","3","3","0","FIRST NAME","Tyler","O"
"FORM 1","1","1","0","LAST NAME","Garcia","O"
"FORM 1","1","2","0","LAST NAME","Guerra","O"
"FORM 1","1","3","0","LAST NAME","Gonzales","O"
"FORM 1","2","1","0","LAST NAME","Garza","O"
"FORM 1","2","2","0","LAST NAME","Houston","O"
"FORM 1","2","3","0","LAST NAME","Brady","O"
"FORM 1","3","1","0","LAST NAME","Garcia","O"
"FORM 1","3","2","0","LAST NAME","Preslee","O"
"FORM 1","3","3","0","LAST NAME","Hal","O"
"FORM 1","1","1","0","GRADE LEVEL","1","O"
"FORM 1","1","2","0","GRADE LEVEL","1","O"
"FORM 1","1","3","0","GRADE LEVEL","2","O"
"FORM 1","2","1","0","GRADE LEVEL","3","O"
"FORM 1","2","2","0","GRADE LEVEL","3","O"
"FORM 1","2","3","0","GRADE LEVEL","1","O"
"FORM 1","3","1","0","GRADE LEVEL","4","O"
"FORM 1","3","2","0","GRADE LEVEL","5","O"
"FORM 1","3","3","0","GRADE LEVEL","3","O"
"FORM B","1","1","0","BEG GRADE 1","90","O"
"FORM B","1","2","0","BEG GRADE 1","100","O"
"FORM B","1","3","0","BEG GRADE 1","60","O"
"FORM B","2","1","0","BEG GRADE 1","73","O"
"FORM B","2","2","0","BEG GRADE 1","88","O"
"FORM B","2","3","0","BEG GRADE 1","55","O"
"FORM B","3","1","0","BEG GRADE 1","77","O"
"FORM B","3","2","0","BEG GRADE 1","75","O"
"FORM B","3","3","0","BEG GRADE 1","81","O"
"FORM B","1","1","0","END GRADE 1","90","O"
"FORM B","1","2","0","END GRADE 1","95","O"
"FORM B","1","3","0","END GRADE 1","91","O"
"FORM B","2","1","0","END GRADE 11","79","O"
"FORM B","2","2","0","END GRADE 1","70","O"
"FORM B","2","3","0","END GRADE 1","90","O"
"FORM B","3","1","0","END GRADE 1","80","O"
"FORM B","3","2","0","END GRADE 1","85","O"
"FORM B","3","3","0","END GRADE 1","75","O"
"FORM B","1","1","0","END GRADE NAME","Jose","O"
"FORM B","1","2","0","END GRADE NAME","Joseph","O"
"FORM B","1","3","0","END GRADE NAME","Pedro","O"
"FORM B","2","1","0","END GRADE NAME","Christian","O"
"FORM B","2","2","0","END GRADE NAME","Pablo","O"
"FORM B","2","3","0","END GRADE NAME","Tristian","O"
"FORM B","3","1","0","END GRADE NAME","Chris","O"
"FORM B","3","2","0","END GRADE NAME","Preslee","O"
"FORM B","3","3","0","END GRADE NAME","Tyler","O"

Hopefully i provided the information correctly and I do appreciate the help! Thank you soooo much
 

Attachments

  • txt ex.PNG
    txt ex.PNG
    43.3 KB · Views: 5
Upvote 0
This is a first try. I am not sure if wanted your sheet to look exactly as you posted or if that was just more info for your requirements. This code will start writing to Cell A2. I am assuming you have the Header row already written. If not we can add a line of code to write it. Also, I was not sure what you wanted in the "Sheet" Column, right now it is showing "O" taken from the text file- Did you want that to show either an "A" or "B" according to your key. Sorry about the confusion.

VBA Code:
Sub TextFlie()

    Dim row_number As Long, col_number As Long, i As Long
    Dim LineFromFile As Variant
    Dim FilePath As String
    
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
    row_number = 1
    col_number = 0
    Range("a1").Select

    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For i = 0 To UBound(LineItems)
            If Replace(LineItems(i), """", "") = "GRADE LEVEL" Or Replace(LineItems(i), """", "") = "END GRADE 1" Then
                ActiveCell.Offset(row_number, col_number + i).Value = ""
            Else
                ActiveCell.Offset(row_number, col_number + i).Value = Replace(LineItems(i), """", "")
            End If
        Next
        row_number = row_number + 1
    Loop

    Close #1
    
    Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
 
Upvote 0
This is a first try. I am not sure if wanted your sheet to look exactly as you posted or if that was just more info for your requirements. This code will start writing to Cell A2. I am assuming you have the Header row already written. If not we can add a line of code to write it. Also, I was not sure what you wanted in the "Sheet" Column, right now it is showing "O" taken from the text file- Did you want that to show either an "A" or "B" according to your key. Sorry about the confusion.

VBA Code:
Sub TextFlie()

    Dim row_number As Long, col_number As Long, i As Long
    Dim LineFromFile As Variant
    Dim FilePath As String
   
    FilePath = "G:\Excel VBA\drop05.txt"    'Change path here"
    Close #1
    Open FilePath For Input As #1
    row_number = 1
    col_number = 0
    Range("a1").Select

    Do Until EOF(1)
        Line Input #1, LineFromFile
        Dim LineItems As Variant: LineItems = Split(LineFromFile, ",")
        For i = 0 To UBound(LineItems)
            If Replace(LineItems(i), """", "") = "GRADE LEVEL" Or Replace(LineItems(i), """", "") = "END GRADE 1" Then
                ActiveCell.Offset(row_number, col_number + i).Value = ""
            Else
                ActiveCell.Offset(row_number, col_number + i).Value = Replace(LineItems(i), """", "")
            End If
        Next
        row_number = row_number + 1
    Loop

    Close #1
   
    Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
Sorry i have not tried it had something come up emergancy, but id like it to look like the right table and using the table on the left to use column A and B for helping to find those values in the text file rather than just doing "" in the code because I would add more, but only those that if column D the boolean is true
Then once finding them in the text file yeah to look like the table on the right with grabbing the form, section, group, subgroup, key, value; but the order is how the headers are in the table: form, key, group, subgroup, value, not sheet that i have a formula. The tables are name tables with the table on the left being table1 and the one on the right is table2

this is how it is in the text file a little different positioning wise from what id like to try and get into the table

Form, Section, Group, SubGroup, Key, Value

"FORM 1","1","1","0","FIRST NAME","Jose","O"
 
Upvote 0
Are you saying that want to keep the table on the left and then the user would change the various boolean values and the extracted text would change accordingly. If that is the case, where do you want the text extracted to. The same sheet and position that it is now or on a new sheet. Also do you want the extracted text as a range of data or in a table. If you want a table, do you have the name of the table.
rather than just doing "" in the code
I am not sure what the above means. Did you try the code, all blanks are removed.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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