Import new data from a text file based on certain field criteria.

maudedog

New Member
Joined
Feb 8, 2009
Messages
17
I have a little VB knowledge but this one is way beyond my scope. This is a multipart problem I am trying to find a solution.

We have a tab delineated text file with 11 fields that is constantly updated by another program. We would like to copy/import just two fields of that data, let’s say fields #9 & #11 into Excel but only bring in the new data since the previous import. So if initially there are 5 lines of data, that would come in and populate the first 5 rows and 2 columns in a worksheet. Then subsequently it would bring in only anything added after the initial copy/import. I have been able to write a routine that simply copies everything and overwrites what is there but this is not what we want.

The next part of this is that based on certain criteria, the data will be copied/imported into certain columns. The criteria variables will be in fields #2 and #6. So for example, if field #2 = 1, and field #6 = 2, then fields #9 & #11 will go in columns A & B. If field #2 = 2 and field #6 = 1, then fields #9 & #11 will go into columns C&D. There would most likely be 6 possible combinations of those variables (1/1, ½, 1/3, 2/1, 2/2, 2/3) so columns A-L could be populated. Note that field #9 could be blank in which case it would just bring in field #11 or just copy the blank text. These variable numbers could be referenced to cells in the workbook where we select which variables will correspond to which columns. For example if we wanted columns A & B to contain the data with the variables 1 & 2, cells G1 & H1 could have #1 & #2 in their cells if that is helpful. There may be times when we want that variable combo of 2/1 to go into cells A & B.

Here is the text file:

0 1 2 0 0 1 b 1648 1648 9:55:02.61 09:55:03
0 2 5 0 0 1 b 1648 1659 9:57:45.70 09:57:46
0 2 7 0 0 2 b 1649 1649 9:58:09.84 09:58:10
0 3 8 0 0 1 s 9:58:09.87 09:58:10
0 1 9 0 0 1 b 1656 1656 9:58:13.80 09:58:15
0 1 11 0 0 1 b 1657 1657 9:58:19.88 09:58:20
0 3 12 0 0 1 s 9:58:19.93 09:58:20
0 1 13 0 0 1 b 1663 1663 9:58:25.77 09:58:26
0 1 14 0 0 2 s 9:58:25.78 09:58:26
0 1 15 0 0 2 s 9:59:22.40 09:59:23
0 2 16 0 0 1 b 1664 1664 9:59:22.41 09:59:23
0 2 17 0 0 2 s 1671 1671 9:59:26.33 09:59:27

I have attached a copy of screenshots of the text file along with what the workbook could look like.

The last part is ideally this routine would only run when the text file has been updated but I am not sure how possible that is. Otherwise I can just put this on a timer that will run every minute or so.

Being a bit of neophyte with VBA I have found this forum to be an awesome resource so I hope I have explained this correctly. If this info is already posted somewhere please point me in that direction. I am always willing to learn but this one is bit beyond me right now.

As always, many thanks in advance.
 

Attachments

  • TXT File.png
    TXT File.png
    75.3 KB · Views: 16
  • Excel File.png
    Excel File.png
    133.1 KB · Views: 15

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
VBA is not the Holy Grail. Neither is Power Query, but it IS a LOT easier! This is what I got doing nothing but specifying TAB as the delimiter (PQ thought Colon was)
Book6
ABCDEFGHIJK
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11
2012001b164816489:55:02.6109:55:03
3025001b164816599:57:45.7009:57:46
4027002b164916499:58:09.8409:58:10
5038001s9:58:09.8709:58:10
6019001b165616569:58:13.8009:58:15
70111001b165716579:58:19.8809:58:20
80312001s9:58:19.9309:58:20
90113001b166316639:58:25.7709:58:26
100114002s9:58:25.7809:58:26
110115002s9:59:22.4009:59:23
120216001b166416649:59:22.4109:59:23
130217002s167116719:59:26.3309:59:27
Sheet1

This is with ONE STEP to Remove other columns:
Book6
MN
1Column9Column11
2164809:55:03
3165909:57:46
4164909:58:10
509:58:10
6165609:58:15
7165709:58:20
809:58:20
9166309:58:26
1009:58:26
1109:59:23
12166409:59:23
13167109:59:27
Sheet1

I realize that's not what you're looking for, but it's not clear what is determining what goes in what column. BTW, it was relatively easy to replace all Spaces with Tabs for the Text file, but posing screenshots is rarely helpful. Use XL2BB instead.
Also, I'm guessing you're using that #$!@ Merge & Center button. There's not much that screws up the structure of a Worksheet more! Use Center Across Selection under Alignment -> Horizontal. This has been requested to be added to the M&C Drop Down for years. I added it to my QAT.
I suspect the codes in columns 1-7 mean something that might be useful in getting this fleshed out further, but as it is it's hard to understand why the output is what it is and what columns are related.
BTW, if this is a single text file that is updated, the Query can be set up to run automatically on opening the file. When open, Refresh All will update it.
 
Upvote 0
VBA is not the Holy Grail. Neither is Power Query, but it IS a LOT easier! This is what I got doing nothing but specifying TAB as the delimiter (PQ thought Colon was)
Book6
ABCDEFGHIJK
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11
2012001b164816489:55:02.6109:55:03
3025001b164816599:57:45.7009:57:46
4027002b164916499:58:09.8409:58:10
5038001s9:58:09.8709:58:10
6019001b165616569:58:13.8009:58:15
70111001b165716579:58:19.8809:58:20
80312001s9:58:19.9309:58:20
90113001b166316639:58:25.7709:58:26
100114002s9:58:25.7809:58:26
110115002s9:59:22.4009:59:23
120216001b166416649:59:22.4109:59:23
130217002s167116719:59:26.3309:59:27
Sheet1

This is with ONE STEP to Remove other columns:
Book6
MN
1Column9Column11
2164809:55:03
3165909:57:46
4164909:58:10
509:58:10
6165609:58:15
7165709:58:20
809:58:20
9166309:58:26
1009:58:26
1109:59:23
12166409:59:23
13167109:59:27
Sheet1

I realize that's not what you're looking for, but it's not clear what is determining what goes in what column. BTW, it was relatively easy to replace all Spaces with Tabs for the Text file, but posing screenshots is rarely helpful. Use XL2BB instead.
Also, I'm guessing you're using that #$!@ Merge & Center button. There's not much that screws up the structure of a Worksheet more! Use Center Across Selection under Alignment -> Horizontal. This has been requested to be added to the M&C Drop Down for years. I added it to my QAT.
I suspect the codes in columns 1-7 mean something that might be useful in getting this fleshed out further, but as it is it's hard to understand why the output is what it is and what columns are related.
BTW, if this is a single text file that is updated, the Query can be set up to run automatically on opening the file. When open, Refresh All will update it.
Nordic Timing Book v1.7.xlsm
ABCDEFGHIJK
1011002s9:55:02.319:55:02.00
2012001b164816489:55:02.619:55:03.00
3013001b9:57:19.979:57:21.00
4014001b9:57:21.419:57:21.00
5015001b164816489:57:45.709:57:46.00
6016002s9:57:45.719:57:46.00
7017001b164916499:58:09.849:58:10.00
8018002s9:58:09.879:58:10.00
9019001b165616569:58:13.809:58:15.00
100110002s9:58:13.829:58:15.00
110111001b165716579:58:19.889:58:20.00
120112002s9:58:19.939:58:20.00
130113001b166316639:58:25.779:58:26.00
140114002s9:58:25.789:58:26.00
150115002s9:59:22.409:59:23.00
160116001b166416649:59:22.419:59:23.00
170117002s9:59:26.339:59:27.00
180118001b167516759:59:26.349:59:27.00
190119001b167716779:59:30.489:59:31.00
200120002s9:59:30.509:59:31.00
210121001b169716979:59:35.209:59:36.00
220122002s9:59:35.229:59:36.00
230123002s10:00:06.7810:00:07.00
240124001b1701170110:00:06.7810:00:07.00
250125001b1712171210:00:10.9710:00:12.00
260126002s10:00:10.9810:00:12.00
srt_data (2)
 
Upvote 0
Nordic Timing Book v1.7.xlsm
ABCDEFGHIJK
1011002s9:55:02.319:55:02.00
2012001b164816489:55:02.619:55:03.00
3013001b9:57:19.979:57:21.00
4014001b9:57:21.419:57:21.00
5015001b164816489:57:45.709:57:46.00
6016002s9:57:45.719:57:46.00
7017001b164916499:58:09.849:58:10.00
8018002s9:58:09.879:58:10.00
9019001b165616569:58:13.809:58:15.00
100110002s9:58:13.829:58:15.00
110111001b165716579:58:19.889:58:20.00
120112002s9:58:19.939:58:20.00
130113001b166316639:58:25.779:58:26.00
140114002s9:58:25.789:58:26.00
150115002s9:59:22.409:59:23.00
160116001b166416649:59:22.419:59:23.00
170117002s9:59:26.339:59:27.00
180118001b167516759:59:26.349:59:27.00
190119001b167716779:59:30.489:59:31.00
200120002s9:59:30.509:59:31.00
210121001b169716979:59:35.209:59:36.00
220122002s9:59:35.229:59:36.00
230123002s10:00:06.7810:00:07.00
240124001b1701170110:00:06.7810:00:07.00
250125001b1712171210:00:10.9710:00:12.00
260126002s10:00:10.9810:00:12.00
srt_data (2)
Nordic Timing Book v1.7.xlsm
ABCD
1number 1 time 2number 1 time 1
2numbertimenumbertime
39:57:45.7116489:57:45.70
49:58:09.8716499:58:09.84
59:58:13.8216569:58:13.80
69:58:19.9316579:58:19.88
79:58:25.7816639:58:25.77
89:59:22.4016649:59:22.41
99:59:26.33
Sheet2
 
Upvote 0
VBA is not the Holy Grail. Neither is Power Query, but it IS a LOT easier! This is what I got doing nothing but specifying TAB as the delimiter (PQ thought Colon was)
Book6
ABCDEFGHIJK
1Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11
2012001b164816489:55:02.6109:55:03
3025001b164816599:57:45.7009:57:46
4027002b164916499:58:09.8409:58:10
5038001s9:58:09.8709:58:10
6019001b165616569:58:13.8009:58:15
70111001b165716579:58:19.8809:58:20
80312001s9:58:19.9309:58:20
90113001b166316639:58:25.7709:58:26
100114002s9:58:25.7809:58:26
110115002s9:59:22.4009:59:23
120216001b166416649:59:22.4109:59:23
130217002s167116719:59:26.3309:59:27
Sheet1

This is with ONE STEP to Remove other columns:
Book6
MN
1Column9Column11
2164809:55:03
3165909:57:46
4164909:58:10
509:58:10
6165609:58:15
7165709:58:20
809:58:20
9166309:58:26
1009:58:26
1109:59:23
12166409:59:23
13167109:59:27
Sheet1

I realize that's not what you're looking for, but it's not clear what is determining what goes in what column. BTW, it was relatively easy to replace all Spaces with Tabs for the Text file, but posing screenshots is rarely helpful. Use XL2BB instead.
Also, I'm guessing you're using that #$!@ Merge & Center button. There's not much that screws up the structure of a Worksheet more! Use Center Across Selection under Alignment -> Horizontal. This has been requested to be added to the M&C Drop Down for years. I added it to my QAT.
I suspect the codes in columns 1-7 mean something that might be useful in getting this fleshed out further, but as it is it's hard to understand why the output is what it is and what columns are related.
BTW, if this is a single text file that is updated, the Query can be set up to run automatically on opening the file. When open, Refresh All will update it.
Just uploaded the text file (in Excel format) and the sample worksheet
 
Upvote 0
I think there was a couple errors in your original post. If so, I have a workign answer, although it doesn't do an incremental read - it reads the whole file. And, so far, it's limited to about 675 lines in the text file. Given how the picture of the text file looks, I assumed it is tab deliminated.

I think these statements are errors based on looking at the images of your text file and desired output file.
if field #2 = 1, and field #6 = 2, then fields #9 & #11 will go in columns A & B.
If field #2 = 2 and field #6 = 1, then fields #9 & #11 will go into columns C&D.
There would most likely be 6 possible combinations of those variables (1/1, ½, 1/3, 2/1, 2/2, 2/3) so columns A-L could be populated.

In your example table of output, the #2 = 2 and # 6 =1 maps to columns E and F and 1/3 and 2/3 combinations don't make sense (but 3/1 and 3/2 combinations do make sense).

Looking at the output table, number in column 2 gives you the group of 4 columns to write to (1st group of 4 columns, 2nd group of 4 columns, etc.). The number in Column 6 tells you it's the first half or second half of the 4 column group ... e.g., cols A & B or C & D. Combos 1/3 and 2/3 don't fit but combos 3/1 and 3/2 do fit (and 3/1 is in your example text and table.) So, the function is

COL1 = ( (ValueIn#2 - 1) * 4 ) + ( ( ValueIn#6 - 1 ) * 2 ) + 1
COL2 = COL1 + 1


1670627337838.png


If this is correct. the code below will largely do what you want. The file location and where you write the data are hard coded at the top.

VBA Code:
Sub TableMagic()
Dim strTextFile$, rngOutputAnchor As Range
Dim objFSO, strData, ayLines, ayBody(), i%, ayOutput(), ayRows()
Dim c1%, c2%, ayOutputTable
Const ForReading = 1

'------------------------------------------------------
'name of the text file
strTextFile = "C:\Users\voudoujoe\Downloads\@TEST\TEST.txt"
'top left cell of your output table
Set rngOutputAnchor = ThisWorkbook.Worksheets("TESTTABLE").Range("$B$28")
'------------------------------------------------------

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll

'Split by lines, put into an array
ayLines = Split(strData, vbCrLf)

'Split each line into its own array
ReDim ayBody(UBound(ayLines) - 1)
For i = 0 To UBound(ayBody)
    ayBody(i) = Split(ayLines(i), vbTab)
Next i

ReDim ayOutput(1 To 12, 1 To 1)
ReDim ayRows(1 To 12)

For i = 0 To UBound(ayBody)

    'first output column is a function of ayBody (1) and (5)
    c1 = ((ayBody(i)(1) - 1) * 4) + ((ayBody(i)(5) - 1) * 2) + 1
    c2 = c1 + 1
    
    ayRows(c1) = ayRows(c1) + 1: ayRows(c2) = ayRows(c2) + 1
    If UBound(ayOutput, 2) < ayRows(c1) Then ReDim Preserve ayOutput(1 To 12, 1 To ayRows(c1))
    
    ayOutput(c1, ayRows(c1)) = ayBody(i)(8)
    ayOutput(c1 + 1, ayRows(c1)) = ayBody(i)(10)
    
Next i

    ayOutputTable = Application.WorksheetFunction.Transpose(ayOutput)
    rngOutputAnchor.Resize(UBound(ayOutputTable, 1), 12) = ayOutputTable
    
'Cleanup
Set objFSO = Nothing
Set rngOutputAnchor = Nothing
Exit Sub

WriteTextFileToSpreadsheet:
    Dim r%, c%
    r = -1: c = -1
    With rngOutputAnchor
        For r = 0 To UBound(ayBody)
            For c = 0 To UBound(ayBody(r))
                .Offset(r, c) = ayBody(r)(c)
            Next c
        Next r
    End With
End Sub
 
Upvote 0
I think these statements are errors based on looking at the images of your text file and desired output file.
if field #2 = 1, and field #6 = 2, then fields #9 & #11 will go in columns A & B.
If field #2 = 2 and field #6 = 1, then fields #9 & #11 will go into columns C&D.
Sorry - Meant to say that line 2 (not line 1) had an error.
 
Upvote 0
I think there was a couple errors in your original post. If so, I have a workign answer, although it doesn't do an incremental read - it reads the whole file. And, so far, it's limited to about 675 lines in the text file. Given how the picture of the text file looks, I assumed it is tab deliminated.

I think these statements are errors based on looking at the images of your text file and desired output file.



In your example table of output, the #2 = 2 and # 6 =1 maps to columns E and F and 1/3 and 2/3 combinations don't make sense (but 3/1 and 3/2 combinations do make sense).

Looking at the output table, number in column 2 gives you the group of 4 columns to write to (1st group of 4 columns, 2nd group of 4 columns, etc.). The number in Column 6 tells you it's the first half or second half of the 4 column group ... e.g., cols A & B or C & D. Combos 1/3 and 2/3 don't fit but combos 3/1 and 3/2 do fit (and 3/1 is in your example text and table.) So, the function is

COL1 = ( (ValueIn#2 - 1) * 4 ) + ( ( ValueIn#6 - 1 ) * 2 ) + 1
COL2 = COL1 + 1


View attachment 80553

If this is correct. the code below will largely do what you want. The file location and where you write the data are hard coded at the top.

VBA Code:
Sub TableMagic()
Dim strTextFile$, rngOutputAnchor As Range
Dim objFSO, strData, ayLines, ayBody(), i%, ayOutput(), ayRows()
Dim c1%, c2%, ayOutputTable
Const ForReading = 1

'------------------------------------------------------
'name of the text file
strTextFile = "C:\Users\voudoujoe\Downloads\@TEST\TEST.txt"
'top left cell of your output table
Set rngOutputAnchor = ThisWorkbook.Worksheets("TESTTABLE").Range("$B$28")
'------------------------------------------------------

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll

'Split by lines, put into an array
ayLines = Split(strData, vbCrLf)

'Split each line into its own array
ReDim ayBody(UBound(ayLines) - 1)
For i = 0 To UBound(ayBody)
    ayBody(i) = Split(ayLines(i), vbTab)
Next i

ReDim ayOutput(1 To 12, 1 To 1)
ReDim ayRows(1 To 12)

For i = 0 To UBound(ayBody)

    'first output column is a function of ayBody (1) and (5)
    c1 = ((ayBody(i)(1) - 1) * 4) + ((ayBody(i)(5) - 1) * 2) + 1
    c2 = c1 + 1
   
    ayRows(c1) = ayRows(c1) + 1: ayRows(c2) = ayRows(c2) + 1
    If UBound(ayOutput, 2) < ayRows(c1) Then ReDim Preserve ayOutput(1 To 12, 1 To ayRows(c1))
   
    ayOutput(c1, ayRows(c1)) = ayBody(i)(8)
    ayOutput(c1 + 1, ayRows(c1)) = ayBody(i)(10)
   
Next i

    ayOutputTable = Application.WorksheetFunction.Transpose(ayOutput)
    rngOutputAnchor.Resize(UBound(ayOutputTable, 1), 12) = ayOutputTable
   
'Cleanup
Set objFSO = Nothing
Set rngOutputAnchor = Nothing
Exit Sub

WriteTextFileToSpreadsheet:
    Dim r%, c%
    r = -1: c = -1
    With rngOutputAnchor
        For r = 0 To UBound(ayBody)
            For c = 0 To UBound(ayBody(r))
                .Offset(r, c) = ayBody(r)(c)
            Next c
        Next r
    End With
End Sub
mmhill - Awesome - thanks for posting this so quickly. I just tried to run this but I am getting a "subscript out of range error" at:
1670633392901.png

Any ideas?
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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