+ Reply to Thread
Results 1 to 22 of 22

Calculate Total using specific numbers

  1. #1
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Calculate Total using specific numbers

    I have four numbers which must to be used to calculate number X. Calculated total number can NOT be equal to the X. It must always be higher than X.

    For example:
    Calculate using only these numbers: 1, 2.7, 9 and 18.
    and X is 12.5.

    So formula should look something likes this: 9 + 2.7 + 1 = 12.7. If X is 11, then 9 + 2.7 = 11.7. If X is 21, then 18 + 2.7 + 1 = 21.7 and so forth.

    Remember the number X is not always the same number.

    Formula should also return how many times number was used. For example if number 9 was used twice it should return 9 x2 or something similar. So later I can also calculate the final price.
    Last edited by JackWhite; 03-21-2017 at 07:28 AM.

  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,613

    Re: Calculate Total using specific numbers

    It depends on a situation, but based on what you presented above, the best solution would be the use of solver add-in.
    See attached file (start point was 1 1 1 1 in column B)
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    But is it possible, that column B numbers are being generated automatically, based on the required result.

    Example:
    Let's say required result is 12.5.
    First it will check how many times required result can divide be divided by 18. If answer is lower than 1. Then it moves on to the number 9 and checks how many times you can divide with required result. With number 9 you can divide it only one time. So It subtracts 9 from 12.5. Result is 3.5. So again it checks how many times can you divide 3.5 with 2.7 and so forth.

    Problem would be with the last calculation. Since 3.5 minus 2.7 is 0.8. But 0.8 doesn't fit with number 1.

    Is there any kind of solution for this problem?

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    I think you will need VBA to do this.

  5. #5
    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,613

    Re: Calculate Total using specific numbers

    Have you tried to play with Zeszyt2.xlsx above?

    Insert required value in C2 and run solver to see (inserted there automatically) output in column B

  6. #6
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    So far I tried to used this solver. But sometimes, when my required result was 11. It used input number 1, eleven times. When is should have used number 9 and 2.7 once.

    Could you explain a bit more how should I use this Solver add-in? Maybe there's a bit more automated solution other than having to run Solver Add-in?
    Last edited by JackWhite; 03-20-2017 at 10:53 AM.

  7. #7
    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,613

    Re: Calculate Total using specific numbers

    Why
    number 1, eleven times
    is not good (and better than 9 & 2.7 which totals 11.7 not 11).
    I set a goal for solver to obtain smallest value greather or equal the one from cell C2

  8. #8
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Lets say I sell 18, 9, 2.7 and 1 liters of paint. It will be cheaper to buy 18 liters rather than buying two 9 liters of paint. So that's why formula should always check if the highest number fits to required amount first, then the rest.

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    Please Login or Register  to view this content.
    =get_numbers(A2:A5,B1)

    B1=Target value, A2:a5 =18 ,9, 2.7, 1

  10. #10
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    I'm bit confused with your VBA code.

    I created a new .xlsm file. Copied code to VBA with no Sub and End Sub. Added B1 target value, A2-A5 added those four numbers. Now I don't know what to do.

    Added attachment.
    Attached Images Attached Images

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    There is no Sub / End sub as it is a function.

    in a cell put .. e.g, B2

    =get_numbers(A2:A5,B1)

  12. #12
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Excel says "There's a problem with this formula".
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    May need to change "," to ";"
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Now it's working and got my problem solved. Thanks again JohnTopley and Kaper for your time.

  15. #15
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    But I got one question remaining. How do separate loops results to each cell and not in one string? Like in Kaper excel file. Where he output input numbers and used number to different column and rows.

  16. #16
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    Right click on Sheet1 tab, "View code", copy/paste code below

    Please Login or Register  to view this content.
    Add the code below to standard module

    Please Login or Register  to view this content.
    Enter data in B1 and code will be invoked.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Okay got it. Thanks again for your help.

  18. #18
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    I have changed original position of the cells and now I'm trying to figure out how to make it work again.

    Target number is at E19, those four numbers are at B12:B15 and outputted numbers are at D12:D15.

    Here's my attempt at the code.
    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    Code changes required ....


    Please Login or Register  to view this content.

    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Code is working. But the issue is that it doesn't calculate automatically, since the target number uses formula. More specifically it clones a value from another cell.

  21. #21
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,152

    Re: Calculate Total using specific numbers

    Try

    In Sheet1 Code
    Please Login or Register  to view this content.
    in Module 1 code add

    Please Login or Register  to view this content.
    in "Thisworkbook"

    Please Login or Register  to view this content.
    In attached, enter data in A8:

    E19: =A8
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    03-20-2017
    Location
    Helsinki, Finland
    MS-Off Ver
    2016
    Posts
    12

    Re: Calculate Total using specific numbers

    Its working again. Thank you.

+ 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. [SOLVED] Function possible for Using specific number to count other specific numbers total ?
    By Karnik in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-02-2015, 03:35 AM
  2. Random numbers to specific total
    By juriemagic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2015, 02:20 AM
  3. Replies: 10
    Last Post: 08-12-2014, 07:48 AM
  4. Calculate total numbers based on percentages
    By FM1 in forum Excel General
    Replies: 2
    Last Post: 02-09-2009, 08:15 AM
  5. Replies: 3
    Last Post: 01-25-2009, 12:45 PM
  6. [SOLVED] calculate total for only cells in a specific category
    By pyrofenix in forum Excel General
    Replies: 2
    Last Post: 12-11-2008, 02:27 AM
  7. How do I add a range of numbers to sum a specific total?
    By SJoshi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-15-2005, 10:06 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