Try such code:
Option Explicit
Dim input_val() As Double
Sub test()
Dim lr As Long, i As Long, j As Long, tmp_arr
With New Collection
For j = 1 To 3
tmp_arr = Application.Transpose(Range(Cells(2, j), Cells(Rows.Count, j).End(xlUp)).Value)
For i = 1 To UBound(tmp_arr)
On Error Resume Next
.Add tmp_arr(i), CStr(tmp_arr(i))
Next i
Next j
Debug.Print .Count
ReDim input_val(1 To .Count)
For i = 1 To .Count
input_val(i) = .Item(i)
Next i
End With
lr = Cells(Rows.Count, "E").End(xlUp).Row
tmp_arr = Application.Transpose(Range("E2:E" & lr).Value)
For i = 1 To UBound(tmp_arr)
tmp_arr(i) = check_avail(CDbl(tmp_arr(i)))
Next i
Range("F2:H" & lr + 5).ClearContents
Range("F2:F" & lr).Value = Application.Transpose(tmp_arr)
Range("F2:F" & lr).TextToColumns Destination:=Range("F2"), DataType:=xlDelimited, Semicolon:=True
End Sub
Function check_avail(needed_val As Double) As Variant
Const max_error = 0.00000001
Dim i&, j&, k&
For i = 1 To UBound(input_val)
For j = i To UBound(input_val)
For k = j To UBound(input_val)
If Abs(input_val(i) + input_val(j) + input_val(k) - 3 * needed_val) < max_error Then
check_avail = input_val(i) & ";" & input_val(j) & ";" & input_val(k)
Exit Function
End If
Next k, j, i
check_avail = "not available"
End Function
Note that because you are working with floating point numbers (not just mathematical Real numbers), the accuracy of computations is not perfect.
So just checing equality can sometimes return wrong results. Thatls why I'm checking if the difference is smaller than some acceptable inaccuracy.
Just as a proof: ry writing in A1:A100 values 0.1 and in B1:
Formula:
=SUM(A:A)-10
It's not 0, is it?
Bookmarks