+ Reply to Thread
Results 1 to 19 of 19

Alter given values to achieve desired result

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-16-2014
    Location
    Witbank, SA
    MS-Off Ver
    Excel 2013
    Posts
    268

    Alter given values to achieve desired result

    Hi good people!,

    Want to play with some numbers? Then this is for you... if you look at the attachment block A, we have the yellow, which is given, the green which also is given and the blue which is a given calculation. The result is the red. I was wondering if maybe by code or clever formulae, it could be done to alter the values in the green, which will now display in block B, in such a way that the sum of these values still come to the given 3000 in this scenario, but the previous red result would now be the orange result, which ties up with the values in yellow?. I think the problem might come in with the fact that the values in blue also recalculates as the green is altered.

    Is there anyone who might know of any method to achieve this. All and any help is always accepted with great humility and appreciation. Thank you..
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Alter given values to achieve desired result

    Sorry - you have 4 unknown values (cells D3:G3 - which you want to change), but only two constraints (or equations): that the sum of the 4 variables be equal to A3, and that the Sum of the Values divided by the constants in D2:G2 be another specific value (A3/B3).

    They will not give a unique solution - in fact, they will give an almost infinite number of solutions - the first two solutions from my brute force solving are, for example:

    1 934 2055 10
    1 944 2015 40

    You can only solve those for 2 of the unknowns as expressions of the other 2 values.

    Here is code that does the brute force solution, for just one value of the first cell (D3 = 1):

    Sub FirstSolution()
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
        
        Dim i1 As Integer
        Dim j1 As Integer
        Dim k1 As Integer
        Dim l1 As Integer
        
        Dim iH As Integer
        Dim iL As Integer
        
        Dim lRow As Long
        Dim Sh1 As Worksheet
        Dim Sh2 As Worksheet
        
        Set Sh1 = Worksheets("Sheet1")
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("Solutions").Delete
        On Error GoTo 0
        
        Set Sh2 = Worksheets.Add(After:=Sh1)
        Sh2.Name = "Solutions"
        
        iH = Sh1.Range("A3").Value
        iL = Sh1.Range("A3").Value / Sh1.Range("B3").Value
        
        i1 = Sh1.Range("D2").Value
        j1 = Sh1.Range("E2").Value
        k1 = Sh1.Range("F2").Value
        l1 = Sh1.Range("G2").Value
        
        lRow = 0
        
        i = i1
        For j = j1 To iH - i Step j1
            For k = k1 To iH - i - j Step k1
                l = iH - i - j - k
                If l Mod l1 = 0 Then
                    If i / i1 + j / j1 + k / k1 + l / l1 = iL Then
                        lRow = lRow + 1
                        With Sh2
                            .Cells(lRow, "A") = i
                            .Cells(lRow, "B") = j
                            .Cells(lRow, "C") = k
                            .Cells(lRow, "D") = l
                        End With
                    End If
                End If
            Next k
        Next j
        
    End Sub
    Last edited by Bernie Deitrick; 01-22-2015 at 02:45 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Valued Forum Contributor
    Join Date
    12-28-2014
    Location
    NSW, Australia
    MS-Off Ver
    MS365
    Posts
    606

    Re: Alter given values to achieve desired result

    Hi juriemagic, the answer in red/orange is 3000 divided by the blue total, so if you want a different answer and you need to keep the 3000 then the blue has to change. When you have 3 parts of an equation and 2 of them are fixed then only 1 is left.
    The blue is the relationship between green and the numbers above green. Can either of those change or only the green?
    I did notice that your desired answer is exactly double the incorrect result. Can you just add a /2 into the blue cells?
    e.g. Cell I3 =D3/D2/2 (copy over to L3)

    Edit: Sorry Bernie Deitrick, I started typing this ages ago while I was playing with the numbers. I didn't realize that you had answered this (and much better then me too). I should have refreshed the page before I posted.
    Attached Files Attached Files
    Last edited by Beamernsw; 01-22-2015 at 03:33 PM.

  4. #4
    Forum Contributor
    Join Date
    04-16-2014
    Location
    Witbank, SA
    MS-Off Ver
    Excel 2013
    Posts
    268

    Re: Alter given values to achieve desired result

    Good day Bernie Deitrick, You are the man!!!..finally I have received an answer to this problem!!, and an excellent one is it!!. The results I get in the solutions sheet I use now to calculate the second value I need. I am as happy as a pig in Philistine!!

    May I ask just one more thing...the solutions given by the code, can I safely assume they will be ALL possible solutions? For the 3000 I understand there are over a million solutions, but not all of them, when calculated (i.e. divided by the constants and their sum devided back into 3000), will actually give the 3.409 as in this example. ONLY the ones given by the code are the possible (out of ALL possible) solutions for 3000, that will also give the 3.409?..Can I assume this?

  5. #5
    Forum Contributor
    Join Date
    04-16-2014
    Location
    Witbank, SA
    MS-Off Ver
    Excel 2013
    Posts
    268

    Re: Alter given values to achieve desired result

    Hi Bernie Deitrick, Please, just one more thing, I promise to "release" you then.. haha..

    I was thinking and came to the conclusion that I will need to add at least one column in the green, and obviously also in the blue, the reason being: column D will have all 1's..the remaining constants have their values which sum to 3000. However, the need for the constant "1", in column D also need to have values. So if I add one column next to column "D", for example, this new column D, can then have the 1's, while columns E-H (4 columns), will now have real values which will add up to 3000. All I do is make the header of this new column D also 1. Do you think this idea will work?

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Alter given values to achieve desired result

    For your stated problem, there are 30,352 solutions, all given by this code - all give the 3.409 ratio as desired, since that is part of the check.

    Option Explicit
    
    Sub AllSolutions()
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim l As Integer
        
        Dim i1 As Integer
        Dim j1 As Integer
        Dim k1 As Integer
        Dim l1 As Integer
        
        Dim iH As Integer
        Dim iL As Integer
        Dim iC As Integer
        
        Dim lRow As Long
        Dim Sh1 As Worksheet
        Dim Sh2 As Worksheet
        
        Set Sh1 = Worksheets("Sheet1")
        On Error Resume Next
        Application.DisplayAlerts = False
        Worksheets("Solutions").Delete
        On Error GoTo 0
        
        Set Sh2 = Worksheets.Add(After:=Sh1)
        Sh2.Name = "Solutions"
        
        iH = Sh1.Range("A3").Value
        iL = Sh1.Range("A3").Value / Sh1.Range("B3").Value
        
        i1 = Sh1.Range("D2").Value
        j1 = Sh1.Range("E2").Value
        k1 = Sh1.Range("F2").Value
        l1 = Sh1.Range("G2").Value
        
        lRow = 0
        
        For i = i1 To iH Step i1
            For j = j1 To iH - i Step j1
                For k = k1 To iH - i - j Step k1
                    l = iH - i - j - k
                    If l Mod l1 = 0 Then
                        If i / i1 + j / j1 + k / k1 + l / l1 = iL Then
                            lRow = lRow + 1
                            With Sh2
                                .Cells(lRow, "A") = i
                                .Cells(lRow, "B") = j
                                .Cells(lRow, "C") = k
                                .Cells(lRow, "D") = l
                            End With
                        End If
                    End If
                Next k
            Next j
        Next i
        
    End Sub

  7. #7
    Forum Contributor
    Join Date
    04-16-2014
    Location
    Witbank, SA
    MS-Off Ver
    Excel 2013
    Posts
    268

    Re: Alter given values to achieve desired result

    When I run the code in the Excel file provided, I get 53 solutions only. I do not understand why you say there are 30,352 solutions?. Also, please, how do I alter the code to add one column to the left of column D and one to the left of column I, so that the code will work with columns D-H and J-N, as opposed to with columns D-G and I-L?. I am able to alter the code but get stuck right at the bottom, which is here:
         End With
                        End If
                    End If
                Next k
            Next j
        Next i

  8. #8
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,301

    Re: Alter given values to achieve desired result

    Play with numbers.xlsm

    Click the button in the attached.

+ 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] IF formula with certain conditions to achieve a desired value
    By Excel Dumbo in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-11-2014, 10:59 PM
  2. Combine two vba Codes to achieve a result
    By newqueen in forum Excel Programming / VBA / Macros
    Replies: 43
    Last Post: 06-12-2013, 06:15 AM
  3. How do you achieve the result of cell date activation
    By Mrsharhar in forum Excel General
    Replies: 1
    Last Post: 01-06-2013, 04:32 AM
  4. Excel 2007 : calculating numbers needed to achieve best result
    By candiecane_81 in forum Excel General
    Replies: 3
    Last Post: 06-20-2011, 12:40 AM
  5. How do I achieve this result for Multiple range of number
    By pashaK in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-07-2007, 12:39 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