Phil
Phil

Reputation: 63

Compare every line in file

I need to compare a "field"

e.g. mid(stringX, 10, 20)

of each line with every other line in a file. If a match is found, I want to sum some numbers, but that's not really part of the problem.

My problem is, that I don't know, how to do a loop or something else, that compares each line with all others and does a certain operation at match...

I thought of doing something like this:

A=x
B=y

While (A != *Value of last line*) {
                While (B < *value of last line*) {
                compare a and b
                B++; }
A++
B=A+1; }

But I don't know how to implement something like this in vbscript.

Can someone please help me out?

Thanks

Upvotes: 1

Views: 92

Answers (1)

MikeD
MikeD

Reputation: 8941

To compare each element in a list (e.g. a line) with all others, you compare each element "with all elements below"

A code block in VBA could be like this:

Sub CompFull()
Dim X As Integer, Y As Integer
Dim MyRange As Range

Set MyRange = Selection

X = 1

Do While MyRange(X, 1) <> ""     ' outer loop from start to end
    Y = X + 1
    Do While MyRange(Y, 1) <> "" ' inner loop from one below current to end
        If MyRange(X, 1) = MyRange(Y, 1) Then
            MsgBox "Row " & X & " matches row " & Y, vbOKOnly, "Match found"
        End If
        Y = Y + 1
    Loop                         ' end of inner loop

    X = X + 1
Loop                             ' end of outer loop
End Sub

Upvotes: 2

Related Questions