Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SUMM value baes a uniques ID during loop line by line txt file

Status
Not open for further replies.

2009luca

Programmer
Jul 27, 2013
222
0
16
IT
i loop a txt file with the tipcal free file do while eof ecc...


during the loop of txt with a Left postion i get a value and store in a Myvar, for example Myvar="1234A"

example of txt file:

1234A 1 8 78 56
1234A 12 45 11 5
1234B 8 1 1 2

Ecc...

on the same line a get the Mid value of 1, 8, 78 and 56

similar

T1=1
T2=8
T3=78
T4=56

now i need to summ and assign the to single key correspondent value

to the end of txt i I would like to have stored in collection, array or dictionry items or other ...:

1234A 13 53 89 61
1234B 8 1 1 2
 
Started with this in a text file:
[tt]
1234A 1 8 78 56
1234A 12 45 11 5
1234B 8 1 1 2[/tt]

Ended up with this:
[tt]
1234A 1 8 78 56
1234A 12 45 11 5
1234B 8 1 1 2
1234A 13 53 89 61
1234B 8 1 1 2[/tt]

Code:
Dim strFPath As String
Dim strTextLine As String
Dim ary() As String
Dim aryLine(3) As String
Dim Myvar As String
Dim str2AddAtTheEnd As String
Dim i As Integer

strFPath = App.Path & "\MyTextFile.txt"

Open strFPath For Input As #1
Do While Not EOF(1)
    i = i + 1
    Line Input #1, strTextLine
    
    ary = Split(strTextLine, " ")
    
    If Myvar = ary(0) Then
        aryLine(0) = Val(aryLine(0)) + Val(ary(1))
        aryLine(1) = Val(aryLine(1)) + Val(ary(2))
        aryLine(2) = Val(aryLine(2)) + Val(ary(3))
        aryLine(3) = Val(aryLine(3)) + Val(ary(4))
    Else
        If i > 1 Then
            str2AddAtTheEnd = str2AddAtTheEnd & Myvar & " " & Join(aryLine, " ") & vbNewLine
        End If
        Myvar = ary(0)
        aryLine(0) = Val(ary(1))
        aryLine(1) = Val(ary(2))
        aryLine(2) = Val(ary(3))
        aryLine(3) = Val(ary(4))
    End If
   
Loop
Close #1

str2AddAtTheEnd = str2AddAtTheEnd & Myvar & " " & Join(aryLine, " ")

Open strFPath For Append As #1
    Print #1, vbNewLine & str2AddAtTheEnd
Close #1

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
That rather assumes that the data is in a sorted, sequential order - which may be fair enough.

Here's an alternative using a Dictionary that doesn't care:
Code:
[blue]Public Sub Example()
    Dim mylines() As String
    Dim myData() As String
    Dim getline As Variant
    Dim lp As Long
    Dim Results As Variant
    
    With New FileSystemObject
        mylines = Split(.OpenTextFile("c:\source.txt", ForReading).ReadAll, vbCrLf)
    End With
    
    With New Dictionary
        On Error Resume Next
        For Each getline In mylines
            myData = Split(getline, " ")
            .Add myData(0), myData
            If Err <> 0 Then
                For lp = 1 To 4
                    myData(lp) = CStr(CLng(myData(lp)) + CLng(.Item(myData(0))(lp)))
                Next
                .Item(myData(0)) = myData
                Err.Clear
            End If
        Next
        On Error GoTo 0
        [green]' Dictionary now holds our results, keyed on the identifier[/green]
        Results = .Items [green]' Results is now a variant array containing the data you want[/green]
        For Each getline In Results
            Debug.Print Join(getline, " ")
        Next
    End With
End Sub[/blue]
 
TKS FOR CODE TO THE ALL!

But in my txt file i need to get the numeric value, with a Mid statement and not with a split, the value of 1, 8, 78 and 56 and assign to the var T1,T2,T3,T4

EXAMPLE for the first line in txt file refered to 1234A 1 8 78 56

T1=1
T2=8
T3=78
T4=56
 
>But in my txt file i need to get the numeric value, with a Mid statement and not with a split

So ... this is some form of school exercise? Can't think of any other good reason why this would be mandated.

And then you say:
>assign to the var T1,T2,T3,T4

But your original post concluded:
> i need to summ ... and I would like to have stored in collection, array or dictionry items or other ...:

So which is it?

Having said that, I would just point out that both examples provided above do return the intermediate result you are looking for, just not as directly obviously as in variables called T1 thru T4
 
This has now been cross-posted to at least two other sites with slightly different wording.

More than one viable approach has already been doled out but you clearly haven't used them because you can't. Time to go learn the basics of programming because nobody is going to turn these into copy/paste code per your precise specifications.

If you want cheap custom programming try a rental-coder site that features bottom feeders willing to bid each other down into abject poverty. You might get lucky and they'll bid into negative territory and then you will get paid by them!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top