INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

(OP)
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

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

Started with this in a text file:

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


Ended up with this:

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


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.

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

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

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
        ' Dictionary now holds our results, keyed on the identifier
        Results = .Items ' Results is now a variant array containing the data you want
        For Each getline In Results
            Debug.Print Join(getline, " ")
        Next
    End With
End Sub 

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

(OP)
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

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

>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

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

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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close