The Sum of A1 + A2 + A3 + A4 = 52

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,648
Team,

As I enter integers into range A1:A4, say 5 into cell A1 the remaining cells A2:A4 would populate with intergers where the sum of the range would total 52.

If A1 = 5, and I enter 15 into A4, cells A2 and A3 would populate with integers, where the sum of the range would total 52.

I do not have a clue where to begin.

Thanks for reading this post.

I hope someone has a solution.

Thanks in advance.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Team,

As I enter integers into range A1:A4, say 5 into cell A1 the remaining cells A2:A4 would populate with intergers where the sum of the range would total 52.

If A1 = 5, and I enter 15 into A4, cells A2 and A3 would populate with integers, where the sum of the range would total 52.

I do not have a clue where to begin.

Thanks for reading this post.

I hope someone has a solution.

Thanks in advance.

Do you want like comma separated list of possible value pairs or what kind of answer do you want? I mean, if A1 = 5 and A4 = 15 then there's 32 (?) chances for the remaining value pairs.

Could you manual calculate an answer where you use, say, 7 instead of 52? We would see what kind of answer you are looking for.
 
Upvote 0
Hello,

I am not sure how to automatically trigger this as there could be more than one entry to 'fire' the code..

Are there further details about the criterial for each?

I came up with the below, hope it helps.

-Jeff

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> FiftyTwo()<br><br><SPAN style="color:#00007F">Dim</SPAN> intCount        <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> intLoopCount    <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> intFeildAns     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br><br>intCount = Application.WorksheetFunction.Count(Range("A1:A4"))<br>intLoopCount = 4 - intCount<br><br>    <SPAN style="color:#00007F">If</SPAN> intLoopCount = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br>intFeildAns = (52 - Application.WorksheetFunction.Sum(Range("A1:A4"))) / intLoopCount<br><br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> intLoopCount<br>        <SPAN style="color:#00007F">If</SPAN> i = intLoopCount <SPAN style="color:#00007F">Then</SPAN><br>            Range("A1").Offset(i, 0).Value = 52 - _<br>                    Application.WorksheetFunction.Sum(Range("A1:A4"))<br>        <SPAN style="color:#00007F">Else</SPAN><br>            Range("A1").Offset(i, 0).Value = intFeildAns<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Hi hiker,

I have a the first step at a solution here based on randomly choosing numbers (I assume that there are no other criteria involved).

This assumes that that you input a number into A1 and has the potential drawback that numbers will *tend* to get smaller as move from A2 to A4 (A3 will not always be smaller but most of the time it will). I believe it is also possible to get 0 as an answer in one or more cells.

However, before going further, I will wait and see if this is at all useful for you.

Since you fill out A1 - here are the formulae for A2:A5

=INT(RAND()*(52-A1))
=INT(RAND()*(52-A1-A2))
=52-A3-A2-A1
=SUM(A1:A4)

Cheers, :)
 
Upvote 0
I see a problem with auto-filling.

The range starts off empty.
Type 7 in A1
the code then automaticaly fills the blank cells A2:A4 with 15 to make the total 52.
Type 7 in A2. The sum is now 44. The code tries to distribute 8 among the cells of the range.

Should the code do nothing, because there are no blank cells to fill?
Should the code add 2 to every cell (including the newly changed A2)?
Should the code add 2 to A1 and A3 and 3 to A4, altering the unchanged cells in the range to achieve the total?
Should the code add 4 to A3 and A4, leaving the user entered A1 and A2 unchanged? (How does it know that A1 was changed before A2)
 
Upvote 0
Team,

repairman615, shawnhet, thank you for your responses. Both work correctly according to the original posted requirement.

shawnhet, your respones works with integers, doubles, and negative numbers.

mikerickson, thank you for your response and the additional questions.

I am trying to assist someone else, and they just added another requirement:
If we change any of the values in range A1:A4, the other 3 cells should adjust accordingly so that cell A5 still equals 52.

I am open to a VBA solution, code for a Worksheet_Change Event?
 
Last edited:
Upvote 0
This uses a string-cutting algorithm that results in the 'correct' distribution of numbers totalling a particular value.

The constant iMin can be changed to any value. Call HikerChange from the worksheet change event.

Code:
Option Explicit
Public Const sRng   As String = "Sheet1!A1:A4"
Public Const iTot   As Long = 52
Public Const iMin   As Long = 1
 
Sub HikerChange(Target As Range)
    Dim rInt        As Range
    Dim adCal()     As Double
    Dim i           As Long
    Dim cell        As Range

    If Not Target.Worksheet Is Range(sRng).Worksheet Then Exit Sub
    Set rInt = Intersect(Target, Range(sRng))
    If rInt Is Nothing Then Exit Sub
    If rInt.Cells.Count > 1 Then Exit Sub
 
    On Error GoTo Oops
    Application.EnableEvents = False
    If iTot - WorksheetFunction.Sum(rInt) < iMin * Range(sRng).Cells.Count Then
        For Each cell In Range(sRng)
            If cell.Address <> rInt.Address Then
                cell.Value2 = "Oops!"
            End If
        Next cell
 
    Else
        adCal = aiRandLen(dTot:=iTot - rInt.Value2, _
                          nNum:=Range(sRng).Cells.Count - 1, _
                          dMin:=iMin, _
                          iSig:=0)
        
        For Each cell In Range(sRng)
            If cell.Address <> rInt.Address Then
                i = i + 1
                cell.Value2 = adCal(i)
            End If
        Next cell
    End If
Oops:
    Application.EnableEvents = True
End Sub
 
Function aiRandLen(ByVal dTot As Double, _
                   nNum As Long, _
                   Optional ByVal dMin As Double = 0, _
                   Optional ByVal iSig As Long = 307) As Double()
    ' shg 2011
    ' Applies string-cutting to return an array of nNum
    ' numbers totalling dTot, with each in the range
    '    dMin <= number <= Round(dTot, iSig) - nNum * round(dMin, iSig)
    ' Each number is rounded to iSig decimals
    Dim i           As Long
    Dim j           As Long
    Dim dRnd        As Double
    Dim dSig        As Double
    Dim col         As Collection
    Dim adOut()     As Double
 
    dTot = WorksheetFunction.Round(dTot, iSig)
    dMin = WorksheetFunction.Round(dMin, iSig)
    If nNum < 1 Or dTot < nNum * dMin Then Exit Function
 
    ReDim adOut(1 To nNum)
    dSig = 10 ^ -iSig
 
    With New Collection
        .Add Item:=0
        .Add Item:=dTot - nNum * dMin
 
        ' create the cuts
        For i = 1 To nNum - 1
            dRnd = Int(Rnd * ((dTot - nNum * dMin) / dSig)) * dSig
            ' insertion-sort the cut
            For j = .Count To 1 Step -1
                If .Item(j) <= dRnd Then
                    .Add Item:=dRnd, After:=j
                    Exit For
                End If
            Next j
        Next i
        ' measure the lengths
        For i = 1 To nNum
            adOut(i) = .Item(i + 1) - .Item(i) + dMin
        Next i
    End With
    aiRandLen = adOut
End Function
 
Upvote 0
shg,

That is simply amazing.

Thank you soooooooooo much.

I will be investigating the code to learn how it works.

What a great site for learning new things.

Another one for the archives.
 
Upvote 0
You're welcome.

This line

Code:
    If iTot - WorksheetFunction.Sum(rInt) < iMin * Range(sRng).Cells.Count Then

should be

Code:
    If iTot - rInt.Value2 < iMin * (Range(sRng).Cells.Count - 1) Then
 
Upvote 0

Forum statistics

Threads
1,222,422
Messages
6,165,945
Members
451,997
Latest member
Maaaart

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top