sum next twelve cells after hitting threshold

3  1 solution  High Priority ?  55 Views  1 Endorsement  Last Modified: 2018-09-06
need a formula to find the column where an array crosses a certain threshold

so need to find the column number where that happens

threshold is a certain number in a cell.  looking for the first time an array crosses that mark

note- an array is between columns H-CA

once it hits that sum threshold, NOWi need to sum the NEXt twelve columns of data.
finnstoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

byundtMechanical EngineerCommented:
Use the following formula:
=SumAfterThreshold(H1:CA1,12,19000,0)

Open in new window

The code is shown below:
Function SumAfterThreshold(rgToCheck As Range, LookAhead As Long, TargetValue As Double, Optional IncludePivotCell As Long = 0) As Variant
'Examines values in rgToCheck. When one of those equals or exceeds TargetValue, then sums the specified number of LookAhead columns.
'If LookAhead is 0, then all remaining columns in rgToCheck are included in the sum
'If IncludePivotCell is 0, it is not included in looking forward sum.
'If IncludePivotCell is 1, it is included in looking forward sum
Dim cel As Range, Finish As Range, rgAhead As Range
Dim dLookAhead As Double
Dim FinishAddr As String
Dim i As Long, j As Long
Set Finish = rgToCheck.Cells(rgToCheck.Cells.Count)
FinishAddr = Finish.Address
SumAfterThreshold = "Failed"

For Each cel In rgToCheck.Cells
    If cel.Value >= TargetValue Then
        Set rgAhead = Nothing
        dLookAhead = 0
        
        Select Case IncludePivotCell
        Case 0
            If Not cel.Address = FinishAddr Then Set rgAhead = cel.Offset(0, 1)
        Case 1
            Set rgAhead = cel
        End Select
            
        If Not rgAhead Is Nothing Then
            j = IIf(LookAhead = 0, Finish.Column, Application.Min(Finish.Column, rgAhead.Column + LookAhead - 1))
            Set rgAhead = Range(rgAhead, rgAhead.EntireRow.Cells(1, j))
            dLookAhead = Application.Sum(rgAhead)
        End If
        
        SumAfterThreshold = dLookAhead
        Exit Function
    
    End If
Next
End Function

Open in new window

RunningSumTestingQ29115305-SUMMING-.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
finnstoneAuthor Commented:
thanks a ton
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.