+ Reply to Thread
Results 1 to 20 of 20

Calculate according to proper size

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Calculate according to proper size

    Hello everyone
    I have in sheet1 a number of products 1 , 2 , 3 in column B and each product consists of 4 items A , B , C and D in column C
    In column D there are different sizes of each item
    In row 1 in range ("F1:Q1") there are fixed sizes that I need to see the proper size for each item

    * Note : the first item "A" is essential and should occur only at least for once
    * The second item "B" would be 1 or 2 or 3 or more according to the available size in row 1
    ---------------------------------------------------------------------------------------
    Examples :

    1) As for the value in F1 is 1.500 - the expected result would be in range("F5:F8")
    suppose A = 1 and B =1 >> so 1 * 0.280 + 1 * 0.600 = 0.880

    Now we see the proper item (from "A" or "C" or "D") that will be close to 1.500 but it must be equal to 1.500 or smaller a little than this size
    If we add 0.880 + item "A" (0.280) = 1.160
    If we add 0.880 + item "C" (1.800) = 2.680
    If we add 0.880 + item "D" (0.880) = 1.760
    NOW let's the code determine the closest area of the three results 1.160 / 2.680 / 1.760 >>> the nearest one is 1.160

    Finally the result in range("F5:F8") would be as following : F5 = 2 / F6 = 1 / F7 ="x" / F8 = "x" and F9 would have the remaining size which is calculated
    (1.500 - 1.160) which will equal 0.340

    --->> in summary the first item "A" is essential that should be 1 at least then the second item "B" would be flexible ( 1 / 2 / 3 or more according to
    the available size ) then there must be one of the three items "A" or "C" or "D" according to the remaining size (the choice will be the nearest ..
    equal to less than the fixed size in row 1)
    2) Another example :
    In range("J11:J15") :
    In J14 will be "x" as there is no size related to item "D"
    item "A" will be 1 which equals to 0.060
    item "C" will be 1 which equals to 1.02
    item "B" will be changeable to suit the desired size in J1 so I do it manually and reached 10 as 10 * 0.080 = 0.8
    Now the total of the three items will be 1.880 (0.060 + 1.02 + 0.8)
    In J15 will be the remaining which is 1.900 - 1.880 = 0.020


    I know this is complicated issue but I hope to find help in this topic
    Thanks advanced for help

    * the thread is posted here too
    http://www.eileenslounge.com/viewtopic.php?f=30&t=28955

    * Sample file edited a little
    Attached Files Attached Files
    Last edited by YasserKhalil; 01-25-2018 at 05:04 PM.
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Hello
    Any help in this topic ..
    We would start with just one example so as to find a starting idea

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    I have started this code but couldn't finish it yet. Can you help me
    Sub Test()
        Dim r1 As Range
        Dim r2 As Range
        Dim x As Double
        Dim i As Long
        
        Set r1 = Range("D5:D8")
        Set r2 = Range("F5:F8")
        
        Range("F5").Value = 1
        
        Do
            Range("F6").Value = i + 1
            i = i + 1
            
            x = Evaluate("SUMPRODUCT(" & r1.Address & "," & r2.Address & ")")
            MsgBox x
            If x > Range("F1").Value Then Exit Do
        Loop Until Evaluate("F1-SUMPRODUCT(" & r1.Address & "," & r2.Address & ")") <= Application.Min(Range("F5").Value, Range("F7").Value, Range("F8").Value)
        
        Range("F9").Value = Evaluate("F1-SUMPRODUCT(" & r1.Address & "," & r2.Address & ")")
    End Sub

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    This is a modified one but the result is not as expected
    Sub Test()
        Dim r1 As Range
        Dim r2 As Range
        Dim x As Double
        Dim i As Long
        
        Set r1 = Range("D5:D8")
        Set r2 = Range("F5:F8")
        
        Range("F5").Value = 1
        
        Do
            Range("F6").Value = i + 1
            i = i + 1
            
            x = Evaluate("SUMPRODUCT(" & r1.Address & "," & r2.Address & ")")
            If x > Range("F1").Value Then Exit Do
        Loop Until Evaluate("F1-SUMPRODUCT(" & r1.Address & "," & r2.Address & ")") <= Application.Min(Range("D5").Value, Range("D7").Value, Range("D8").Value)
        
        Range("F9").Value = Evaluate("F1-SUMPRODUCT(" & r1.Address & "," & r2.Address & ")")
        If Range("F7") = "" Then Range("F7") = "x"
        If Range("F8") = "" Then Range("F8") = "x"
    End Sub
    Waiting for help from eperts

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Any help in this topic please?

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Can you give me an example of how to use solver here in this file .. I am confused about how to use the constraints

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    Not sure I understand your problem properly but using solver I got these result:

    solver_rsults.jpg

    The bad, you need to set up a solver model for each "case" so I've uploaded you file with the solver model for the first case.

    Alf
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Thanks a lot for reply
    I have looked at the file and that's great .. but I need t add more constraints if possible
    Some basics first:

    item "A" >> will be 1 or 2
    item "B" >> will be 1 at least but may be more than 1 >> so it is >=1
    item "C" >> will be 0 or 1
    item "D" >> will be 0 or 1

    item "B" should have at least 1 (this is a must) and may be more as I mentioned

    After item "B" there should be two other items (according to the remaining available size) >> so the possibilities would be:
    * item "A" = 2
    * item "A" = 1 + item "C" = 1
    * item "A" = 1 + item "D" = 1
    that depends on the available nearest remaining size

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    item "C" >> will be 0 or 1
    item "D" >> will be 0 or 1
    You could defined item "C" and "D" as binary.

    If item "B" should at least be 1, then add the constraint the cell for the "B" value should be ">=1" as well as an integer.

    For item "A" to be 1 or 2 you need to more constraints cell for item "A" should be ">=1" and "<=2" as well as an integer

    the last term

    * item "A" = 2
    * item "A" = 1 + item "C" = 1
    * item "A" = 1 + item "D" = 1
    I take to be if A = 2 C and D = 0 but if A=1 then C and D should be equal to 1 so perhaps a constraint like

    A + C = 2 and A + D = 2 as C and D are defined as binaries they can only take value 0 or 1.

    Alf

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Thanks a lot for reply. Forgive me as I have no great idea of using the solver so if possible can you attach a file with those constraints and I will study them through the file

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    No problem.

    As I said you need to setup the solver model for each different case I've made a copy of your sheet and set up each model on a separate sheet.

    For your second problem solver a different solution to what you posted but with the same result regarding excess value.

    One could change the constraint part a bit and remove the constraint line K12 = 2.

    As there is no value in cell D14 (Sheet2) the A value (cell J11) will always be 2 because of the constraint A + D = 2 (K12 = 2) but even if the constraint line is removed
    solver will still give the same result as the target option cell J15 has the same value in your setup and the solver solution and both solutions minimizes the target value i.e. cell J15.

    These result I found by using the "Simplex LP" engine but lo and behold if I remove the constraint K12 = 2 and change the solving engine to "GRG Nonlinear" I do get the same result i.e.
    1, 10, 1

    Solver works in mysterious ways its miracles to preform!

    GRG_solv.jpg



    Alf
    Attached Files Attached Files

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Thank you very very much for great help. I began to learn a little about the solver and it is great
    Now I have edited the Sheet2 example a little. Please have a look
    I just want to the solver to skip D14 because it is empty. Is that possible or the solver doesn't manipulate the boolean values?
    Attached Files Attached Files

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    In K11 I have adjusted the formula in that way and worked well
    =IF(D13="",SUM(J11,J14),IF(D14="",SUM(J11,J13),SUM(J11,J13,J14)))
    ** Last point .. Is that possible to convert those steps to a macro as I have a large sheet with the same processes?

  14. #14
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    Not sure about constraint for C and D as you last uploaded file had D as 1 (J14 = 1) but as D14 was blank it did not add anything to the target value.

    I've set up a macro to run solver in a loop. To test you must first set a reference to solver in "Visual Basic".

    Click on "Visual basic" icon, ->Tools ->References and find box marked solver and thick it. See jpg

    solv_ref.jpg

    Then you can run the "SolvLoop" macro that will find three solutions for you. I did just write the formulas in F9, J14 and N29 it could of course be written using a macro but
    I was a bit lazy.

    I've set up the macro using the "GRG Nonlinear" engine, I you do wish to use the "Simplex LP" model change this part of the line

    Engine:=1
    to

    Engine:=2
    Alf
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Thanks a lot Mr. Alf
    If you have navigated to the other thread you will find that we reached a very good point and you are the cause because you have adjusted the constraints in great way
    Thank you very very much for your great and awesome help

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    This is the last solution at this link
    http://www.eileenslounge.com/viewtop...224320#p224320

  17. #17
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    You are welcome.

    And thanks for feedback and rep

    Alf

  18. #18
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    Just for the fun of it I extended the macro to cover all the rows and columns only difference was I used the "Simplex LP" engine to run solver.

    Comparing the two results summing rows 9, 15 and 21 I got a total of 1.724 with the "Simplex LP" as opposed to 3.012 with the "GRG Nonlinear" engine.

    To test run macro "SolvLoop"

    Alf
    Attached Files Attached Files
    Last edited by Alf; 01-30-2018 at 01:57 AM. Reason: Adding formula line to macro

  19. #19
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Calculate according to proper size

    Thank you very much for awesome support Mr. Alf
    You are wonderful ..

    Best Regards

  20. #20
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Calculate according to proper size

    Think nothing of it as I do enjoy working with solver

    Regards Alf

+ 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. VBA Code to calculate Xbar-Bar with varying subgroup size
    By ironfelix717 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-22-2017, 04:15 PM
  2. Calculate dimensions of needed box based on size of machine being packed???
    By Denny Morgan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-28-2012, 05:36 AM
  3. [SOLVED] Need to calculate price from size and quantity selection
    By TheGecko in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-23-2012, 09:14 AM
  4. Replies: 2
    Last Post: 03-15-2012, 12:11 AM
  5. calculate the batch size
    By dtgheath in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-15-2010, 09:46 AM
  6. calculate the quantity of each size of an item
    By kimcole5 in forum Excel General
    Replies: 2
    Last Post: 11-26-2008, 01:32 PM
  7. Calculate prices using values entered by size
    By keascheer in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-12-2008, 12:57 AM

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