+ Reply to Thread
Results 1 to 2 of 2

vba select best sum combination from a column which exceeds a value by the lowest amout?

Hybrid View

  1. #1
    Registered User
    Join Date
    05-30-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2003
    Posts
    23

    vba select best sum combination from a column which exceeds a value by the lowest amout?

    I have a column of numbers eg:
    Colum B
    B1 523
    B2 438
    B3 956
    B4 673
    B5 45
    B6 55
    B7 911


    How do i write code (using vba) to Sum of all the possible combinations of these numbers in order to reach the value of a cell (A1) and gives me the smallest excess?
    For example A1 is 1100
    So result should be =SUM(O25,O28)
    =SUM(673,438) 1111
    Remainder
    remainder 11

    All I really need is something that will sum all the possible comlumns and i will be able to complete the rest myself.

  2. #2
    Registered User
    Join Date
    05-30-2012
    Location
    Ireland
    MS-Off Ver
    Excel 2003
    Posts
    23

    Re: vba select best sum combination from a column which exceeds a value by the lowest amou

    Sub combi_3()
    Dim a
    Dim b
    Dim last_row As Long
    Dim combi_number As Long
    Dim n As Long
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim l As Long
    With ActiveSheet
    last_row = .Range("a8").End(xlUp).Row
    a = .Range("a1:a8" & last_row).Value
    End With
    Debug.Print a(1, 1)
    combi_number = WorksheetFunction.Combin(last_row, 3)
    ReDim b(1 To combi_number, 1 To 1)
    For i = 1 To last_row
    For j = i + 1 To last_row
    For k = j + 1 To last_row
    For l = k + 1 To last_row
    n = n + 1: b(n, 1) = a(i, 1) + a(j, 1) + a(k, 1) + a(l, 1)

    Next l
    Next k
    Next j
    Next i
    ReDim Preserve b(1 To combi_number, 1 To 1)
    ActiveSheet.Range("b1").Resize(combi_number, 1) = b
    End Sub

    I tried the above code but it doesnt give all permutations of the sums

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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