• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Flexible Sum to achieve given total

Ravi Malpani

New Member
I have 12 cells in a row. Each cell contains a number for example 12, 15,10, 5,7,15,25,12, 15,10, 5,7. on the other hand I have a number (50). I want to create a flexible formula to know total of how many cells will reach to a total of 50. So in the given example starting from first column total of 7 columns will be higher than 50. Can you please help me with this formula.
 
Here is a VBA solution for you
Code:
Option Explicit

Sub FindFifty()
    Dim i As Long, j As Long
    Dim x As Long, y As Long
    Dim lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    x = 0
    For j = 1 To lr
5:
        For i = 1 To 12
            y = Cells(j, i)
            x = x + y
            If x >= 50 Then
                MsgBox (Cells(j, i).Address)
                x = 0
                j = j + 1
                GoTo 5
            End If
        Next i
    Next j
End Sub

How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
A formula solution

upload_2018-6-25_5-39-42.png

1] Assume 12, 15,10, 5, 7,15, 25,12, 15,10, 5 and 7 located in A2:L2

2] "Given total" in N2 : 50

3] In "Result" O2, enter :

=MATCH(1,INDEX(0+(SUMIF(OFFSET(A2,,,,COLUMN(A2:L2)),"<>")>N2),0),0)

Regards
Bosco
 

Attachments

  • SumGivenTotal.xlsx
    11.1 KB · Views: 9
Last edited:
I have a additional query on this. Currently we applied this formula adding number from left to right, how can we modify this to add the numbers from right to left.
Count A2:L2 cumulative numbers > 50 from right to left, try this :

=MATCH(1,INDEX(0+(SUMIF(OFFSET(L2,,,,-COLUMN(A2:L2)),"<>")>N2),0),0)

Regards
Bosco
 
Back
Top