Looping a one row occurence through rows

hgus393

New Member
Joined
Mar 22, 2011
Messages
29
Hi all,

Sorry for the title. My problem is the following. I have a txt file that i read into excel. The file looks like this..an excerpt:
01ABBBBBB
02Hello
0320110906 1JamesSmith

0520110905+004800000000+004865693590+0009996001601
.
.
.

The row that starts with 05 is the row that contain all the data. That row goes on for 10 000 entries then it starts again with the row 03 chaning to say for example 0320110906 1AdamJones

I can capture the data rows, ie 05 rows, no problems. But how can I loop the row 03 ie JamesSmith and put this beside the data rows 05 and then loop this until the next occurence of a 03 row and the loop the new 03 row beside the data row 05....etc..

This is what I have now:
Code:
Sub TxtfileImport()
    Dim objFso
    Dim objTF
    Dim i As Long
    Dim X()
    Dim ArrLines, aLine
    Dim InFile As String
    Dim z
 
    With Application
        .ScreenUpdating = False
        .Application.EnableEvents = False
    End With
    InFile = "C:\temp\Data.txt"
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objTF = objFso.OpenTextFile(InFile)
    ArrLines = Split(objTF.ReadAll, vbCrLf)
    ReDim X(1 To UBound(ArrLines, 1) + 1, 1 To 2)
 
   For Each aLine In ArrLines
 
           If Left$(aLine, 2) = "05" Then
            i = i + 1
            X(i, 1) = Mid$(aLine, 3, 10) 'gets date
            X(i, 2) = Mid$(aLine, 37, 14) ' gets values
 
 
     End If
    Next
    ThisWorkbook.Sheets("Sheet1").[a1].Resize(UBound(X, 1), UBound(X, 2)) = X
 
    With Application
        .ScreenUpdating = True
        .Application.EnableEvents = True
    End With
    objTF.Close
    Set objTF = Nothing
End Sub
Cheers
Rob
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If I understand you question correctly possibly something like:

Code:
Sub TxtfileImport()
    Dim objFso
    Dim objTF
    Dim i As Long
    Dim X()
    Dim ArrLines, aLine
    Dim InFile As String
    Dim z
Dim FullName As String
 
    With Application
        .ScreenUpdating = False
        .Application.EnableEvents = False
    End With
    InFile = "C:\temp\Data.txt"
    Set objFso = CreateObject("Scripting.FileSystemObject")
    Set objTF = objFso.OpenTextFile(InFile)
    ArrLines = Split(objTF.ReadAll, vbCrLf)
    ReDim X(1 To UBound(ArrLines, 1) + 1, 1 To 2)
 
   For Each aLine In ArrLines
 If Left$(aLine, 2) = "03" Then FullName = Mid$(aLine,13,10)'Not sure what you want to extract from the "03" String
           If Left$(aLine, 2) = "05" Then
            i = i + 1
            X(i, 1) = FullName & " " & Mid$(aLine, 3, 10) 'gets date
            X(i, 2) = FullName & " " & Mid$(aLine, 37, 14) ' gets values
 
 
     End If
    Next
    ThisWorkbook.Sheets("Sheet1").[a1].Resize(UBound(X, 1), UBound(X, 2)) = X
 
    With Application
        .ScreenUpdating = True
        .Application.EnableEvents = True
    End With
    objTF.Close
    Set objTF = Nothing
End Sub
 
Last edited:
Upvote 0
Thanks for your answer, however there is only one row (that begins with 03) for each name and some 10 000 rows with the data (row that begins with 05). These rows are seperate, and as such your code will not return the data required as it will only look in row 05.

cheers

Rob
 
Upvote 0
Could you explain you expected output abit more then please.

As you initial description said there would be multiple listing of 03.

03 Users name1
05 Data1
05 Data2
05 Data3
03 Users name2
05 Data1
05 Data2
05 Data3
03 Users name3
05 Data1
05 Data2
05 Data3

Output:

Users Name1 Data1
Users Name1 Data2
Users Name1 Data3
Users Name2 Data1
Users Name2 Data2
Users Name2 Data3
Users Name3 Data1
Users Name3 Data2
Users Name3 Data3

Is this incorrect? :)
 
Upvote 0
Could you explain you expected output abit more then please.

As you initial description said there would be multiple listing of 03.

03 Users name1
05 Data1
05 Data2
05 Data3
03 Users name2
05 Data1
05 Data2
05 Data3
03 Users name3
05 Data1
05 Data2
05 Data3

Output:

Users Name1 Data1
Users Name1 Data2
Users Name1 Data3
Users Name2 Data1
Users Name2 Data2
Users Name2 Data3
Users Name3 Data1
Users Name3 Data2
Users Name3 Data3

Is this incorrect? :)

No this is entirely correct but since the code loops thourgh all lines trying to find a string that starts with 05 it will not include the full name...the above i exactly what I would like to do....:)
 
Upvote 0
Have you pasted the code into a copy of your workbook and run it?

I created a sample file ran it and this was the output:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style=";">JamesSmith 20110905+0</td><td style=";">JamesSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style=";">DavidSmith 20110905+0</td><td style=";">DavidSmith +0009996001601</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />
 
Upvote 0
Or do you want an output like:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">JamesSmith</td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style=";">DavidSmith</td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style="text-align: right;;"></td><td style=";">20110905+0</td><td style=";">9996001601</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />
 
Upvote 0
Or do you want an output like:

Excel 2007<TABLE style="BORDER-RIGHT: #a6aab6 1px solid; BORDER-TOP: #a6aab6 1px solid; BORDER-LEFT: #a6aab6 1px solid; BORDER-BOTTOM: #a6aab6 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #ffffff" cellPadding=2 rules=all><COLGROUP><COL style="BACKGROUND-COLOR: #e0e0f0" width=25><COL><COL><COL></COLGROUP><THEAD><TR style="COLOR: #161120; BACKGROUND-COLOR: #e0e0f0; TEXT-ALIGN: center"><TH></TH><TH>A</TH><TH>B</TH><TH>C</TH></TR></THEAD><TBODY><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">1</TD><TD>JamesSmith</TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">2</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">3</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">4</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">5</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">6</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">7</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">8</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">9</TD><TD>DavidSmith</TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">10</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">11</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">12</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">13</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">14</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">15</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR><TR><TD style="COLOR: #161120; TEXT-ALIGN: center">16</TD><TD style="TEXT-ALIGN: right"></TD><TD>20110905+0</TD><TD>9996001601</TD></TR></TBODY></TABLE>
Sheet1

This would be nice. However, when i run your code I don't get the name as (when i refer to row 03 i mean that it starts with 03 and not row 3) the name is in a seperate row. The code identifies the rows with the data ie the row that starts with "05", ie
Code:
If Left$(aLine, 2) = "05" Then

As the namerow starts with "03" and only occurs at the top I will not get any names I am afraid.

Cheers

Rob
 
Upvote 0
Code:
If Left$(aLine, 2) = "03" Then FullName = Mid$(aLine,13,10)'Not sure what you want to extract from the "03" String

I had amended my original code shortly after to posting.

It looks for any lines that start with 03 and sets the name based on your example. You will probably need to amend this for different names. If I had more examples of name strings I could have a look.

Could you provide a better example of your data.txt

with multiple name row and data rows.

Thanks,
/Comfy
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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