+ Reply to Thread
Results 1 to 2 of 2

Finding N numbers in column to achieve desired value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2018
    Location
    german
    MS-Off Ver
    2013
    Posts
    1

    Finding N numbers in column to achieve desired value

    Hi
    I have numbers in columns A:C and E. I want to choose any combination of three numbers from the array of numbers in A:C (including repeats) that when averaged equal one of the value in Column E.

    I then want to display the three numbers that I used in the next three columns
    Untitled.jpg

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Finding N numbers in column to achieve desired value

    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: copy to clipboard
    =SUM(A:A)-10

    It's not 0, is it?
    Best Regards,

    Kaper

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2017, 04:55 PM
  2. inserting empty cells to achieve matching numbers across two columns
    By JanieceT in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-11-2015, 06:37 PM
  3. [SOLVED] Alter given values to achieve desired result
    By juriemagic in forum Excel Formulas & Functions
    Replies: 18
    Last Post: 01-30-2015, 09:53 AM
  4. Replies: 4
    Last Post: 05-08-2014, 09:43 PM
  5. [SOLVED] IF formula with certain conditions to achieve a desired value
    By Excel Dumbo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-11-2014, 10:59 PM
  6. Finding multiple cells that add up to a desired amount
    By txgodfather in forum Excel General
    Replies: 2
    Last Post: 03-09-2013, 08:25 AM
  7. Excel 2007 : calculating numbers needed to achieve best result
    By candiecane_81 in forum Excel General
    Replies: 3
    Last Post: 06-20-2011, 12:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1