+ Reply to Thread
Results 1 to 8 of 8

Automating a calculation - (Solver)

  1. #1
    Registered User
    Join Date
    12-10-2006
    Posts
    3

    Automating a calculation - (Solver)

    I have a list of numbers as such:

    5
    1
    3
    6
    100

    I need to add up rows in order to reach a target figure. At the moment I'm manually going through the list and adding the numbers. Great fun.

    So say I want 101, I want the system to return row no.2 and row no.5.
    I'm pretty sure this can be done via VBA but I'm no code jocky. On the code front, the solution needs to be decently scalable as the list can be up to 20 variables long.

    Many thanks

  2. #2
    Registered User
    Join Date
    11-28-2006
    Posts
    45
    Just but this formula in where cell a1 id the first value in your list. Then just fill the formula down to the bottom of your list. Where this column = 101 will be the same as where you manually workrd it out.

    =SUM($A$1:A1)

    Cheers

  3. #3
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Hi,

    If you have solver add-in installed try this

    In range B1:B5, add a set of 0s

    Then Enter this in C1

    =SUMPRODUCT(A1:A5*B1:B5)


    Now do Tools / Solver / Set Target Cell 'C1' to 'value of' and put in your
    target value of 101
    Next, using the range selector under the 'By Changing cells'
    section, select cells B1:B5 as the ones to change and hit enter which will
    take you back to the first dialog box. Now hit the 'Add' button, and add
    the constraint that B1:B30 must be 'bin' (Means binary as in 1 or 0, and
    it's one of the dropdowns, so just hit the arrow and select 'bin') and just
    hit Solve. You MUST ensure that in this example, when you add the 'bin'
    constraint range, you do not inadvertantly include the formula cell C1,
    else you will get an error message such as 'Binary Contsraint cell reference
    must include only adjustable cells'

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  4. #4
    Registered User
    Join Date
    12-10-2006
    Posts
    3
    TomMoston = Thats not working, it sounds like my initial post wasn't clear. Your forumla just seems to be adding the numbers up, it isn't telling me which combination of rows = my target.

  5. #5
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Hi,

    Here's an example

    VBA Noob
    Attached Files Attached Files

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Here's a VBA example

    VBA Noob
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-10-2006
    Posts
    3
    Just gave this a whirl and it does the business.

    Many thanks VBA noob.

  8. #8
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    No problem

    Glad it worked for you

    Thanks also for the feedback

    VBA Noob

+ 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