find four elements in array whose sum equal to a given number X
for me following algorithm works:
Dim Amt(1 To 10) As Long
Dim i
For i = 1 To 10
Amt(i) = Val(List1.List(i))
Next i
Dim Tot As Integer
Dim lenth As Integer
lenth = List1.ListCount
'sum of 4 numbers
For i = 1 To lenth
For j = 1 To lenth
For k = 1 To lenth
For l = 1 To lenth
If i <> j And i <> k And i <> l And j <> k And j <> l And k <> l Then
Debug.Print CBAmt(i) & " , " & CBAmt(j) & " , " & CBAmt(k) & " , " & CBAmt(l)
If CBAmt(i) + CBAmt(j) + CBAmt(k) + CBAmt(l) = Val(Text1) Then
MsgBox "Found"
found = True
Exit For
End If
End If
Next
If found = True Then Exit For
Next
If found = True Then Exit For
Next
If found = True Then Exit For
Next