Sum of a number of column values until a value of another column is equal to a specific value.

janpol

New Member
Joined
Apr 30, 2019
Messages
6
Hi everyone,
I am trying to complete the following as a VBA loop for a production plan: The values in column A need to be summed for every occurrence of 40 in column B. The result is going to appear in column C as the total of all values until the next occurrence of 40. For example 2 + 12+ 4 = 18 appears in cell C1, and 8+15 = 23 appears in cell C4. It looks simple but somehow my approaches do not work.

Untitled.png
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
In column C add below formula
=IF(B1=40,SUMIF($D$1:$D$12,COUNTIF($B$1:B1,B1),$A$1:$A$12),0)

In Column D add below formula
=COUNTIF(B$1:B1,40)

For sample excel click Here
 
Upvote 0
Thank you very much! Do you think it is efficient to record this as macro for large number of records or can this be done in VBA in a For loop?
 
Upvote 0
Thank you very much! Do you think it is efficient to record this as macro for large number of records or can this be done in VBA in a For loop?
how large is your data . like count.
 
Upvote 0
Would this be sufficient?

VBA Code:
Sub Total_40()
  Dim lr As Long
  
  lr = Range("A" & Rows.Count).End(xlUp).Row
  With Range("C1:C" & lr)
    .FormulaR1C1 = "=IF(RC[-1]=40,SUM(RC[-2]:R" & lr + 1 & "C[-2])-SUM(R[1]C:R" & lr + 1 & "C),0)"
    .Value = .Value
  End With
End Sub
 
Upvote 0
Both suggestions worked perfect, thank you! I tried it myself too, it works for most but it is not calculating the last total. The loop may need a small adjustment. I would appreciate if you are able to spot the mistake.

VBA Code:
Sub Macro1()
Dim a(1 To 12) As Long
Dim b(1 To 12) As Long
Dim c(1 To 12) As Long
Dim m As Long
Dim i As Integer
Dim j As Integer
Dim x As Integer


For j = 1 To 12
    a(j) = ActiveSheet.Cells(j, 1)
    b(j) = ActiveSheet.Cells(j, 2)
Next j

c(1) = 40
x = 0

For j = 2 To 12

    If b(j) > b(j - 1) Then
        m = 0
        For i = x + 1 To j - 1
            m = m + a(i)
            c(x + 1) = m
            If j = 12 Then
              c(j) = a(j)
            End If
        Next i
        x = j - 1
    End If

Next j

For j = 1 To 12
ActiveSheet.Cells(j, 3) = c(j)
Next j

End Sub
 
Upvote 0
I would appreciate if you are able to spot the mistake.
Your code is a very long-winded way to read in the data & get the results but the (or at least one) error is this (assuming that column B reduces until it jumps back to 40 which appears to be the case from your code)

Rich (BB code):
For j = 2 To 12

    If b(j) > b(j - 1) Then
        m = 0
        For i = x + 1 To j - 1
            m = m + a(i)
            c(x + 1) = m
            If j = 12 Then
              c(j) = a(j)
            End If
        Next i
        x = j - 1
    End If

Next j
When j gets to 12
b(12) > B(11) will always be false, unless b(12) = 40
Therefore the code never gets to the amber code when j = 12


You can usually figure out things like that for yourself if you step through your code with the F8 key, noting what happens at each step.
 
Upvote 0
If you wanted to get those results with a looping macro, here is a more efficient way.

VBA Code:
Sub SumAt40()
  Dim a As Variant
  Dim b(1 To 12) As Long
  Dim i As Long
  Dim m As Long
  
  a = Range("A1:B12").Value
  For i = UBound(a) To 1 Step -1
    m = m + a(i, 1)
    If a(i, 2) = 40 Then
      b(i) = m
      m = 0
    End If
  Next i
  Range("C1:C12").Value = Application.Transpose(b)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,241
Members
449,075
Latest member
staticfluids

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