Solved

greatest number of numbers that sums to n

Posted on 2004-08-07
2
181 Views
Last Modified: 2012-05-05
Hi.  I am trying to make a program that takes in a list box (lets call it list1) with a bunch of items in it that have a numerical value.  Next, I have 4 other numbers in a different list.  The point of this is to take a number from the list2 and display all the possibilities in which when you sum up that first card with any number of cards from list1, the total is exactly 11.

Can anyone make me a function that does this? Thank you.
0
Comment
Question by:cachedVB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 80 total points
ID: 11745822
This creates two string arrays.
Indices will show the combination in terms of the list positions of the parts, e.g. "1, 3 ,4"
Values shows the component parts including the number from list2 e.g. "1 + 5 + 5"

Private Sub GetCombinations(List2Number as integer)
Dim Indices() As String
Dim Values() As String
Dim CombinationCount As Integer
Dim Total As Integer
Dim i As Integer
Dim j As Integer
Dim Value As String
Dim Index As String

For i = 1 To (2 ^ List1.ListCount)
    Value = List2Number
    Index = ""
    Total = List2Number
    For j = 0 To List1.ListCount - 1
        If 2 ^ j And i Then
            Total = Total + List1.List(j)
            Index = Index & IIf(Index = "", "", ", ") & j
            Value = Value & " + " & List1.List(j)
        End If
        Next j
    If Total = 11 Then
        ReDim Preserve Indices(CombinationCount)
        ReDim Preserve Values(CombinationCount)
        Indices(CombinationCount) = Index
        Values(CombinationCount) = Value
        CombinationCount = CombinationCount + 1
    End If
Next i
End Sub
0
 
LVL 4

Author Comment

by:cachedVB
ID: 11746340
Thanks! Just what I wanted... although I had to tweak it a bit to find the longest one. :)
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question